Reputation: 937
Im trying to get WebFaultException to be returns as json and xml depending on what the client asks for as described in
My service interface looks like this
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "session_record?id={id}&command={command}")]
void SessionRecord(Guid id, String command);
The exception
throw new WebFaultException<string>("Session not started", HttpStatusCode.Conflict);
Web.config service setup
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" faultExceptionEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
My client call to the service
$.ajax({
url: "Webservice/session_record?id={id}&command={command}".format({ id: $("#sessionGuid").val(), command : "start" }),
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("Started");
}
});
I would except to receive json from the server, but i get xml
And as i read it in the link i have posted i should be possible
With the WebFaultException, the detail of the exception that is serialized in the body of >the response message will always be in the format (XML or JSON) that the client would have >received had there not been an error. If the client was expecting XML, the client will >get the exception detail serialized as XML. Likewise, if the client was expecting JSON, >the client will get the exception detail serialized as JSON.
Upvotes: 3
Views: 4662
Reputation:
minor changes in Web.config service setup should work too: automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="json"
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" faultExceptionEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json" />
</webHttpEndpoint>
</standardEndpoints>
Upvotes: 1
Reputation: 937
I found the solution; faultExceptionEnabled needed to be false. Now I get the exception in json.
Upvotes: 4