当服务绑定是nettcpbinding时,使用http发布元数据

时间:2015-10-25 05:50:58

标签: .net wcf .net-4.0

我是WCF的新用户,我正在尝试使用netTcpBinding

我的问题是如何发布元数据?

当我使用tcp地址作为基地址时,它会出现此错误

: The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address. Either supply an http base address or set HttpGetUrl to an absolute address.

我需要发布元数据。但是如何在使用此

时执行此操作
<baseAddresses>
        <add baseAddress = "net.tcp://localhost:8732/Design_Time_Addresses/tester/Service1/" />

</baseAddresses>

我的终点是这样的

 <endpoint address ="" binding="netTcpBinding" contract="tester.IService1">

当我更改我的端点以获取此类

的元数据时
   <endpoint address="mex" binding="wsHttpBinding" contract="IMetadataExchange"/>

现在它给了我这个错误

 Please try changing the HTTP port to 8732 or running as Administrator.

3 个答案:

答案 0 :(得分:0)

在您的serviceBehaviors中,您指定了以下行为:

<serviceMetadata httpGetEnabled=True/>

由于您决定使用HTTP协议公开元数据,因此默认情况下,WCF会在baseAddresses中寻找HTTP等价物。唉,你没有指定这样的错误发生。解决方案是放弃在HTTP上公开元数据或添加另一个baseAddress,这次使用http协议或设置具有绝对地址的HttpGetUrl。

答案 1 :(得分:0)

你在IIS上托管了吗?如果是这样,您需要启用WAS - Windows激活服务。请参阅:what is the diffence between WAS and IIS?

如果没有,请在配置设置中提供地址参考。

答案 2 :(得分:0)

因为当我搜索通过nettcp绑定发布元数据时,这是第一次导致谷歌搜索。所以想再回答一次。以下是app.config文件。

<endpoint address ="" binding="netTcpBinding" contract="tester.IService1">
<baseAddresses>
<add baseAddress = "net.tcp://localhost:8732/Design_Time_Addresses/tester/Service1/" />

步骤1:通过端点以及需要托管的baseaddress定义nettcp绑定。所以它将是

<services>
    <service name="tester.Service1">
    <endpoint address ="" binding="netTcpBinding" contract="tester.IService></endpoint>
<host>
  <baseAddresses>
    <add baseAddress="net.tcp://localhost:8732/Design_Time_Addresses/tester/Service1/"/>
  </baseAddresses>
</host>
</service>

步骤2:启用nettcp元数据端点。

<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>

步骤3:启用nettcp元数据端点后,使其可被外部发现,即通过serviceMetadata标记实现IMetadataExchange合同。

 <behaviors>
    <serviceBehaviors>
      <behavior name="Default">
        <serviceMetadata httpGetEnabled="false" httpGetUrl=""/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  </services>

步骤4:现在将服务行为与暴露的服务联系起来并将其绑定。

    <services>
            <service name="tester.Service1" behaviorConfiguration="Default">
            <endpoint address ="" binding="netTcpBinding" contract="tester.IService></endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8732/Design_Time_Addresses/tester/Service1/"/>
          </baseAddresses>
        </host>
        </service>
        </services>

现在,您可以通过VS命令提示符检查WCFtestclient.exe并浏览net.tcp:// localhost:8732 / Design_Time_Addresses / tester / Service1 /或浏览此地址和服务nettcp元数据将被发现。

注意:如果您希望通过http(浏览器)发现元数据需要wsdl,则需要进行以下修改。

1.添加http元数据端点

 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

2.提供合同实施并将httpGetEnabled设为true以使服务可被发现。

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

所以最终端点定义将是

    <services>
    <service name="tester.Service1" behaviorConfiguration="Default">
    <endpoint address ="" binding="netTcpBinding" contract="tester.IService></endpoint>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      <host>
      <baseAddresses>
      <add baseAddress="net.tcp://localhost:8732/Design_Time_Addresses/tester/Service1/"/>
     <add  baseAddress="http://localhost:8732/Design_Time_Addresses/tester/Service1/"/>
       </baseAddresses>
       </host>
 </service>
</services>