申し訳ありませんが、これは少し長くなりますが、すべての情報が必要です。
- JSON.NET が組み込まれた .NET 4.5
- 次の動作が定義されています。
ビズ:
<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."
}
}
だから私の質問は:
- 応答に __type が表示されるのはなぜですか? (残りの応答は私が望むとおりです)
- JSON 応答から __type を削除するにはどうすればよいですか?