WCF REST服务客户端调用映射到错误的Uris

时间:2022-11-25 19:38:06

I have a WCF REST service, very simple, like this:

我有一个WCF REST服务,非常简单,像这样:

[ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        [WebInvoke(
           Method = "GET",
           UriTemplate = "getNumber")]
         int GetSomeNumber();
    }

public class TestService : TestServiceApp.ITestService
    {
        public int GetSomeNumber()
        {
            return 5;
        }
    }

It is configured like this:

它配置如下:

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

It works fine called from the browser using the specified Uri template:

它使用指定的Uri模板从浏览器调用很好:

http://localhost:52602/TestService.svc/getnumber.

I configured my client like this:

我像这样配置我的客户端:

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

When I'm calling the service with following code:

当我使用以下代码调用服务时:

    using (WebChannelFactory<ITestService> factory = new WebChannelFactory<ITestService>("TstSvc"))
    {
        var svc = factory.CreateChannel(); 
        svc.GetSomeNumber();
    }

I get error:

我收到错误:

"There was no endpoint listening at http://localhost:52602/TestService.svc/GetSomeNumber that could accept the message."

“在http:// localhost:52602 / TestService.svc / GetSomeNumber上没有可以接受该消息的端点。”

I suspect that for some reason my calls using GetSomeNumber method aren't correctly mapped to the uri /getnumber. Why? How can I fix this, what did I miss?

我怀疑由于某种原因,使用GetSomeNumber方法的调用未正确映射到uri / getnumber。为什么?我怎么能解决这个问题,我错过了什么?

1 个解决方案

#1


0  

The only issue I could think of is that your ITestService contract is not up to date in the WebChannelFactory client code.

我能想到的唯一问题是您的ITestService合约在WebChannelFactory客户端代码中不是最新的。

I did notice in my testing that where you use WebInvoke matters. WebInvoke should always be placed on your ServiceContract (interface) so that WebChannelFactory has the proper URIs when communicating via REST. If you place WebInvoke on the concrete class (implementation) then WebChannelFactory defaults to the OperationContract name as you observed above.

我在测试中注意到你使用WebInvoke的地方很重要。应始终将WebInvoke放在ServiceContract(接口)上,以便在通过REST进行通信时,WebChannelFactory具有正确的URI。如果将WebInvoke放在具体类(实现)上,则WebChannelFactory默认为您在上面观察到的OperationContract名称。

#1


0  

The only issue I could think of is that your ITestService contract is not up to date in the WebChannelFactory client code.

我能想到的唯一问题是您的ITestService合约在WebChannelFactory客户端代码中不是最新的。

I did notice in my testing that where you use WebInvoke matters. WebInvoke should always be placed on your ServiceContract (interface) so that WebChannelFactory has the proper URIs when communicating via REST. If you place WebInvoke on the concrete class (implementation) then WebChannelFactory defaults to the OperationContract name as you observed above.

我在测试中注意到你使用WebInvoke的地方很重要。应始终将WebInvoke放在ServiceContract(接口)上,以便在通过REST进行通信时,WebChannelFactory具有正确的URI。如果将WebInvoke放在具体类(实现)上,则WebChannelFactory默认为您在上面观察到的OperationContract名称。