2

I'm not entirely sure how to explain this, but basically I am trying to use a Silverlight application hosted within an ASP.NET MVC application. I have a basic controller on the MVC side that contains a method which accepts some string parameter and returns an ActionResult (for the purpose of this program, that result is some Json data).

My problem arises when I handle the WebClient.OpenReadCompleted event within the Silverlight control. When the WebClient.OpenReadAsync method is called within this control, it successfully reaches the controller and then reports back to the relevant event handler as expected. However, when it is handled, the event arguments contain an error stating: "The remote server returned an error: NotFound.".

Previously, I have noticed this is caused when some part of my communication URL is incorrect - in this case it is not. From some Googling, I have also noticed that this is a generic error. As such, I'm rather stumped. To make matters more confusing, I use this exact same communication attempt in another part of the program, that retrieves an array of strings, and that works perfectly fine.

Please see the example code below (due to the nature of this program, I am unable to post the full code).

Silverlight Control

WebClient mClient = new WebClient();

public void RequestData()
{
    mClient.OpenReadAsync(new Uri("http://localhost:51234/Home/GetData"));
    mClient.OpenReadCompleted += new OpenReadCompletedEventHandler(mClient_OpenReadCompleted);
}

private void mClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    if(!e.Cancelled && e.Error == null) // <-- e.Error here is a WebException
    {
        var serializer = new DataContractJsonSerializer(typeof(Data));
        Data data = (Data)serializer.ReadObject(e.Result);
    }
}

MVC Controller - named HomeController and accessed with "Home"

public ActionResult GetData()
{
    return Json(new Data(), JsonRequestBehaviour.AllowGet);
}

Note Here, Data contains three members of types; string, string and byte array. For the purpose of serialization, I have exposed all three members through public properties containing both get and set parts. I have also added a public constructor taking no arguments.

Any advice on this would be greatly appreciated.

Many thanks in advance.

UPDATE

I've just tried the same bit of code with different data, and it works fine. I wondered if it was the size of the data (as the first attempt was with very large data), but I don't understand why that would matter if the call managed to hit the controller.

tereško
  • 58,060
  • 25
  • 98
  • 150
Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
  • Yes, another WebException with the same message. – Samuel Slade Jan 11 '11 at 20:07
  • Can you use Fiddler (or similar) to see what the real message is? – Mike Hildner Jan 11 '11 at 22:39
  • I downloaded and had a go with Fiddler, but didn't see anything for this program. I am running this application on the localhost - having not used Fiddler before, I've no idea if it works for this. – Samuel Slade Jan 12 '11 at 17:18
  • It *should* give you better details than "NotFound". However, as a proxy, you need a workaround to see localhost traffic. Please see http://www.fiddlertool.com/fiddler/help/hookup.asp#Q-LocalTraffic – Mike Hildner Jan 12 '11 at 18:41

1 Answers1

1

If you want to see the real server-side exception, this may help you:

http://msdn.microsoft.com/en-us/library/ee844556(v=VS.95).aspx

Either approach described there would probably illuminate the real problem.

Tim
  • 135
  • 6
  • Cheers. I have moved onto another part of the project I'm working on at the moment, so I'm not working with this problem currently. As such, I will leave this post open until I come back to this problem; at which point I will look into the links provided so far. – Samuel Slade Feb 15 '11 at 12:27