Wcf 何时何地设置自定义IOperationVoker?
我正在尝试扩展WCF,以便拥有一个RESTful web服务,在该服务中,对于每个操作,我都要对HTTP授权头进行验证,我使用该头的值来调用Login()方法 登录完成后,如果引发安全异常,我希望调用操作的相应方法检查,在这种情况下,我将使用适当的HTTP状态代码以自定义的“拒绝访问”消息进行回复 考虑到这一点,我认为实现IEndpointBehavior将IOperationInvoker的实现应用于每个操作(设置DispatchOperation.Invoker属性)将是一个好主意 我决定使用Decorator设计模式实现一个IOperationInvoker。我的实现需要在其构造函数中另一个IOperationInvoker,方法调用将委托给它 这是我的IOperationVokerImplementation:Wcf 何时何地设置自定义IOperationVoker?,wcf,Wcf,我正在尝试扩展WCF,以便拥有一个RESTful web服务,在该服务中,对于每个操作,我都要对HTTP授权头进行验证,我使用该头的值来调用Login()方法 登录完成后,如果引发安全异常,我希望调用操作的相应方法检查,在这种情况下,我将使用适当的HTTP状态代码以自定义的“拒绝访问”消息进行回复 考虑到这一点,我认为实现IEndpointBehavior将IOperationInvoker的实现应用于每个操作(设置DispatchOperation.Invoker属性)将是一个好主意 我决定使
public class BookSmarTkOperationInvoker : IOperationInvoker{
private readonly IOperationInvoker invoker;
public BookSmarTkOperationInvoker(IOperationInvoker decoratee)
{
this.invoker = decoratee;
}
public object[] AllocateInputs()
{
return this.invoker.AllocateInputs();
}
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
BeforeOperation(); // Where there's code to perform the login using WebOperationContext.Current
object o = null;
try
{
o = this.invoker.Invoke(instance, inputs, out outputs);
}
catch (Exception exception)
{
outputs = null;
return AfterFailedOperation(exception); // Return a custom access denied response
}
return o;
}
public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
{
throw new Exception("The operation invoker is not asynchronous.");
}
public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
{
throw new Exception("The operation invoker is not asynchronous.");
}
public bool IsSynchronous
{
get
{
return false;
}
}
}
我决定通过扩展我已经需要的行为(WebHttpBehavior)来实现IEndpointBehavior。我只使用了一个beavior。下面是我编写的代码:
public class BookSmarTkEndpointBehavior : WebHttpBehavior
{
public override void Validate(ServiceEndpoint endpoint)
{
base.Validate(endpoint);
}
public override void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
base.AddBindingParameters(endpoint, bindingParameters);
}
public override void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
base.ApplyDispatchBehavior(endpoint, endpointDispatcher);
foreach (DispatchOperation operation in endpointDispatcher.DispatchRuntime.Operations)
{
IOperationInvoker defaultInvoker = operation.Invoker;
IOperationInvoker decoratorInvoker = new BookSmarTkOperationInvoker(defaultInvoker);
operation.Invoker = decoratorInvoker;
Console.Write("Before: " + ((object)defaultInvoker ?? "null"));
Console.WriteLine(" After: " + operation.Invoker);
}
}
public override void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
base.ApplyClientBehavior(endpoint, clientRuntime);
throw new Exception("The BookSmarTkEndointBehavior cannot be used in client endpoints.");
}
}
现在问题来了:
<services>
<service name="BookSmarTk.Web.Service.BookSmarTkService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/service"/>
</baseAddresses>
</host>
<endpoint
address=""
behaviorConfiguration="BookSmaTkEndpointBehavior"
binding="webHttpBinding"
bindingConfiguration="BookSmarTkBinding"
contract="BookSmarTk.Web.Service.BookSmarTkService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name ="BookSmartkServiceBehavior">
<serviceDebug httpHelpPageEnabled="true" httpHelpPageUrl="/help.htm" includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="BookSmaTkEndpointBehavior">
<!--<webHttp/>-->
<bookSmarTkEndpointBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="BookSmarTkBinding">
</binding>
</webHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="bookSmarTkEndpointBehavior" type="BookSmarTk.Web.Service.BookSmarTkEndpointBehaviorElement, BookSmarTk.Web.Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
读到这里,我对你深表感激。真的,谢谢你!我正在构建类似的东西(我想,我没有时间浏览你所有的代码),但我用了不同的方式 为了实现这一点,我使用以下方法:
- 用于读取传入HTTP请求消息头的IMessageInspector(在本例中,从cookie中提取会话Id并从缓存中检索会话对象)
- IPrincipal和IAuthorizationPolicy的组合,用于实现我自己的自定义授权代码(WCF将自动调用我的代码,以请求设置了“[PrincipalPermission(SecurityAction.Demand,Role=“somerole”)]”属性的web服务方法)
- 从web服务方法捕获任何未捕获异常的IErrorHandler(包括在授权失败时引发的权限拒绝异常,即在IPrincipal中实现的IsRole方法返回false)。如果捕获到安全拒绝异常,则可以使用WebOperationContext.Current为响应消息设置自定义HTTP错误代码
- 自定义行为(IContractBehavior-但您也可以使用端点或服务行为或任何您想要的行为),它在运行时创建上述所有行为并将它们附加到适当的端点
public class MyOperationBehavior: IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.Invoker = new BookSmarTkOperationInvoker(dispatchOperation.Invoker);
}
public void Validate(OperationDescription operationDescription)
{
}
}
然后在ApplyDispatchBehavior()中设置该行为:
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
foreach (var operation in endpoint.Contract.Operations) {
if (operation.Behaviors.Contains(typeof(MyOperationBehavior)))
continue;
operation.Behaviors.Add(new MyOperationBehavior());
}
}
我知道这很古老,但对我来说 工作。但是,仅当ApplyDispatchBehavior方法调用基本方法时。如下所示:
public override void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
base.ApplyDispatchBehavior(endpoint, endpointDispatcher);
foreach (var operation in endpoint.Contract.Operations)
{
if (operation.Behaviors.Contains(typeof(AccessControlOperationBehaviour)))
continue;
operation.Behaviors.Add(new AccessControlOperationBehaviour());
}
}