Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 3 WCF Service `CommunicationException` Server returned error: NotFound

Tags:

I have a Silverlight 3 application, which 95% of the time is successfully requesting data from a WCF Service (in the same webapp) and displaying it.

This happens infrequently, usually if I hit the service a bunch of times quickly, but sometimes it'll happen on a single lone request.

Every once in a while, if I request a lot of transactions in a short period, I get one of two exceptions, they both occure in the Reference.cs file in the EndMyMethod(System.IAsyncResult result).

There are a few methods, and the exceptions occure on any number of them. The first one, is a TimeoutException() which I understand and it makes sense, the second one, which I totally don't get is the "CommunicationException() was unhandled by user code: The remote server returned an error: NotFound."

I've put try..catch blocks both arround the .MyMethodAsync() and in the handler for MyMethodCompleted both to no avail, as the exception occurs in the generated Reference.cs file.

Any help is greatly appreciated.

update

Reference.cs -- generated by "Add Service Reference"

public System.IAsyncResult BeginTogglePicked(string ID, string toggle, System.AsyncCallback callback, object asyncState) 
{
   object[] _args = new object[2];
   _args[0] = ID;
   _args[1] = toggle;
   System.IAsyncResult _result = base.BeginInvoke("TogglePicked", _args, callback, asyncState);
   return _result;
}

public void EndTogglePicked(System.IAsyncResult result) 
{
   object[] _args = new object[0];
   // This is the line where the Exception is Thrown
   base.EndInvoke("TogglePicked", _args, result);
}

Calling Code -- pickedIDs is a list of Strings, and userIDSelecting is a string defined at the top of the procedure. The Event Handler mdc_TogglePIckedCompleted is empty at the moment.

MapDataClient mdc = new MyDataClient();
mdc.TogglePickedCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(mdc_TogglePickedCompleted);

foreach (string id in pickedIDs)
{
    mdc.TogglePickedAsync(id, userIDSelecting, mdc);
}

Update 2

This is the "InnerException" from the CommunicationException: System.Net.WebException: The remote server returned an error: NotFound.

Not sure if this is any more helpful, since it doesn't give any extra details. As I said, this happens intermitently not every time I call a service method. I'd also like to point out that the same call will work sometimes and not others, I'm starting to think this issue is because IIS is failing to respond to my service calls, thoughts?

Update 3

When I mean intermiently, I mean truel intrmitent. This may only occur a single time in a user's session, and it may only occur on one of fifty sessions. Its not an all-or-nothing sitation. The calling application is hosted within the same "webite" as the WCF Service, so I don't think a clintaccesspolicy.xml is the issue, but I could be wrong.

like image 251
Nate Avatar asked Dec 01 '09 20:12

Nate


2 Answers

The message that you are getting are probably a red herring :-(

When internal WCF service exceptions are thrown, these will ALWAYS manifest themselves as Server Not Found exceptions in the Silverlight UI. This is because the HTTP response is of type 500. The best article I read on this was from David Betz - http://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2 (this was written for SL2, but the concepts still holds for SL3. Also, some of his approaches are for purists - e.g. "NEVER" using the Add Service Reference features from VS - you don't have to follow all his advice ;-) )

Anyway, back to your question, you need to convert the response type to 200 and parse the exception in the message. This can be done using a MessageInspector (in the service and SL app).

  • There are quite a few articles on how to do this on the net: http://www.liquidjelly.co.uk/supersearch/?q=silverlight%20messageinspector&lang=en-GB.
  • A working example can be downloaded from CodePlex: http://code.msdn.microsoft.com/silverlightws/Release/ProjectReleases.aspx?ReleaseId=1660 (download link at the bottom of the page "Message Inspectors")

Some of these approaches can seem quite daunting - take some time to understand this - the concept is crucial for WCF <--> SL applications, and it makes sense once you get it :-)

We've used this with a lot of success since the start of the year, so if you need anymore help with this just let me know.

like image 191
Mark Cooper Avatar answered Oct 26 '22 23:10

Mark Cooper


Can I recommend always, always having Fiddler running when you are working with Silverlight and WCF?

like image 39
Intrigue Avatar answered Oct 27 '22 00:10

Intrigue