Lorenzo B
Lorenzo B

Reputation: 33428

Monotouch: WCF services and Exception handling

I'm using a WCF service created with Visual Studio.

I'm doing a call such as GetDataAsync(param) for retrieve data. In the GetDataCompleted handler, I'm using the retrieved data.

The service works. Sometimes I can't retrieve data. In this case, an exception occurred like the following:

Exception in async operation: System.Net.ProtocolViolationException: The number of bytes to be written is greater than the specified ContentLength.
  at System.Net.WebConnectionStream.CheckWriteOverflow (Int64 contentLength, Int64 totalWritten, Int64 size) [0x00038] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnectionStream.cs:546 

How is it possible to catch a similar excpetion? The application still working but the exception is printed at console. I think the exception cames from Channel or something else.

Thank you in advance.

Upvotes: 6

Views: 822

Answers (3)

Dale
Dale

Reputation: 13024

Unfortunately these WCF exceptions can't be caught in Monotouch at present. This seems to be a known bug. See MonoTouch - WCF Services made by Silverlight tool - Can't catch exceptions

Upvotes: 2

kroonwijk
kroonwijk

Reputation: 8400

Your question is:

How is it possible to catch a similar (red: ProtocolViolationException) exception?

In your service application, catch the ProtocolViolationException with the following code:

catch (ProtocolViolationException ex)
{
  // do something with your exception here
  // for example, throw a FaultException that will be communicated to the client
  throw new FaultException<ProtocolViolationException> 
        (ex, new FaultReason(ex.Message), new FaultCode("Sender")); 
}

For this to be send back to the client correctly, you'll need to set up an additional attribute on the operation contract, like:

[OperationContract()] 
[FaultContract(typeof(ProtocolViolationException))] 

And then, on the client side you can anticipate this specific exception and handle it gracefully, like:

catch (FaultException<ProtocolViolationException> ex) 
{ 
    Console.WriteLine("FaultException<>: " + ex.Detail.GetType().Name + " - " + ex.Detail.Message); 
} 

Does that answer your question?

Upvotes: 2

merbla
merbla

Reputation: 547

When using WCF its always good to try to get as much visibility as possible. There are two tools that I often use. They are the WCF Trace Viewer and WCF Config Editor

  • SvcConfigEditor.exe
  • SvcTraceViewer.exe

Depending if you are on a x64 or x84 machine and the version of .Net they should be located either.

  • C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\
  • C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\
  • C:\Program Files (x86)\Microsoft SDKs\Windows\v6.0A\Bin
  • C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin Check out

Check out http://merbla.blogspot.com/2009/02/wcf-tools.html

Upvotes: 0

Related Questions