Search code examples
c#.netsilverlightodatasimple.odata

Simple.OData.Client fails with Silverlight 5


The following little piece of code works great in a console application, but in a Silverlight 5 application (where I need it!) it fails by throwing a NotSupportedException:

    var client = new ODataClient("http://MYSERVER:9000/OData_v4/ProductionDb/");

    try
    {
        //This statement throws in Silverlight 5 but not in a .NET 4.5 Console application!!??
        var Meter = await client
            .For("MyEntityName")
            .Top(1)
            .FindEntryAsync();

        foreach (var entry in Meter)
            Debug.WriteLine(string.Format("{0}: {1}", entry.Key, entry.Value));
    }
    catch (NotSupportedException ex)
    {
        Debug.WriteLine(string.Format( "Exception {0}: {1} ", ex.GetType().ToString(), ex.Message ));
    }

Why doesn't it work in Silverlight? According to the documentation it should work with Silverlight right out of the box....?

I used NuGet to install Simple.OData.Client vers. 4.13.0 (=latest stable) into my Visual Studio 2015 Silverlight project.


Solution

  • Add the following code to your MainPage constructor, right after InitializeComponent():

    HttpWebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
    HttpWebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
    

    This should fix the problem (at least if I was able to run your code with these changes). Quite frustrating but has nothing to do with OData library. You can read more about the issue here: https://mattduffield.wordpress.com/2011/12/11/silverlight-specified-method-is-not-supported-on-this-request/