开发者

WCF Service Endpoints vs Host Base address

开发者 https://www.devze.com 2023-03-25 23:01 出处:网络
so i\'m a litle confused over what the service endpoints and host base address is for. In all the examples i have walked through so far they talk about setting up the endpoints with the required bindi

so i'm a litle confused over what the service endpoints and host base address is for. In all the examples i have walked through so far they talk about setting up the endpoints with the required bindings and you can normally navigate to those endpoints

Hoever when i use the following config to set up and host my service it only seems to expose the Hosts base address.

    <configuration>
      <system.web>
        <compilation debug="true" />
      </system.web>
      <!-- When deploying the service library project, the content of the config file must be added to the host's
      app.config file. System.Configuration does not support config files for libraries. -->
      <system.serviceModel>
        <services>
          <service name="HostService.EvalService">
            <endpoint address="http://localhost:8080/basic"
              binding="basicHttpBinding" contract="HostService.开发者_如何学编程IEvalService" />
            <endpoint address="http://localhost:8080/ws"
              binding="wsHttpBinding" contract="HostService.IEvalService" />
            <endpoint address="mex" binding="mexHttpBinding"
              name="mex" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/EvalsService" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>

Can someone explain this to me?


When you host the WCF service on IIS, the base address can only be the URL to the .svc file. If you specify any other base address, it's ignored. You can still specify the relative URI for your endpoints, such as address="basic" or address = "ws". Then the address on the endpoint becomes <URL to the .svc file>/basic and <URL to the .svc file>/ws in this case.


When you leave the endpoint address empty, it means that endpoint will simply use the corresponding base address for its endpoint address. Alternatively, you can configure the base address as the absolute path of your service, or as a better approach, write relative addresses for your endpoints.

When you host your services on IIS, service base address is determined by the IIS virtual directory along with the .svc file.

Suppose you have a file named calc.svc and you place it in a virtual directory that corresponds to 'http://localhost:8080/calcservice'. The base address for this service will be 'http://localhost:8080/calcservice/calc.svc'.

IIS forces your endpoints to use this base address determined according to your service deployment path. If you specify a different base address then the corresponding virtual directory, you'll get an exception.

Consider the configuration below;

<configuration>
    <system.serviceModel>
        <services>
            <service name="CalculatorService">
              <!-- base address determined by IIS virtual directory -->
              <endpoint binding="basicHttpBinding" contract="ISimpleMath"/>
              <endpoint address="secure" binding="wsHttpBinding" contract="ISimpleMath"/>
              <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        ...

... the address of the first endpoint becomes the same as the base address ('http://localhost:8080/calcservice/calc.svc') since I left the endpoint address empty. The address of the second endpoint becomes the combination of the base address appended with "secure", like this: 'http://localhost:8080/calcservice/calc.svc/secure'. And the address of the "mex" endpoint is 'http://localhost:8080/calcservice/calc.svc/mex'. This can seem a bit strange to some people since the relative portion of the address is added to the right of the file name, but you have to remember that calc.svc is part of the base address so it has to work this way.

Though you can not navigate to the "../mex" or "../secure" URLs through browser, they are actually active and clients can consume these addresses.

Client behavior

Clients have no awareness of the service’s base address and have no need to support something similar on their side of the wire. As a result, you won’t find anything related to base addresses in the client-side object model or the configuration section. Clients simply choose a particular endpoint, which always comes configured with an absolute address, and that absolute address determines the address it will use during transmission.

Above information mostly extracted from Aaron Skonnard's excellent article on msdn. I strongly suggest you read it to get the fundamentals behind WCF addressing.


when you use base address, you don't need to provide absolute URI for your endpoints, for example, you may use address="basic" in the endpoint configuration section, this means the address for that endpoint is http://localhost:8080/EvalsService/basic.

0

精彩评论

暂无评论...
验证码 换一张
取 消