WebInvoke POST “Method not allowed” in Wcf Rest

5,424 views
Skip to first unread message

purna pradeep

unread,
Sep 3, 2013, 2:17:14 AM9/3/13
to rest...@googlegroups.com

my Rest wcf service as below...

[ServiceContract]
public interface IWinPhoneService
{

    [OperationContract]
    [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "getkpimeasuredata", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped)]
    List<MeasureData> GetKpiMeasureData(DomainData data);


    [OperationContract]
    [WebGet(UriTemplate = "getdata/{value}", ResponseFormat = WebMessageFormat.Json)]
    string GetData(string value);

   
    // TODO: Add your service operations here
}

[DataContract]
public class DomainData
{
    [DataMember]
    public int? KPIId
    {
        get;
        set ;
    }

    [DataMember]
    public int? ScorecardId
    {
        get;
        set;
    }

    [DataMember]
    public short? CumulativeMonth
    {
        get;
        set;
    }

    [DataMember]
    public int? EngineeringOrgId
    {
        get;
        set;
    }
   
    [DataMember]
    public int? BusinessOrgId
    {
        get;
        set;
    }
    [DataMember]
    public int? DataValuetypeId
    {
        get;
        set;
    }

}

and when I consume this service using Restsharp as below

    string URL = "http://<servername>:8085/WinPhoneService.svc";
      
                RestClient client = new RestClient(URL);
   
                RestRequest request = new RestRequest("getkpimeasuredata",Method.POST);
                DomainData data = new DomainData();
                data.KPIId = 1006;
                data.ScorecardId = 3;
                data.EngineeringOrgId = 11;
                data.DataValuetypeId = 1;
                data.CumulativeMonth = 463;
                data.BusinessOrgId = 1;
   
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
   
                json = "{\"data\" : " + json + "}";
              
                request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
                request.RequestFormat = DataFormat.Json;
     client.ExecuteAsync(request, response =>
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                       
   
                       
                    }
                    else
                    {
                        //NOK
                    }
                });

Even tried a webget method also as below..

     RestClient client1 = new RestClient(URL);
                RestRequest request1 = new RestRequest(string.Format("getdata/{0}", 1), Method.GET);
                request1.RequestFormat = DataFormat.Json;
   
                var x = client1.ExecuteAsync(request1, response =>
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
   
                        }
                    });

Im able to the response.StatusCode as NotFound and even when I checked fiddler **the service is not hitting at all** and when I verified the service url
 in composer im getting the error as
"**HTTP/1.1 405 Method Not Allowed**"

and even when I tried with Webclient as below

    string URL = "http://<servername>:8085/WinPhoneService.svc";
                WebClient wclient = new WebClient();
                wclient.UseDefaultCredentials = true;
                wclient.Headers["Content-Type"] = "application/json";

   
                DomainData kpidata = new DomainData();
                data.KPIId = 1006;
                data.ScorecardId = 3;
                data.EngineeringOrgId = 11;
                data.DataValuetypeId = 1;
                data.CumulativeMonth = 463;
                data.BusinessOrgId = 1;
   
                string json = SerializeJson(kpidata);
                String str = wclient.UploadString(new Uri(URL ),"getkpimeasuredata",json);

Even here im getting **"HTTP/1.1 405 Method Not Allowed"** but atleast in this case its **hitting the service as shown in fiddler**

PFB the webconfig for the same...

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation>
          <assemblies>
            <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          </assemblies>
        </compilation>
        <!--<httpRuntime targetFramework="4.0" />-->
      </system.web>
      <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding name="webCorpBinding">
              <!--<security mode="TransportCredentialOnly">
                <transport clientCredentialType="Windows"></transport>
              </security>-->
            </binding>
          </webHttpBinding>
   
        </bindings>
        <behaviors>
   
          <endpointBehaviors>
            <behavior name="RESTCorpBehavior">
              <webHttp/>
            </behavior>
   
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true"  />
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service name="WinPhoneService.WinPhoneService">
            <endpoint name="WinPhoneServiceeCorp" address="" binding="webHttpBinding" bindingConfiguration="webCorpBinding" behaviorConfiguration="RESTCorpBehavior"
             contract="WinPhoneService.IWinPhoneService" />
          </service>
        </services>
        <!--<protocolMapping>
          <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>-->
        <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />-->
      </system.serviceModel>
      <!--<system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        --><!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          --><!--
        <directoryBrowse enabled="true" />
      </system.webServer>-->
    
   
      <connectionStrings>
        <add name="ScaasEntities" connectionString="metadata=res://*/Scaas.csdl|res://*/Scaas.ssdl|res://*/Scaas.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=<servername>;initial catalog=<catalog>;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      </entityFramework>
    </configuration>

Please help me regarding the same..

João Cardoso

unread,
Sep 9, 2013, 8:57:24 AM9/9/13
to rest...@googlegroups.com
one thing that pops out instantly. you have this entry in web.config:

 <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
but the service is not implemented to use the async pattern.
try changing it to async pattern and try to invoke the service operations again.
if i'm not mistaken the same problem happened to me when i was developing a wcf service a little while ago. 

João Cardoso

unread,
Sep 17, 2013, 11:20:02 AM9/17/13
to rest...@googlegroups.com
purna pradeep check the attachment.
i've picked your implementation and corrected it.
and it's working. i've tested it with fiddler and a client app.

Terça-feira, 3 de Setembro de 2013 7:17:14 UTC+1, purna pradeep escreveu:
TestWCFPost.zip
Reply all
Reply to author
Forward
0 new messages