English 中文(简体)
我如何用代码确定周转基金错误的信息回报格式?
原标题:How do I set the WCF error message return format in code?
  • 时间:2012-05-07 15:58:06
  •  标签:
  • wcf

我有这样的服务合同:

[WebGet(UriTemplate = "getdata?key={key}&format={format}")]
Event[] GetIncidentsXml(string key, string format);

在编码中,我放弃了反应方式,如:

var selectedFormat = ParseWebMessageFormat(format);
WebOperationContext.Current.OutgoingResponse.Format = selectedFormat;

(ParseWebMessage) 表格是填平Enum parsing for the category

这一部分按预期运作,我根据所通过的参数获得XML或JSON。

当我提出例外时,情况就是如此。 如果通过的关键无效,我就这样做:

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

throw new WebProtocolException(HttpStatusCode.BadRequest, "Invalid Key.", exception, null);

当出现例外情况时,返回类型为always。 XML:

<ServiceResponse xmlns="http://schemas.datacontract.org/2004/07/IBI.ATIS.Web.ServiceExceptions" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <ErrorCode>
        <Code>100</Code>
        <Msg>Invalid Key</Msg>
    </ErrorCode>
    <State>fail</State>
</ServiceResponse>

回归类型的变化是服务方法中第一条代码,在放弃例外之前就是如此。

我知道,我可以根据申请表确定WCF的回报类型,但要求使用通过询问显示的类型。

自动电文类型转换为:

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" />
问题回答

由于你需要具体指明违约反应格式。 你看到的行为是标准的行为。 你们需要寻找和确定具体负责人,并确定应对方式应当是什么。 然后,你可以执行请求拦截者,以说明即将采取的应对方式。

为了回答你的问题,如果无人提及或提出不好的要求,你就需要坚持一种缺省格式。 与《经济、社会、文化权利国际公约》不同的议定书相比,教育、科学和技术部更是一个范例。

UPDATE: For clarification sake. You could do something like this.

[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 DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

Access WCF service on same server

I have a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...but that seems ...

WCF binding error

So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...

The service operation requires a transaction to be flowed

I am facing strange issue with our WCF service. The same code was working fine until recently we added more OperationContracts(Web Methods). We have common 3 tier architecture. DAL (WCF) BLL Web ...

热门标签