抛出 IOperationBehavior 中使用的新 WebFaultException 不起作用
Throw new WebFaultException used in IOperationBehavior does not work
我在扩展属性、IOperationBehavior、IParameterInspector 时抛出 WebFaultException,但它不起作用。
public class ApplicationNotSupportedAttribute : Attribute, IOperationBehavior, IParameterInspector
{
private readonly ApplicationNotSupportedBehaviour behaviour;
public ApplicationNotSupportedAttribute()
{
Logging<string>.Error("ApplicationNotSupportedAttribute");
behaviour = new ApplicationNotSupportedBehaviour();
}
public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
behaviour.AddBindingParameters(operationDescription, bindingParameters);
}
public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation)
{
behaviour.ApplyClientBehavior(operationDescription, clientOperation);
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
{
behaviour.ApplyDispatchBehavior(operationDescription, dispatchOperation);
}
public void Validate(OperationDescription operationDescription)
{
behaviour.Validate(operationDescription);
}
public object BeforeCall(string operationName, object[] inputs)
{
throw new WebFaultException(HttpStatusCode.Forbidden);
}
public void AfterCall(string operationName, object[] outputs,
object returnValue, object correlationState)
{
}
然后我在服务中有:
[WebInvoke(Method = "POST", UriTemplate = "myMethod", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
[ApplicationNotSupportedAttribute]
myMethodResult myMethod(MyMethodRequest myMethodRequest);
在我 运行 上面的代码之后,我得到 200 Ok。如果我添加
throw new WebFaultException(HttpStatusCode.Forbidden);
在 myMethod 中我得到 403 Forbidden。如以下代码所示:
public myMethodResult myMethod(MyMethodRequest myMethodRequest)
{
throw new WebFaultException<myObject>(myObj, HttpStatusCode.Forbidden);
}
有没有可能在我们真正进入 myMethod 之前得到 403?
我在 class ApplicationNotSupportedAttribute 中发现了问题,除了 BeforeCall 和 ApplyDispatchBehavior 外,所有被覆盖的方法都可能是空的,它们应该如下所示:
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(this);
}
public object BeforeCall(string operationName, object[] inputs)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden;
throw new WebFaultException<string>("Unauthorized", HttpStatusCode.Forbidden);
}
我好像错过了 dispatchOperation.ParameterInspectors.Add(this)。这解决了我的问题。
我在扩展属性、IOperationBehavior、IParameterInspector 时抛出 WebFaultException,但它不起作用。
public class ApplicationNotSupportedAttribute : Attribute, IOperationBehavior, IParameterInspector
{
private readonly ApplicationNotSupportedBehaviour behaviour;
public ApplicationNotSupportedAttribute()
{
Logging<string>.Error("ApplicationNotSupportedAttribute");
behaviour = new ApplicationNotSupportedBehaviour();
}
public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
behaviour.AddBindingParameters(operationDescription, bindingParameters);
}
public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation)
{
behaviour.ApplyClientBehavior(operationDescription, clientOperation);
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
{
behaviour.ApplyDispatchBehavior(operationDescription, dispatchOperation);
}
public void Validate(OperationDescription operationDescription)
{
behaviour.Validate(operationDescription);
}
public object BeforeCall(string operationName, object[] inputs)
{
throw new WebFaultException(HttpStatusCode.Forbidden);
}
public void AfterCall(string operationName, object[] outputs,
object returnValue, object correlationState)
{
}
然后我在服务中有:
[WebInvoke(Method = "POST", UriTemplate = "myMethod", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
[ApplicationNotSupportedAttribute]
myMethodResult myMethod(MyMethodRequest myMethodRequest);
在我 运行 上面的代码之后,我得到 200 Ok。如果我添加
throw new WebFaultException(HttpStatusCode.Forbidden);
在 myMethod 中我得到 403 Forbidden。如以下代码所示:
public myMethodResult myMethod(MyMethodRequest myMethodRequest)
{
throw new WebFaultException<myObject>(myObj, HttpStatusCode.Forbidden);
}
有没有可能在我们真正进入 myMethod 之前得到 403?
我在 class ApplicationNotSupportedAttribute 中发现了问题,除了 BeforeCall 和 ApplyDispatchBehavior 外,所有被覆盖的方法都可能是空的,它们应该如下所示:
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(this);
}
public object BeforeCall(string operationName, object[] inputs)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden;
throw new WebFaultException<string>("Unauthorized", HttpStatusCode.Forbidden);
}
我好像错过了 dispatchOperation.ParameterInspectors.Add(this)。这解决了我的问题。