快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门24小时娱乐在线_龟发之家论坛



近来开拓一套由客户方定制的办事,听说之前版本是经由过程C写的WebService。那个神奇的Service我是没见过。只是有一点,之前的验证历程居然是这样进行的:客户端发送账号、密码,Service进行验证。验证成功后,Service会将孕育发生一个加密字符,以类似Session要领存储并发送一个加密字符串给客户端。之后客户端每次调用办事都发送这个加密字符串以供办事端进行验证合法性。虽然小我感觉极为掉常,然则交涉未果。别的还有一点需求便是:办事端会有一个授权文件,这个授权文件里面寄放的是对客户端接口调用的授权信息。

基于以上缘故原由,斟酌到WCF优秀的扩展性抉择对他进行必然的澳门24小时娱乐在线扩展,以完成以上需求。

因为客户端每次发送加密字符串用作身份凭据、以及授权的需求,是以选择IParameterInspector进行扩展。

大年夜家都知道经由过程设置设置设备摆设摆设的要领可以使法度榜样加倍机动。那么我们应该斟酌一下如下几个问题:

1、若何经由过程设置设置设备摆设摆设的要领实现扩展?

2、设置设置设备摆设摆设今后,如何使我们的扩展对WCF框架生效呢。?

对付第一个问题,在WCF中,可以经由过程承袭BehaviorExtensionElement来实现;第二个问题,将参数检测利用到遣散点行径上就能实现我们预期的目标,在这个历程中还必要实现IEndpointBehavior接口。

开始先容之前先看看IParameterInspector接口定义:

public interface IParameterInspector

{///

/// 用于操作调用完成后///

///

接口操作名称///

调用参数

///

返回值///

与BeforeCall的关联状态

void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState){

}

///

/// 用于操作调用前///

///

接口操作名称///

调用参数

/// AfterCall中的correlationState。假如AfterCall必要用到correlationState,就返回;否则返回nullpublic object BeforeCall(string operationName, object[] inputs)

{

} }

实现扩展的步骤:

1、实现IParameterInspector接口:

public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)

{}

public object BeforeCall(string operationName, object[] inputs)

{//登岸接口不做反省

const string actionName = "Login";if (actionName == operationName)

{return null;

}Dictionarystring, Operation> dictionary = GetAuthorizationInfo();

if (0 == dictionary.Count){

ServiceFaultException exception =GetFormContainer(ConfigSetting.Instance.GetAuthorizationFileNotExistContainerName());

throw new FaultException(exception, exception.Reason,FaultCode.CreateSenderFaultCode(

exception.ErrorCode.ToString(CultureInfo.InvariantCulture), string.Empty));

}if (!dictionary.ContainsKey(operationName)

|| Operation.Deny == dictionary[operationName]){

ServiceFaultException exception =GetFormContainer(ConfigSetting.Instance.GetNotAccessContainerName());

throw new FaultException(exception, exception.Reason,FaultCode.CreateSenderFaultCode(

except澳门24小时娱乐在线ion.ErrorCode.ToString(CultureInfo.InvariantCulture), string.Empty));

}bool flag = inputs.Any(input => (null != input) && CacheManager.ContainsKey(input.ToString()));

if (!flag){

var exception = GetFormContainer(ConfigSetting.Instance.GetClientTicketOutContainerName());throw new FaultException(exception, exception.Details,

FaultCode.CreateSenderFaultCo澳门24小时娱乐在线de(new FaultCode(

exception.ErrorCode.ToString(CultureInfo.CurrentCulture))));

}return null;

}

2、实现IEndpointBehavior接口,以将参数检测利用到DispatchRuntime的Operations的参数反省器中:

internal class ValidEndpointBehavior : IEndpointBehavior

{#region IEndpointBehavior Members

public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)

{

}

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime){

}

public void ApplyDispatchBehavior(Se澳门24小时娱乐在线rviceEndpoint endpoint, EndpointDispatcher endpointDispatcher)

{SynchronizedKeyedCollectionstring, DispatchOperation> keyedCollection =

endpointDispatcher.DispatchRuntime.Operations;foreach (var dispatchOperation in keyedCollection)

{dispatchOperation.ParameterInspectors.Add(new ParameterInspector());

}

}

public void Validate(ServiceEndpoint endpoint){

}

#endregion

}

注:以上扩展我只利用在了办事端。假如想利用在客户端,就应对ApplyClientBehavior中进行实现

3、承袭抽象类BehaviorExtesionElement,以在设置设置设备摆设摆设文件中设置设置设备摆设摆设参数检测器。

internal class ExstensionBehaviorElement : BehaviorExtensionElement

{public override Type BehaviorType

{get { return typeof (ValidEndpointBehavior); }

}

protected override object CreateBehavior(){

return new ValidEndpointBehavior();}

}

4、利用扩展:

system.serviceModel>

extensions>behaviorExtensions>

add name="endpointExstention"type="WcfExtensions.ExstensionBehaviorElement, WcfExtensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

behaviorExtensions>extensions>

bindings>basicHttpBinding>

binding maxReceivedMessageSize="2147483647" name="vrvbasicBinding">readerQuotas maxStringContentLength="2147483647"/>

binding>basicHttpBinding>

bindings>serviceHostingEnvironment>

serviceActivations>add service="VrvService.StateGrid.TerminalService" relativeAddress="TerminalService.svc"/>

add service="VrvService.StateGrid.MapRegionService" relativeAddress="MapRegionService.svc"/>add service="VrvService.LoginService" relativeAddress="LoginService.svc"/>

serviceActivations>serviceHostingEnvironment>

behaviors>serviceBehaviors>

behavior>serviceMetadata httpGetEnabled="true"/>

serviceDebug includeExceptionDetailInFaults="True"/>dataContractSerializer maxItemsInObjectGraph="3000"/>

behavior>serviceBehaviors>

endpointBehaviors>behavior>

endpointExstention/>behavior>

endpointBehaviors>behaviors>

system.serviceModel>

着末对IParameterInspector接口中AfterCall 中参数correlationState进行一下验证。我们应该若何利用它?

我将IParameterInspector接口中BeforeCall改为如下:

public object BeforeCall(string operat澳门24小时娱乐在线ionName, object[] inputs)

{//登岸接口不做反省

const string actionName = "Login";if (actionName == operationName)

{return "ParameterInspector operationName is Login";

}}

按 Ctrl+C 复制代码

您可能还会对下面的文章感兴趣: