返回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