我在C#典型的WCF REST服务,接受JSON输入,并返回一个JSON输出:WCF REST服务:WebInvoke用JSON和多态性不能正常工作
[ServiceContract]
public class WCFService
{
[WebInvoke(Method = "POST", UriTemplate = "register", ResponseFormat = WebMessageFormat.Json)]
public BasicResponse RegisterNewUser(UserDTO newUser)
{
return new BasicResponse()
{ status = "ERR_USER_NAME" };
}
}
public class BasicResponse
{
public string status { get; set; }
}
public class UserDTO
{
public string username { get; set; }
public string authCode { get; set; }
}
可正常工作,但我想在正常执行和出错的情况下返回不同的对象。我创建了一个基础响应类和几个继承者。现在WCF的JSON序列崩溃并产生“400错误的请求”:
[ServiceContract]
public class WCFService
{
[WebInvoke(Method = "POST", UriTemplate = "register",
ResponseFormat = WebMessageFormat.Json)]
public BasicResponse RegisterNewUser(UserDTO newUser)
{
return new ErrorResponse()
{
status = "ERR_USER_NAME",
errorMsg = "Invalid user name."
};
}
}
public class BasicResponse
{
public string status { get; set; }
}
public class ErrorResponse : BasicResponse
{
public string errorMsg { get; set; }
}
public class UserDTO
{
public string username { get; set; }
public string authCode { get; set; }
}
我想没有任何成功应用[KnownType(typeof(ErrorResponse))]
和[ServiceKnownType(typeof(ErrorResponse))]
属性。看起来像DataContractJsonSerializer
中的一个bug说明它支持多态。
我的WCF REST服务使用WebServiceHostFactory:
<%@ ServiceHost Language="C#" Debug="true"
Service="WCFService"
CodeBehind="CryptoCharService.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
在我的web.config我有标准的HTTP端点:
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint helpEnabled="true" defaultOutgoingResponseFormat="Json" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
你觉得这是可以解决的?我知道一个解决方法(手动返回字符串和序列化输出),但为什么这不起作用?
感谢这些建议。我知道通过WCF中的概念,我应该使用[DataContract]和[DataMember],但经过数小时的实验后,我发现DataContractJsonSerializer完全忽略了这些属性,因此我将它们从代码中移除了。你的代码没有帮助。串行器仍然崩溃,结果仍然是“400错误请求”。你知道我怎么看到序列化器抛出的异常? – 2011-12-19 22:35:04