【发布时间】: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