2

抱歉,这有点长,但我相信所有信息都是必需的:

  • .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,然后实现了以下ErrorResponse以将 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?
4

2 回答 2

-1

您需要指示服务实际上必须返回什么。有几种方法:

  • ResponseFormat=WebMessageFormat.Json在您WebGet的操作合同的属性中设置
  • defaultOutgoingResponseFormat="Json"webhttpweb.config 中的行为下设置中指定
于 2013-08-05T21:44:32.173 回答
-1

我认为这是您正在寻找的链接http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx

您需要编写自定义格式化程序,并且不包括其中的类型处理。

于 2013-07-30T01:27:45.890 回答