Search code examples
c#vb.netwcfwcf-binding

Method Not Found using WCF POST Method


<ServiceContract()>
Public Interface IService1
    <OperationContract()>
    <WebInvoke(Method:="POST", UriTemplate:="InsertData/{name}/{Surname}")>
    Function InsertData(ByVal name As String, ByVal surname As String) As String
End Interface

Implemented it

Public Class Service1
    Implements IService1

    Public Function InsertData(name As String, surname As String) As String Implements IService1.InsertData
        ' Save name and surname which does work
    End Function
End Class

My web.config

<system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfParameters.Service1">
        <endpoint address="http://localhost:12345/Service1.svc" behaviorConfiguration="webHttpBinding"
          binding="webHttpBinding" bindingConfiguration="" name="WebHttpw"
          contract="WcfParameters.IService1" />
        <!--<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />-->
      </service>
    </services>
    <client>
      <endpoint address="http://localhost:12345/Service1.svc" binding="webHttpBinding"
        bindingConfiguration="" contract="WcfParameters.IService1" />
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
      <endpointBehaviors>
        <behavior name="webHttpBinding">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <!--<protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>-->    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
  </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>

</configuration>

I am trying to create a service which will accept a few parameters and save the data to my database.

Using the above configuration if i navigate to http://localhost:12345/Service1.svc/InsertData/name/surname i get the error "Method Not Found"

If i do the same POST method and run it in Fiddler then it saves to the database successfully.

I then used this link http://blogs.msdn.com/b/carlosfigueira/archive/2012/03/26/mixing-add-service-reference-and-wcf-web-http-a-k-a-rest-endpoint-does-not-work.aspx as a guide but i think ive used as much info in order to accomplish what i think i need (i could be wrong).

Should i be doing something different?


Solution

  • Using the above configuration if i navigate to http://localhost:12345/Service1.svc/InsertData/name/surname i get the error "Method Not Found"

    When you do this trough your browser you are actually doing a GET request, which your method does not support.

    When you do it trough Fiddler you issue a POST.

    You can do a POST using the WebRequest class or HttpRequest trough your code.