我有一个 WCF REST 服务,非常简单,如下所示:
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebInvoke(
Method = "GET",
UriTemplate = "getNumber")]
int GetSomeNumber();
}
public class TestService : TestServiceApp.ITestService
{
public int GetSomeNumber()
{
return 5;
}
}
它是这样配置的:
<services>
<service name="TestServiceApp.TestService">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="restBehaviour" contract="TestServiceApp.ITestService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
使用指定的 Uri 模板从浏览器调用它可以正常工作:
http://localhost:52602/TestService.svc/getnumber.
我这样配置我的客户端:
<client>
<endpoint name="TstSvc" address="http://localhost:52602/TestService.svc" binding="webHttpBinding" contract="TstSvc.ITestService" behaviorConfiguration="restBehaviour"/>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="restBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
当我使用以下代码调用服务时:
using (WebChannelFactory<ITestService> factory = new WebChannelFactory<ITestService>("TstSvc"))
{
var svc = factory.CreateChannel();
svc.GetSomeNumber();
}
我得到错误:
“没有
http://localhost:52602/TestService.svc/GetSomeNumber
可以接受消息的端点监听。”
我怀疑由于某种原因,我使用 GetSomeNumber 方法的调用未正确映射到 uri /getnumber。为什么?我该如何解决这个问题,我错过了什么?