I have written a WCF REST API for third-party use. One of the things I want to do is to return custom error responses to clients if anything goes wrong. I don't want the WCF default error page showing that internal server error has occurred or method name not found.
To do so I throw WebFaultException<Error> where necessary. This return the following type of response to the client:
<Error>
<type>MissingTag</type>
<Desc>Tag 349 is missing</Desc>
</Error>
But how can I handle if any other type of error occurs like a serialization error or the "Method not found" error or place where I want to check that POST, PUT and PATCH have http header content-type present. I want to throw WebFaultException<> there too. I tried looking into IErrorHandler but could not get it working.
Any one got ideas on how to implement this type of thing. Also can I have a simple code demonstrating the IErrorHandler usage?
You can look into Message Inspectors BeforeSendReply for customizing the reply that needs to be sent to the client
If the content-type is not set when the request is made you can look into the AfterReceiveRequest where in you can customise the request received and then action as needed.
Related
When sending a GET request to the API url the following error is shown:
{"message":"The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource."}
and in chrome console:
Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)
How it is possible to solve this? I am not sure what to look for.
I'd leave a comment but I don't have enough reputation yet.
I'm assuming you've made a REST API in C# and are trying to test it?
It seems to me that the backend is expecting a header with Content-Type specified. For example, if you're sending JSON in the body the Content-Type would be JSON.
For testing these kind of things I personally use postman, rather than executing the requests through chrome, since it allows you to set up the HTTP requests more specifically. Other tools would work too though.
The postman docs here cover setting the Content-Type in a HTTP request.
https://learning.postman.com/docs/sending-requests/requests/#configuring-request-headers
There is a Java-Soap-Service which I want to call with WSE 3.0, I generated a Proxy with the WSDL-File but the service expects MTOM for it´s data.
I also followed this tutorial and it worked well but didn´t helped:
http://twit88.com/blog/2008/05/14/net-mtom-enabled-your-application-using-wse/
The Exception I get is:
System.FormatException: "WSE839: An HTTP response was received that used the following content type: text/xml;charset=UTF-8. The following content type was expected: multipart/related; type=application/xop+xml."
I know WSE 3.0 is obsolete if there is an other way to do it please tell.
Can some one please help?
Change your generated classes base class from System.Web.Services.Protocols.SoapHttpClientProtocol to Microsoft.Web.Services3.WebServicesClientProtocol. Once that is done you will be able to access a field called RequireMtom. Set this to true prior to calling any method that needs to send MTOM. Make sure to disable it for non MTOM calls.
I should call a web service in my C# application.
When I add a service reference in Visual Studio to consume the service, in an exception situation I get this message:
An HTTP Content-Type header is required for SOAP messaging and none
was found.
But, when I add a web reference the message is more rich and easy to understand the problem. The final part of this message is a business error message generated by the service. I was provided an invalid authentication token parameter for that service so this error message is returned:
Client found response content type of '', but expected 'text/xml'.
The request failed with the error message:
--
WEB SERVICE ERROR : UNAUTHENTICATED_ACCESS
I was preferred to use "service reference" approach but now I am in doubt.
Is there a way to improve this kind of exceptions or to log raw response using "service reference"?
I think the first message is much clearer.
It's telling you exactly what the problem is, which is the HTTP Content-Type header is missing from the service response. While this header is not mandatory, the HTTP specs say you should use it (from here):
Any HTTP/1.1 message containing an entity-body SHOULD include a
Content-Type header field defining the media type of that body.
Microsoft obviously built into their proxy generation tooling the assumption that this header will always be present.
And no, you should not use WebReference. It's from .net 1.1
I have a WCF service that handles errors by implementing an IErrorHandler and attaching it to the ChannelDispatcher's ErrorHandlers.
The IErrorHandler has a method called
bool HandleError(Exception error);
Which I implement and simply logs the exception to my DB.
My problem is that some exceptions occur before the web service's method gets called.
I also have a MessageInspector that I attach to the EndpointDispatcher that logs the soap messages.
Now, assuming I have an sql error and get an unhandled exception on my MessageInspector, the HandleError method gets called twice with the same sql connection error exception.
The problem is that it happens once when the connection's status is opened and once when the connection's status is closed.
Also, the second method on the IErrorHandler:
void ProvideFault(Exception error, MessageVersion version, ref Message fault);
doesn't get called, and then the framework wraps my exception with a FaultException, without letting me format it.
Is there any reason why the IErrorHandler behaves that way? Is there any other module I need to implement in order to handle unhandled exceptions that occur as part of the request's pipeline?
Why does the IErrorHandler get called when the connection is Closed?
We had a similar implementation of MessageInspector and wanted to know how long each request took. Consequently we did logging for both the incoming and outgoing messages. Perhaps this could be the case for your issue?
I'm not sure that the ProvideFault method is called when inside an extension behavior, however I was able to get it to fire from within the constructor of my restful WCF service. I think the 'workaround' of putting a try/catch in the inspector is a good approach.
Below are a couple of articles with implementation details on this topic:
IErrorHandler returning wrong message body when HTTP status code is 401 Unauthorized
How can I create custom XML namespace attributes when consuming a legacy SOAP service?
Also here is an article on how to do something similar in WebApi:
http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling
The SOAP service I am consuming will send back a 202 Accepted. All I want to do is capture this 202 so I can confirm that the server received the message.
I have found this:
here
So the answer states to implement a soapextension and links to msdn. I looked at the sample code, and I am having a hard time understanding what I should do. The client that was generated submits the data in the following way:
psrs.SubmitPersonSearchRequest(psrt);
and has a void return. How do I get the response from the server?
I was able to see the correct response in soapui and in fiddler.