Search code examples
wcfweb-configwcf-binding

WCF: relativeAddress,baseAddress and binding


I am new in WCF and start my experience with a simple file-less application part of which (web.config) you can see below:

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

Now I can access service at

http://localhost:18148/RelativeAddress.svc

Then I add next lines:

    <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>

So I expect that my service could be accessible through next address:

  http://localhost:18148/RelativeAddressX.svc

but I can't do this. What have I misunderstood?


Solution

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

    There are two ways to specify endpoint addresses for a service in WCF. You can specify an absolute address for each endpoint associated with the service or you can provide a base address for the ServiceHost of a service and then specify an address for each endpoint associated with this service that is defined relative to this base address. You can use each of these procedures to specify the endpoint addresses for a service in either configuration or code. If you do not specify a relative address, the service uses the base address.

    * So according to your example you have base adress

     http://localhost:18148/
    

    and it will be combined with RelativeAddress.svc, as a name of your svc file. And then it will try to combine this string with /RelativeAddressX.svc as a part of endpoint adress. So you will have something like

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

    Your endpoint must not specify the path to svc in IIS. It should containg only a logical adress, assosiated with this point. So try to change your endpoint to the following:

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

    And it should be accessible by the path

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