ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed.(Resolved)
May 15, 2010 at 12:21 pm 1 comment
ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed.(Resolved)
Last week I face this error while accessing the WebGet method of a restful service. Thanks to Pedrams post for providing the solution.
So here it is.
Solution
The error shows up because without creating a new context, proxy ends up using the POST verb which will eventually cause the above exception
To resolve it we need to create a new OperationContextScope everytime we make a call to a WebGet method.
Sample code:
[ServiceContract]
public interface ITwitterStatus
{
[OperationContract]
[WebGet(UriTemplate = "/statuses/friends.xml")]
Message GetFriends();
}
public partial class _Client
{
protected string GetFriends()
{
WebHttpBinding binding = new WebHttpBinding(WebHttpSecurityMode.TransportCredentialOnly);
binding.MaxReceivedMessageSize = 99999999;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Transport.Realm = “Twitter API”;
using (WebChannelFactory<ITwitterStatus> cf = new WebChannelFactory<ITwitterStatus>(binding, new Uri(“http://www.twitter.com”)))
{
cf.Credentials.UserName.UserName = “username”;
cf.Credentials.UserName.Password = “password”;
ITwitterStatus proxy = cf.CreateChannel();
using((IDisposable)proxy)
using (new OperationContextScope((IContextChannel)proxy))
{
Message m = proxy.GetFriends();
return(XDocument.Load(m.GetReaderAtBodyContents()).ToString());
}
}
}
Entry filed under: Silverlight. Tags: calling rest WebGet method, consuming WCF Restful service, Silverlight, The remote server returned an unexpected response: (405) Method Not Allowed, WebChannelFactory.
1.
Tony | June 17, 2011 at 5:06 am
Thank you, it helped me solve a similar problem