WCF禁止在JSON响应中返回__type [英] WCF suppress returning __type in JSON response
问题描述
抱歉,这有点长,但是我相信所有信息都是必需的:
Sorry this is a little long, but all the information is needed I beleive:
-
具有内置JSON.NET的
- .NET 4.5
- 我定义了以下行为:
- .NET 4.5 with built in JSON.NET
- I have the following behaviours defined:
Viz:
<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响应返回给客户端:
I have implemented an IErrorHandler and then the following ErrorResponse to return JSON responses to clients:
[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; }
}
然后我有一个辅助方法来引发异常,该异常最终将错误返回给客户端:
I then have a helper method to throw an exception which ultimately returns the error to the client:
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 后代类之前,这一切都按预期进行,并且效果良好:
This all works well and as expected until I created the following ApiErrorAnotherOne descendant class:
[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))] :
The first issue was I had to add [KnownType(typeof(ApiErrorAnotherOne))] as I received the following error:
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 ,如下所示:
However now my JSON response includes __type as below:
{
"ApiError": {
"__type": "ApiErrorDono:#WebService_SRMS.Errors",
"ErrorNumber": 1,
"ErrorType": "UnknownError",
"Message": "",
"Homer": "You don't make friends with salad."
}
}
所以我的问题是:
- 为什么__type出现在响应中? (其余的响应是我想要的)
- 如何从JSON响应中删除__type?
推荐答案
您需要指示服务实际返回的内容.有几种方法:
You need to instruct the service what actually it has to return. There are several ways:
-
在操作合同的
- 设置
ResponseFormat=WebMessageFormat.Json
在Web.config中的行为下,在 - 指定
defaultOutgoingResponseFormat="Json"
WebGet
属性中webhttp
设置中- setting
ResponseFormat=WebMessageFormat.Json
in yourWebGet
attribute of your operation contracts - specifying
defaultOutgoingResponseFormat="Json"
inwebhttp
setting under behaviours in your web.config
这篇关于WCF禁止在JSON响应中返回__type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!