WCF, changing the baseAdress of an endpoint

我们两清 提交于 2019-12-24 12:00:20

问题


i have a few questions about the below config file:

<system.serviceModel>
  <bindings />
  <services>
    <service behaviorConfiguration="WcfReporting.Service1Behavior"
             name="WcfReporting.Service1">
      <endpoint address="" 
                binding="basicHttpBinding" bindingConfiguration=""
                contract="WcfReporting.IService1">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" 
                contract="IMetadataExchange" />
      <host>
        <baseAddresses>
          <add baseAddress="net.tcp://localhost:5050/" />
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="WcfReporting.Service1Behavior" >
        <!-- To avoid disclosing metadata information, set the value below to false 
             and remove the metadata endpoint above before deployment -->
        <serviceMetadata httpGetEnabled="true"/>
        <!-- To receive exception details in faults for debugging purposes, 
             set the value below to true. Set to false before deployment to 
             avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>
  1. Why when I hit F5 to restart the service, the service starts with this URL http://localhost:2752/ ... why not 5050 as I specified in baseAddresses.

  2. How how can I add another endpoint. I tried with endpoint address="/Address2" binding="basicHttpBinding" contract="WcfReporting.IService1" />

should I how be able to access the service, not only with http://localhost/VirtualDir/ but also with http://localhost/VirtualDir/address2 or how does it work?


回答1:


If you're hosting in Cassini from within Visual Studio 2005 or later, you can specify the port using Project/Properties/Web/Use Visual Studio Development Server/Specific Port.

By default the port will be auto-assigned - which isn't very helpful for Web Services as your clients will probably want to be using a fixed URL.

You don't need to specify <baseAddresses> in the config file when hosting in IIS or Cassini - the base URL is provided by the web server. The <baseAddresses> element is used when self-hosting.




回答2:


How how can I add another endpoint. I tried with endpoint address="/Address2" binding="basicHttpBinding" contract="WcfReporting.IService1" />

The addresses you specify in this endpoint need to be local and relative - e.g. just specify

<endpoint address="Address2"
          binding="basicHttpBinding"
          contract="WcfReporting.IService1" />

and this will create an endpoint at the complete address of

net.tcp://localhost:5050/Address2

But as Darin has already pointed out - if you use IIS / WAS to host your service, the virtual directory where your *.svc file resides will take precedence and the base addresses specified will be ignored. In order to really use the base addresses, you'll need to self-host the service in a console app or Windows service.

Marc




回答3:


If you use a web server (such as Cassini or IIS) to host your WCF service the base address will be provided from this server. Also note that you cannot use TCP bindings over HTTP. If you want to be able to set the base address property you need to host the service yourself (for example in NT service, console or windows application).



来源:https://stackoverflow.com/questions/909332/wcf-changing-the-baseadress-of-an-endpoint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!