Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 当出现异常时,有没有办法在WCF中处理HTTP响应?_.net_Wcf_Web Services - Fatal编程技术网

.net 当出现异常时,有没有办法在WCF中处理HTTP响应?

.net 当出现异常时,有没有办法在WCF中处理HTTP响应?,.net,wcf,web-services,.net,Wcf,Web Services,我有一个类似以下内容的web服务: [ServiceContract] public class MyService { [OperationContract] public void BreakStuff() { throw new MyException(); } } 它现在不是一个非常强大的web服务,但是你看,它会很棒的 如果调用BreakStuff(),则如果IncludeExceptionDetailsInFaults设置为true,WCF将捕获

我有一个类似以下内容的web服务:

[ServiceContract]
public class MyService
{
   [OperationContract]
   public void BreakStuff()
   {
      throw new MyException();
   }
}
它现在不是一个非常强大的web服务,但是你看,它会很棒的

如果调用
BreakStuff()
,则如果
IncludeExceptionDetailsInFaults
设置为true,WCF将捕获异常,返回HTTP 500并可选地抛出序列化堆栈跟踪。我想重写这个行为,抛出我自己的自定义错误消息对象,并返回HTTP200。我之所以想这样做,是因为我认为在某些情况下抛出异常比在代码中乱扔try/catch块以及让所有web方法返回某种结果对象更干净

我所做的是实现我自己的
IDispatchMessageFormatter
,它具有以下契约:

public interface IDispatchMessageFormatter
{
   void DeserializeRequest(Message message, object[] parameters);
   Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result);
}
SerializeReply
被调用用于正常的HTTP 200响应,但是如果web方法引发异常,WCF不会调用
SerializeReply
,因此我没有机会控制响应输出


WCF是否提供自定义错误响应的功能?

您可以通过实现接口来实现自定义错误处理程序。我过去曾使用这种方法以JSON格式返回自定义错误信息。在该方法中,还可以更改响应代码。下面是我使用过的一种方法的示例

public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{   
    fault = GetJsonResponseFromException(version, error);
    ConfigureMessageFormat(ref fault);
    ConfigureHttpReponse(ref fault, error);
}

protected virtual void ConfigureMessageFormat(ref Message fault)
{
    var formatting =
      new WebBodyFormatMessageProperty(WebContentFormat.Json);
    fault.Properties.Add(WebBodyFormatMessageProperty.Name, formatting);
}

protected virtual void ConfigureHttpReponse(ref Message fault, Exception error)
{
    var exception = error as WebFaultException<ServiceFault>;
    var statusCode = HttpStatusCode.InternalServerError;
    var description = HttpWorkerRequest
        .GetStatusDescription(Convert.ToInt32(statusCode));

    if (exception != null)
    {
        statusCode = exception.StatusCode;
        description = exception.Reason.Translations[0].Text;
    }

    var httpResponse = new HttpResponseMessageProperty()
    {
        StatusCode = statusCode,
        StatusDescription = description
    };

    httpResponse.Headers[HttpResponseHeader.ContentType] = "application/json";
    fault.Properties.Add(HttpResponseMessageProperty.Name, httpResponse);
}
public void providefaulture(异常错误,消息版本,ref消息错误)
{   
fault=GetJSONResponseFromeException(版本,错误);
ConfigureMessageFormat(参考故障);
配置HttpReporte(参考故障,错误);
}
受保护的虚拟void ConfigureMessageFormat(ref消息错误)
{
变量格式=
新的WebBodyFormatMessageProperty(WebContentFormat.Json);
添加(WebBodyFormatMessageProperty.Name,格式化);
}
受保护的虚拟无效配置HttpReporte(ref消息错误,异常错误)
{
var exception=错误为WebFaultException;
var statusCode=HttpStatusCode.InternalServerError;
var description=HttpWorkerRequest
.GetStatusDescription(转换为.ToInt32(状态代码));
if(异常!=null)
{
statusCode=exception.statusCode;
description=异常。原因。翻译[0]。文本;
}
var httpResponse=new HttpResponseMessageProperty()
{
状态代码=状态代码,
状态描述=描述
};
httpResponse.Headers[HttpResponseHeader.ContentType]=“应用程序/json”;
添加(HttpResponseMessageProperty.Name,httpResponse);
}