对不起,这有点长,但我需要所有的信息:
即:
<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."
}
}
所以我的问题是:
答案 0 :(得分:0)
我认为这是您正在寻找的链接http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx
您需要编写自定义格式化程序,并且不包括对其中的类型的处理。
答案 1 :(得分:0)
您需要指示服务实际返回的内容。有几种方法:
ResponseFormat=WebMessageFormat.Json
属性中设置WebGet
defaultOutgoingResponseFormat="Json"
设置中指定webhttp