使用WCF POST方法找不到的方法

时间:2015-04-24 08:49:10

标签: c# vb.net wcf wcf-binding

<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

实施它

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

我的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>

我正在尝试创建一个接受一些参数并将数据保存到我的数据库的服务。

使用上述配置,如果我导航到http://localhost:12345/Service1.svc/InsertData/name/surname我收到错误&#34;找不到方法&#34;

如果我使用相同的POST方法并在 Fiddler中运行它,那么它会成功保存到数据库。

然后我使用此链接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作为指南,但我认为我已经使用了尽可能多的信息来完成我认为我需要的东西(我可能是错的)。

我应该做些不同的事吗?

1 个答案:

答案 0 :(得分:0)

  

如果我导航到上面使用上面的配置   http://localhost:12345/Service1.svc/InsertData/name/surname我得到了   错误“找不到方法”

当您通过浏览器执行此操作时,您实际上正在执行GET请求,而您的方法不支持该请求。

当你通过Fiddler这样做时,你发出一个POST。

您可以使用WebRequest课程或HttpRequest通过您的代码进行发布。