使用 WCF POST 方法未找到方法
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
实施了
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 我得到错误 "Method Not Found"
如果我在 Fiddler 中执行相同的 POST 方法和 运行 它,那么它会成功保存到数据库中。
然后我使用这个 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 作为指南,但我认为我使用了尽可能多的信息来完成我认为我需要的东西(我可能是错的)。
我应该做些不同的事情吗?
Using the above configuration if i navigate to
http://localhost:12345/Service1.svc/InsertData/name/surname i get the
error "Method Not Found"
当您通过浏览器执行此操作时,您实际上是在执行 GET 请求,您的方法不支持该请求。
当您通过 Fiddler 执行此操作时,您会发出 POST.
您可以通过代码使用 WebRequest class or HttpRequest 执行 POST。
<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 我得到错误 "Method Not Found"
如果我在 Fiddler 中执行相同的 POST 方法和 运行 它,那么它会成功保存到数据库中。
然后我使用这个 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 作为指南,但我认为我使用了尽可能多的信息来完成我认为我需要的东西(我可能是错的)。
我应该做些不同的事情吗?
Using the above configuration if i navigate to http://localhost:12345/Service1.svc/InsertData/name/surname i get the error "Method Not Found"
当您通过浏览器执行此操作时,您实际上是在执行 GET 请求,您的方法不支持该请求。
当您通过 Fiddler 执行此操作时,您会发出 POST.
您可以通过代码使用 WebRequest class or HttpRequest 执行 POST。