Home > English > Silverlight – Specified method is not supported on this request.

Silverlight – Specified method is not supported on this request.

Recently, I have been spending a lot of time working with the WCF Web API that Glenn Block and team are working on. I really love what they have done and I found a blog post by Joe McBride that provided exactly what I needed to use it in Silverlight as well.

As I started testing a service I was creating, I was able to perform both GET and POST operations without a problem but as soon as I tried to use either PUT or DELETE, I ran into the the following exception:

Specified method is not supported on this request.

When I took a look at the stack trace, here is what it showed me:

System.NotSupportedException was unhandled by user code
Message=Specified method is not supported on this request.
StackTrace:
at System.Net.Browser.BrowserHttpWebRequest.set_Method(String value)
at HttpContrib.Http.SimpleHttpClient.PostContent(HttpRequestMessage request, TaskCompletionSource`1 tcs)
at HttpContrib.Http.SimpleHttpClient.c__DisplayClass3.b__2(Object w)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
InnerException:

This was extremely frustrating and I tried very hard to find an answer to what was going on. You see, I had another project that I had written previously that was working perfectly well and I couldn’t find any differences in the code that I was executing.

It turns out that Silverlight provides HTTP handling in both the Browser and Client stack. By default Silverlight uses the Browser for HTTP handling. The only problem with this is that Browser HTTP Handling only supports GET and POST request methods. Once I found this out, I simply looked in my previous application and this is what I found in my App.xaml.cs file:

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

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

Once I put this in place, I was able to use all the HTTP verbs that I wanted. You may want to read up a bit on “How to: Specify Browser or Client HTTP Handling” before arbitrarily changing your code but it is nice to know what is required when you run into this exception.

Hope this helps…

Advertisements
Categories: English Tags: , , ,
  1. Mark
    December 12, 2011 at 7:29 pm

    Good to know, thanks Matt.

    Mark

  1. December 12, 2011 at 1:53 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: