2013-02-13 44 views
2

我在WCF新的,並用一個簡單的文件,應用較少一部分(web.config中),你可以看到下面開始我的經驗:WCF:relativeAddress,baseAddress並結合

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"> 
     <serviceActivations> 
      <add 
       factory="System.ServiceModel.Activation.ServiceHostFactory" 
       relativeAddress="./RelativeAddress.svc" 
       service="WCF_Transactions.MyService1"/> 
     </serviceActivations> 
    </serviceHostingEnvironment> 

現在我可以訪問在

http://localhost:18148/RelativeAddress.svc 

服務的話,我想補充下一行:

<services> 
     <service name="WCF_Transactions.MyService1" behaviorConfiguration="MyBehavior1"> 
      <host> 
       <baseAddresses> 
        <add baseAddress="http://localhost:18148/" /> 
       </baseAddresses> 
      </host> 
      <endpoint address="/RelativeAddressX.svc" binding="basicHttpBinding" contract="WCF_Transactions.IService1"></endpoint> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
      <behavior name="MyBehavior1"> 
       <serviceMetadata httpGetEnabled="true" /> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 

因此,我希望我的服務可以通過訪問下一個地址:

http://localhost:18148/RelativeAddressX.svc 

但我不能這樣做。我誤解了什麼?

+0

什麼是託管的類型,您使用可訪問?它是IIS還是自託管? – Alex 2013-02-13 08:44:35

+0

我使用Visual Studio開發服務器 – NET 2013-02-13 08:47:58

回答

6

MSDN http://msdn.microsoft.com/en-us/library/ms733749.aspx: *

有指定兩種方式WCF中的服務的端點地址。 您可以爲與服務相關的每個端點指定一個絕對地址,或者您可以爲服務的服務主機提供一個基地址,然後爲與該基地址相​​關的此服務定義的每個端點指定一個地址。您可以使用這些過程中的每一個,以 爲配置或代碼中的 服務指定端點地址。如果您未指定 相對地址,則該服務使用基地址。

* 所以要根據您的例子中,你有基礎ADRESS

http://localhost:18148/ 

,它會與RelativeAddress.svc相結合,爲您的SVC文件的名稱。然後它會嘗試將該字符串與/RelativeAddressX.svc結合起來作爲端點地址的一部分。所以你會有類似的東西

http://localhost:18148/RelativeAddress.svc/RelativeAddressX.svc. 

你的端點不能在IIS中指定svc的路徑。它應該只包含一個合乎邏輯的地址,與此相關。 所以儘量您的端點更改爲以下:

<endpoint address="RelativeAddressX" binding="basicHttpBinding" contract="WCF_Transactions.IService1"></endpoint> 

,應該由道路

http://localhost:18148/RelativeAddress.svc/RelativeAddressX 
2

當您在IIS或Cassini中託管時,您不需要在配置文件中指定 - 基本URL由Web服務器提供。該元素在自託管時使用。卡西尼(VS建立在網絡服務器)將忽略它。

這裏是關於WCF一個良好的網頁地址: http://msdn.microsoft.com/en-us/magazine/cc163412.aspx

這裏有一些相關的帖子: WCF Service Endpoints vs Host Base address WCF, changing the baseAdress of an endpoint

+0

感謝有用的鏈接。 – NET 2013-02-13 09:23:42