返回XML我正在运行的ServiceHost来测试我的服务之一,直到我扔的FaultException一切工作正常 - 砰我得到XML不是JSONWCF JSON服务故障
我的服务合同 - 可爱
/// <summary>
/// <para>Get category by id</para>
/// </summary>
[OperationContract(AsyncPattern = true)]
[FaultContract(typeof(CategoryNotFound))]
[FaultContract(typeof(UnexpectedExceptionDetail))]
IAsyncResult BeginCategoryById(
CategoryByIdRequest request,
AsyncCallback callback, object state);
CategoryByIdResponse EndCategoryById(IAsyncResult result);
主机设置 - scrummy百胜
var host = new ServiceHost(serviceType, new Uri(serviceUrl));
host.AddServiceEndpoint(
serviceContract,
new WebHttpBinding(), "")
.Behaviors.Add(
new WebHttpBehavior
{
DefaultBodyStyle = WebMessageBodyStyle.Bare,
DefaultOutgoingResponseFormat = WebMessageFormat.Json,
FaultExceptionEnabled = true
});
host.Open();
这里的呼唤 - OO肚子疼
var request = WebRequest.Create(serviceUrl + "/" + serviceName);
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
request.ContentLength = 0;
try
{
// receive response
using (var response = request.GetResponse())
{
var responseStream = response.GetResponseStream();
// convert back into referenced object for verification
var deserialiser = new DataContractJsonSerializer(typeof (TResponseData));
return (TResponseData) deserialiser.ReadObject(responseStream);
}
}
catch (WebException wex)
{
var response = wex.Response;
using (var responseStream = response.GetResponseStream())
{
// convert back into fault
//var deserialiser = new DataContractJsonSerializer(typeof(FaultException<CategoryNotFound>));
//var fex = (FaultException<CategoryNotFound>)deserialiser.ReadObject(responseStream);
var text = new StreamReader(responseStream).ReadToEnd();
var fex = new Exception(text, wex);
Logger.Error(fex);
throw fex;
}
}
文本var包含正确的错误,但序列化为Xml 我在这里做了什么错误?
The难点在于如何为SOAP客户端抛出FaultException,并为REST客户端抛出WebHttpException ......我还没有想到这一点。 – Junto
@Junto,你有没有想过这个?它是否为每个客户提供了一个单独的终端,并为每个终端配置了必要的行为配置? –
@MrMoose我在这里有一个开放的问题:http://stackoverflow.com/questions/7188519/wcf-ierrorhandler-to-return-faultexception-to-soap-and-webhttpexception-to-pox-a。你有没有尝试抛出一个新的WebHttpException(){...};并查看SOAP客户端中会发生什么? –
Junto