2013-05-27 35 views
2

對不起,這是一個有點長,但所需要的所有信息,我beleive:WCF抑制返回__type

  • .NET 4.5,內置在JSON.NET
  • 我有以下行爲的定義:

即:

<behaviors> 
    <endpointBehaviors> 
     <behavior name="WebBehavior"> 
      <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" /> 
     </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
     <behavior name="ServiceBehaviour"> 
      <serviceMetadata httpGetEnabled="true" httpGetBinding="" httpGetBindingConfiguration="" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
    </serviceBehaviors> 
</behaviors> 

爲我服務:

[ServiceContract] 
    public interface IWebServiceSrms 
    { 
     //-- Countries -------------------------------------------------------- 
     [OperationContract] 
     [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/Countries")] 
     [return: MessageParameter(Name = "Countries")] 
     List<Country> Countries(); 

     [OperationContract] 
     [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/Countries/{aId}")] 
     [return: MessageParameter(Name = "Country")] 
     Country Country(string aId); 
     //-- /Countries ------------------------------------------------------- 
    } 

我已經實現了IErrorHandler然後將下面的錯誤響應以JSON響應返回給客戶端:

[DataContract(IsReference = false)] 
public class ErrorResponse 
{ 
    public ErrorResponse(ErrorTypes aErrorType, ErrorNumber aErrorNumber, string aMessage) 
    { 
     ApiErrorBase = new ApiErrorBase {ErrorNumber = aErrorNumber, ErrorType = aErrorType, Message = aMessage}; 
    } 
    public ErrorResponse(IApiErrorBase aApiErrorBase) 
    { 
     ApiErrorBase = aApiErrorBase as ApiErrorBase; 
    } 
    [DataMember(Name = "ApiError")] 
    public ApiErrorBase ApiErrorBase { get; set; } 

IApiErrorBase被定義爲:

public interface IApiErrorBase 
{ 
    ErrorNumber ErrorNumber { get; set; } 
    ErrorTypes ErrorType { get; set; } 
    string Message { get; set; } 
} 

和混凝土ApiErrorNase定義爲:

[DataContract(IsReference = false)] 
public class ApiErrorBase : IApiErrorBase 
{ 
    [DataMember(Name = "ErrorType")] 
    private string ErrType 
    { 
     get { return ErrorType.ToString(); } 
     set { return; } 
    } 
    public ApiErrorBase() 
    { 
     ErrorNumber = ErrorNumber.ErrUnknown; 
     ErrorType = ErrorTypes.UnknownError; 
     Message = ""; 
    } 
    [DataMember] 
    public ErrorNumber ErrorNumber { get; set; } 
    [IgnoreDataMember] 
    public ErrorTypes ErrorType { get; set; } 
    [DataMember] 
    public string Message { get; set; } 
} 

然後我有一個助手方法拋出,最終返回錯誤給客戶一個例外:

public static class ErrorHelper 
{ 
    public static void ReturnErrorToClient<T>(ErrorNumber aErrorNumber, ErrorTypes aErrorType, string aMessage, HttpStatusCode aHttpStatusCode = HttpStatusCode.BadRequest) where T : IApiErrorBase, new() 
    { 
     T apiErrorBase = new T {ErrorNumber = aErrorNumber, ErrorType = aErrorType, Message = aMessage}; 

     ErrorResponse errorResponse = new ErrorResponse(apiErrorBase); 

     throw new WebFaultException<ErrorResponse>(errorResponse, aHttpStatusCode); 
    } 
} 

這一切運作良好,並預期,直到我創建了以下ApiErrorAnotherOne子類:

[DataContract(IsReference = false)] 
    [KnownType(typeof(ApiErrorAnotherOne))] 
    public class ApiErrorAnotherOne : ApiErrorBase 
    { 
     [DataMember] 
     public string Homer { get; set; } 

     public ApiErrorAnotherOne() 
     { 
      Homer = "You don't make friends with salad."; 
     } 
    } 

第一個問題是我不得不添加[KnownType(typeof運算(ApiErrorAnotherOne))我收到以下錯誤:

aError = {"Type 'WebService_SRMS.Errors.ApiErrorSomeOtherOne' with data contract name 'ApiErrorSomeOtherOne:http://schemas.datacontract.org/2004/07/WebService_SRMS.Errors' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of kn... 

但是現在我的JSON響應包括如下__type

{ 
    "ApiError": { 
     "__type": "ApiErrorDono:#WebService_SRMS.Errors", 
     "ErrorNumber": 1, 
     "ErrorType": "UnknownError", 
     "Message": "", 
     "Homer": "You don't make friends with salad." 
    } 
} 

所以我的問題是:

  1. 爲什麼在__type出現響應? (響應的其餘部分是我所想要的)
  2. 如何從JSON響應刪除__type?
+0

任何人有什麼見解? – TheEdge

+0

請參閱 中的答案 http://stackoverflow.com/questions/3730297/disable-type-hinting-in-wcf-json-services – kcnygaard

+0

我建議你使用[Json.Net](http:// json。而不是codeplex.com/)。除了更好的性能和更多的功能支持外,您還可以使用動態指令來解析JSON字符串。我相信你的問題不會出現,如果你使用Json.Net。 – Farzan

回答

0

你需要指導服務究竟它必須返回。有幾種方法:

  • 設置ResponseFormat=WebMessageFormat.Json在你的工作你WebGet屬性合約
  • 指定defaultOutgoingResponseFormat="Json"webhttp設置下的行爲在你的web.config
+0

我試着設置ResponseFormat,並且我仍然在結果 –

+0

中得到__Type在這裏相同。 @ScottBeeson你可以修復它嗎? – CJLopez

+0

對不起,太久了,我不記得:( –