我想完全瞭解如何在WCF中添加接口影響方法的URI。我有這樣定義的的ServiceContract:如何將接口添加到WCF ServiceContract更改方法的URL
[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」的實施,同時消除了裝飾器是現在已經在界面中。由於上面的網址不再有效,因此我在這裏遇到了麻煩。
我試着更新web.confg這個(注更改名稱和合同屬性):
<services>
<service name="IDataService.DataService"
behaviorConfiguration="serviceBehavior">
<endpoint address=""
behaviorConfiguration="epBehavior"
binding="webHttpBinding"
contract="IDataService.DataService" />
</service>
</services>
這似乎讓服務再會,但我不能真正得到的方法,如果我能加入serviceMetadata不能再獲取元數據(它可以在原始版本中)。我已經嘗試了各種各樣的web.config和URL的組合,但似乎無法繞過它。如何正確連接新界面?
UPDATE
感謝venerik我得到它的工作,但改變端點的接口點,但離職的是:
<services>
<service name="DataService"
behaviorConfiguration="serviceBehavior">
<endpoint address=""
behaviorConfiguration="epBehavior"
binding="webHttpBinding"
contract="IDataService.DataService" />
</service>
</services>
謝謝!你真的死了。我的錯誤是我改變了服務聲明中的attr屬性,並指出了界面,這是錯誤的 - 它應該一直指向類本身。當類定義實現(服務本身)時,接口定義了信號(即契約),這是有道理的。 – Ernie