如何在代码中设置WCF错误消息返回格式?

如何在代码中设置WCF错误消息返回格式?,wcf,Wcf,我有这样的服务合同: [WebGet(UriTemplate = "getdata?key={key}&format={format}")] Event[] GetIncidentsXml(string key, string format); var selectedFormat = ParseWebMessageFormat(format); WebOperationContext.Current.OutgoingResponse.Format = selectedFormat;

我有这样的服务合同:

[WebGet(UriTemplate = "getdata?key={key}&format={format}")]
Event[] GetIncidentsXml(string key, string format);
var selectedFormat = ParseWebMessageFormat(format);
WebOperationContext.Current.OutgoingResponse.Format = selectedFormat;
在代码中,我切换出如下响应格式:

[WebGet(UriTemplate = "getdata?key={key}&format={format}")]
Event[] GetIncidentsXml(string key, string format);
var selectedFormat = ParseWebMessageFormat(format);
WebOperationContext.Current.OutgoingResponse.Format = selectedFormat;
ParseWebMessageFormat是结束类型的枚举分析的方法

这部分按预期工作,我根据传递的参数获取XML或JSON

当我抛出异常时,它就会崩溃。如果传入的API密钥无效,我将执行以下操作:

var exception = new ServiceResponse
{
    State = "fail", 
    ErrorCode = new ErrorDetail { Code = "100", Msg = "Invalid Key" }
};

throw new WebProtocolException(HttpStatusCode.BadRequest, "Invalid Key.", exception, null);
引发异常时,返回类型始终为XML:

返回类型更改是服务方法中的第一行代码,因此在引发异常之前发生

我知道我可以根据请求格式将WCF设置为返回类型,但需要使用通过查询字符串传入的类型

自动消息类型在配置中关闭:

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" />

因为您需要以某种方式指定默认响应格式。你看到的行为是标准的。您需要查看特定的标题并确定响应格式。然后,您可以实现一个请求拦截器,以确定传出响应格式需要是什么

因此,要回答您的问题,如果未提及任何格式类型,您需要坚持使用默认格式类型,或者返回错误的请求。与SOAP不同,REST更像是一种范例,而不是一种协议

更新:为了澄清起见。你可以这样做

[ServiceContract()]
public interface ICustomerService
{
   [OperationContract]
   [WebGet(UriTemplate ="?format={formatType}",BodyStyle = WebMessageBodyStyle.Bare)]
   IResponseMessage GetEmCustomers(string formatType);

}

public class CustomerService : ServiceBase
{
  IResponseMessage GetEmCustomers(string formatType)
  {
    try
    {
      var isValid  = base.validateformatspecification(formatType);// check to see if format is valid and supported
      if(!isValid) return base.RespondWithError(formatType,new Error {Msg= "Dude we dont support this format"});
      var customers = CustomerProvider.GetAll();
      return base.Respond(formatType,customer);// This method will format the customers in desired format,set correct status codes and respond.
    }catch(Exception ex)
    {
      // log stuff and do whatever you want
      return base.RespondWithError(formatType,new Error{Msg = "Aaah man! Sorry something blew up"});// This method will format the customers in desired format,set correct status codes and respond.

    }      
  }    
}

public class ServiceBase
{
  public IResponseMessage Respond<T>(string format,T entity);
  public IResponseMessage RespondWithError<T>(string format, T errorObject);

}

public class Error:IResponseMessage {/*Implementation*/}

public class GetEmCustomerResponseResource:List<Customer>,IResponseMessage {/* Implementation*/}

public class GetEmCustomerErrorResponse: IResponseMessage {/* Implementation   */}

我不确定我是否明白你的意思。格式类型是方法的一个参数。成功的调用将按调用中指定的类型正确返回。只有错误消息没有以指定的输出格式发送。我需要在请求头中查找什么?因此WCF在抛出错误时会忽略集合输出类型。真烦人。我希望这是一个简单的配置更改或是我忽略了的东西。谢谢你的帮助。