使用netTcp绑定时添加服务引用

时间:2013-03-07 12:01:06

标签: wcf nettcpbinding windows-hosting

我通过将其接口复制到示例客户端项目来获得我测试的WCF服务。
现在我想通过添加服务引用来正常工作 该服务托管在Windows托管中(使用installUtil) 该服务有2个项目 - 外部(接口+数据交换)和内部(实现) 由于某种原因,它没有app.config所以我手动添加了一个:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="ExecutionService" behaviorConfiguration="Default">
        <endpoint name="TCPEndpoint" address="" binding ="netTcpBinding" contract="Externals.IExecutionService"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:3040/ExecutionService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

尝试从我的示例客户端添加服务引用会导致以下错误:

Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:3040/ExecutionService/Externals.IExecutionService'.
There was no endpoint listening at net.tcp://localhost:3040/ExecutionService/Externals.IExecutionService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
If the service is defined in the current solution, try building the solution and adding the service reference again.

我看到here在app.config中没有必要 我有点困惑,我是WCF的初学者 一个不错的WPF应用程序如何引用我的服务?我希望服务是windows托管的,我不想和我一起拖动dll。

修改
我添加了一个元数据端点,我的appconfig现在看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="ExecutionService" behaviorConfiguration="Default">
        <endpoint name="TCPEndpoint" 
                  address="" 
                  binding ="netTcpBinding" 
                  contract="Externals.IExecutionService"/>
        <endpoint address="mex" 
                  binding="maxHttpBinding" 
                  contract="Externals.IExecutionService"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:3040/ExecutionService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我尝试使用net.tcp://localhost:3040/ExecutionServicenet.tcp://localhost:3040/ExecutionService/Externalsnet.tcp://localhost:3040/ExecutionService/Externals/IExecutionService添加服务引用,但我仍然遇到同样的错误。

3 个答案:

答案 0 :(得分:27)

你需要这样做:

  1. maxHttpBinding - &gt; mexTcpBinding - 你不能在net.tcp端点上使用mexHttpBinding(而且它的mex不是最大值)
  2. mex端点的合同必须是IMetadataExchange - 因为您希望通过此端点提供服务元数据
  3. httpGetEnabled =“false”因为没有http端点来从
  4. 获取元数据
  5. 当我在一个简单的控制台主机中测试解决方案时,我需要在&lt; service&gt;中更改名称标记为Externals.ExecutionService(但这取决于您实例化服务的方式)
  6. 然后您的服务参考位于:net.tcp://localhost:3040/ExecutionService/mex 基本地址为net.tcp://localhost:3040/ExecutionService,并且mex端点的相对地址设置为mex

    最终的app.config如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.serviceModel>
    <services>
      <service name="Externals.ExecutionService" behaviorConfiguration="Default">
        <endpoint name="TCPEndpoint"
                  address=""
                  binding ="netTcpBinding"
                  contract="Externals.IExecutionService"/>
        <endpoint address="mex"
                  binding="mexTcpBinding"
                  contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:3040/ExecutionService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    </system.serviceModel>
    </configuration>
    

    如果配置正确,我可以使用控制台主机应用作为服务主机进行快速测试。 的Program.cs:

    using System;
    using System.ServiceModel;
    
    namespace Externals
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                var sh=new ServiceHost(typeof(ExecutionService));
                sh.Open();
                Console.WriteLine("Service running press any key to terminate...");
                Console.ReadKey();
                sh.Close();
            }
        }
    }
    

    运行控制台应用并尝试通过net.tcp://localhost:3040/ExecutionService/mex向项目添加服务引用。

答案 1 :(得分:2)

乍一看,你忘记了Metadata Endpoint

答案 2 :(得分:2)

改变这个:

<endpoint address="mex" 
                  binding="maxHttpBinding" 
                  contract="Externals.IExecutionService"/>

要:

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

注意它不是maxHttpBinding而是mexTcpBinding,你也有错字。

相关问题