2011-01-14 26 views
0

我有一個WCF休息webservice。它工作正常。我想了解端點元素中可用的不同配置值。解釋system.servicemodel爲休息服務的endpoint.address元素

特別是,我想了解地址元素的用途。改變價值似乎並沒有改變我如何處理這項服務。爲此,我正在運行visual studio 2010和cassini的服務。端口號設置爲888.

將地址設置爲空字符串我得到... http://localhost:888/restDataService.svc/hello將返回「hello world」。

地址設置爲「localhost」我得到... http://localhost:888/restDataService.svc/hello將返回「hello world」。

地址設置爲「pox」我得到... http://localhost:888/restDataService.svc/hello將返回「hello world」。

無論我在地址欄中設置了什麼值。它不會影響網址。我唯一的解釋是價值更多的是非REST服務。

<system.serviceModel> 
    <services> 
    <service behaviorConfiguration="MobileService2.DataServiceBehaviour" name="MobileService2.DataService"> 

     <endpoint address="pox" binding="webHttpBinding" contract="MobileService2.IRestDataService" behaviorConfiguration="webHttp"> 
     </endpoint> 

     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
    </services> 
    <behaviors> 
    <endpointBehaviors> 
     <behavior name="webHttp"> 
     <webHttp /> 
     </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
     <behavior name="MobileService2.DataServiceBehaviour" > 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
    </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
</system.serviceModel> 

我也有以下服務合同

[ServiceContract] 
    public interface IRestDataService 
    { 
     [OperationContract] 
     [WebGet(UriTemplate = "hello")] 
     string Hello(); 
    } 

而在.SVC

<%@ ServiceHost Language="C#" Debug="true" 
      Service="MobileService2.RestDataService" 
      Factory="System.ServiceModel.Activation.WebServiceHostFactory" 
      CodeBehind="RestDataService.svc.cs" %> 

和 '代碼隱藏'

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class RestDataService : IRestDataService 
{ 
    public string Hello() 
    { 
     return "hello"; 
    } 
} 

回答

2

你能不能也顯示配置的服務元素?我認爲你的配置沒有被使用,或者你正在訪問應用程序的其他實例(你配置Cassini使用80端口嗎?),因爲你的第二個和第三個測試應該返回HTTP 404資源找不到。

爲您的測試正確的地址是:

  1. http://localhost/restDataService.svc/hello
  2. http://localhost/restDataService.svc/localhost/hello
  3. http://localhost/restDataService.svc/pox/hello

檢查您的服務元素的名稱是完全一樣的服務類型的名稱(包括命名空間)在.svc標記中的ServiceHost指令中使用。

+0

+1絕對正確!我自己測試過這個 - 當配置正確時,這是發生的行爲。 – 2011-01-14 18:10:16