如果在WCF REST调用中出现问题,例如找不到请求的资源,我如何在我的OperationContract方法中使用HTTP响应代码(例如将其设置为HTTP 404)?
我已经看到几个对WebServiceHost2Factory的引用作为用于有效处理WCF Rest服务中的错误的类.显然,在该类中,我只需要抛出一个WebProtocolException,并且响应的主体将包含相关信息.
这堂课现在似乎已经失宠了..NET 4堆栈中是否存在替换?
我想弄清楚如果出现问题,如何在POST操作的响应体中返回错误文本.关键问题是在所有*的旁边
例:
[Description("Performs a full enroll and activation of the member into the Loyalty program")]
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/fullenroll/{clientDeviceId}",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public MemberInfo FullEnroll(string clientDeviceId, FullEnrollmentRequest request)
{
Log.DebugFormat("FullEnroll. ClientDeviceId: {0}, Request:{1}", clientDeviceId, request);
MemberInfo ret = null;
try
{
//Do stuff
}
catch (FaultException<LoyaltyException> fex)
{
Log.ErrorFormat("[loyalty full enroll] Caught faultexception attempting to full enroll. Message: {0}, Reason: {1}, Data: {2}", …
Run Code Online (Sandbox Code Playgroud) 那么,我在使用自定义属性装饰的几个服务中获得了许多方法。
该属性用途ApplyDispatchBehavior
从IOperationBehavior
附加到方法和BeforeCall
和AfterCall
从方法IParameterInspector
之前和服务方法被调用后做一些处理。
一切都很好,很好……当服务方法需要返回 http 状态和消息时,问题就来了。我能找到的唯一方法是让方法WebFaultException<T>
传递我们的消息和适当的 http 状态。
问题AfterCall
在于从未调用附加属性。
它的长短是:
WebFaultException<T>
有没有办法仍然可以使用返回的消息调用我的属性?