【问题标题】:How adding an Interface to a WCF ServiceContract changes the URL of the methods向 WCF ServiceContract 添加接口如何更改方法的 URL
【发布时间】:2014-02-19 20:38:51
【问题描述】:

我试图完全了解在 WCF 中添加接口如何影响方法的 URI。我有一个这样定义的 ServiceContract:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceContract]
public class DataService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public List<List<string>> ListTestMethod()
    {
        return new List<List<string>> 
        { 
            new List<string> {"0", "Test String 1"},
            new List<string> {"1", "Test String 2"},
            new List<string> {"2", "Test String 3"}
        };

    }

}

在我的 web.config 文件中,我有以下内容:

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="epBehavior" >
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                               multipleSiteBindingsEnabled="true" />
    <services>
      <service name="DataService"
               behaviorConfiguration="serviceBehavior">

        <endpoint address=""
                  behaviorConfiguration="epBehavior" 
                  binding="webHttpBinding"
                  contract="DataService" />

      </service>
    </services>
</system.serviceModel>

当我通过浏览器测试方法时:

http://localhost/DataService.svc/ListTestMethod

我知道这是预期的结果:

[["0","Test String 1"],["1","Test String 2"],["2","Test String 3"]]

所以现在我想在后面的代码中添加一个接口,如下所示:

[ServiceContract]
public interface IDataService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    List<List<string>> ListTestMethod();
}

当然,回到 DataService 类并添加适当的“:IDataService”实现,同时删除现在已经在接口中的装饰器。这是我遇到问题的地方,因为上面的 URL 不再有效。

我尝试将 web.confg 更新为此(注意名称和合同属性的更改):

<services>
  <service name="IDataService.DataService"
           behaviorConfiguration="serviceBehavior"> 

    <endpoint address=""
              behaviorConfiguration="epBehavior" 
              binding="webHttpBinding"
              contract="IDataService.DataService" />

  </service>
</services>

这似乎让服务再次运行,但我实际上无法访问方法,如果我添加启用 serviceMetadata,它就无法再访问元数据(它可以在原始版本中)。我已经尝试了 web.config 和 URL 的各种组合,但似乎无法绕过它。如何正确连接新接口?


更新

感谢 venerik 我让它工作了,但将端点更改为指向接口但保持服务不变:

<services>
  <service name="DataService"
           behaviorConfiguration="serviceBehavior"> 

    <endpoint address=""
              behaviorConfiguration="epBehavior" 
              binding="webHttpBinding"
              contract="IDataService.DataService" />

  </service>
</services>

【问题讨论】:

    标签: c# .net wcf web-services interface


    【解决方案1】:

    我对这个答案并不完全有信心,但我认为你的 web.config 应该指向接口:

    <endpoint
        ...
        contract="IDataService"/>
    

    你的服务宿主文件应该指向具体的实现:

    <% @ServiceHost Language=C# Service="SomeNamespace.DataService" %>
    

    关于@ServiceHost的信息

    【讨论】:

    • 谢谢!你实际上已经死了。我的错误是我将服务声明中的名称 attr 更改为也指向接口,这是错误的 - 它应该一直指向类本身。这是有道理的,接口定义了签名(即合同),而类定义了实现(服务本身)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    相关资源
    最近更新 更多