WCF服务端点与主机基址

时间:2011-08-04 07:44:38

标签: wcf

所以我对服务端点和主机基地址的含义感到困惑。在到目前为止我已经走过的所有示例中,他们讨论了使用所需绑定设置端点,并且您通常可以导航到这些端点

当我使用以下配置来设置和托管我的服务时,它似乎只暴露了主机的基地址。

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

有人可以向我解释一下吗?

3 个答案:

答案 0 :(得分:33)

在IIS上托管WCF服务时,基址只能是.svc文件的URL。如果指定任何其他基址,则会被忽略。您仍然可以指定端点的相对URI,例如address="basic"address = "ws"。然后,在这种情况下,端点上的地址变为<URL to the .svc file>/basic<URL to the .svc file>/ws

答案 1 :(得分:18)

当您将端点地址留空时,这意味着端点将仅使用相应的基址作为其端点地址。或者,您可以将基址配置为服务的绝对路径,或者作为更好的方法,为端点编写相对地址。

在IIS上托管服务时,服务基址由IIS虚拟目录和.svc文件确定。

  

假设您有一个名为calc.svc的文件,并将其放在与“http:// localhost:8080 / calcservice”对应的虚拟目录中。该服务的基地址为'http:// localhost:8080 / calcservice / calc.svc'。

IIS强制您的端点使用根据您的服务部署路径确定的此基址。 如果您指定一个不同的基地址,然后指定相应的虚拟目录,您将获得一个例外。

考虑以下配置;

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

...第一个端点的地址与基地址('http:// localhost:8080 / calcservice / calc.svc')相同,因为我将端点地址留空。第二个端点的地址成为附加“安全”的基址的组合,如下所示:'http:// localhost:8080 / calcservice / calc.svc / secure'。并且“mex”端点的地址是“http:// localhost:8080 / calcservice / calc.svc / mex”。对于某些人来说这似乎有点奇怪,因为地址的相对部分被添加到文件名的右侧,但是你必须记住calc.svc是基地址的一部分所以它必须以这种方式工作。 / p>

虽然无法通过浏览器导航到“../mex”或“../secure”网址,但它们实际上处于活动状态,客户端可以使用这些地址。

客户行为

  

客户端没有意识到服务的基地址,也没有必要在线路上支持类似的东西。因此,您将无法在客户端对象模型或配置部分中找到与基址相关的任何内容。客户端只需选择一个特定的端点,该端点始终配置有绝对地址,该绝对地址决定了它在传输过程中将使用的地址。

以上信息主要是从msn上的Aaron Skonnard的优秀article中提取的。我强烈建议你阅读它以获得WCF寻址背后的基础知识。

答案 2 :(得分:6)

当您使用基地址时,您不需要为端点提供绝对URI,例如,您可以在端点配置部分使用address="basic",这意味着该端点的地址为{{1 }}。

相关问题