这是我的代码,有两种宿主方式,我用的是宿主在控制台里面也就是自宿主,而且下面的代码也能启动起来,我是在启动的状态下添加service服务引用的,但是我添加服务的时候缺报错。请问是怎么回事?
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8087/'.
Could not connect to net.tcp://localhost:8087/. The connection attempt lasted for a time span of 00:00:01.9531250. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8087. 
No connection could be made because the target machine actively refused it 127.0.0.1:8087
If the service is defined in the current solution, try building the solution and adding the service reference again.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Cyclone.Chat.Contracts;
using Cyclone.Chat.Service;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;namespace Cyclone.Chat.Host
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("net.tcp://localhost:8087/");
            using (ServiceHost host = new ServiceHost(typeof(ChatService), baseAddress))
            {
                BindingElement bindingElement = new TcpTransportBindingElement();
                CustomBinding binding = new CustomBinding(bindingElement);                 host.AddServiceEndpoint(typeof(IChat), binding, "ChatService");
                ServiceMetadataBehavior metadataBehavior; metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (metadataBehavior == null)
                {
                    metadataBehavior = new ServiceMetadataBehavior();
                    host.Description.Behaviors.Add(metadataBehavior);
                }
                host.Open();
                Console.Write("The service is ready,press any key to exit!");
                Console.ReadLine();
            }
        }
    }
}

解决方案 »

  1.   

    添加service服务引用的时候必须使用http开头的地址,所以你必须保证你的服务里有类似下面的节配置:
             <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8087/ChatService"/>
              </baseAddresses>
            </host>当然,如果你完全使用代码来配置也可以,请测试以下代码:
            static void Main(string[] args)
            {
                Uri baseAddress = new Uri("http://localhost:8087/ChatService");
                using (ServiceHost host = new ServiceHost(typeof(ChatService), baseAddress))
                {
                    BindingElement bindingElement = new TcpTransportBindingElement();
                    CustomBinding binding = new CustomBinding(bindingElement);                 host.AddServiceEndpoint(typeof(IChat), binding, "net.tcp://localhost:8087/ChatService");
                    ServiceMetadataBehavior metadataBehavior; metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                    if (metadataBehavior == null)
                    {
                        metadataBehavior = new ServiceMetadataBehavior();
                        host.Description.Behaviors.Add(metadataBehavior);
                    }
                    host.Open();
                    Console.Write("The service is ready,press any key to exit!");
                    Console.ReadLine();
                }
            }
      

  2.   

    楼上的,报错:
    HTTP could not register URL http://+:8087/ChatService/ because TCP port 8087 is being used by another application.
      

  3.   

    那你试试用配置文件呢?通过service服务引用添加客户端我只在app.config下设置services写过,通过代码方式没做过。
    我一开始使用WCF的时候也是添加service服务引用,那是看着例子设置的app.config。后来发现可以通过代码设置的时候,已经不再添加service服务引用了,客户端直接代码设置,引用公共接口dll,这样效率很高,所以你这种写法我没有试过,你可以考虑将地址写在配置文件中。类似这样的配置文件节点,是可以正常添加引用的,引用地址:http://localhost:8000/ckth/service
    <services>
          <service name="ConsoleApplication1.DataManagerService"
                   behaviorConfiguration="DataManagerBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8000/ckth/service"/>
              </baseAddresses>
            </host>
            <!-- this endpoint is exposed at: net.tcp://localhost:9000/servicemodelsamples/service  -->
            <endpoint address="net.tcp://localhost:9000/ckth/service"
                      binding="netTcpBinding"
                      bindingConfiguration="Binding1" 
                      contract="ConsoleApplication1.IDataManager" />
            <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
            <endpoint address="mex" 
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />
          </service>
        </services>
            static void Main(string[] args)
            {
                // Create a ServiceHost for the CalculatorService type.
                using (ServiceHost serviceHost = new ServiceHost(typeof(DataManagerService)))
                {
                    // Open the ServiceHost to create listeners and start listening for messages.
                    
                    serviceHost.Open();
                    // The service can now be accessed.                Console.WriteLine("服务已开启.");
                    Console.WriteLine("按 <ENTER> 终止服务.");
                    Console.WriteLine();
                    Console.ReadLine();
                    serviceHost.Close();
                }
            }
      

  4.   

    楼上的,WCF对IIS有什么要求吗?我的是IIS 5.1,但是我试过宿主到IIS中绑定用的是wsHttpBinding,这样是没有问题的
      

  5.   

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>  </connectionStrings>
      <system.serviceModel>
        <services>
          <service name="ConsoleApplication1.DataManagerService"
                   behaviorConfiguration="DataManagerBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8000/ckth/service"/>
              </baseAddresses>
            </host>
            <!-- this endpoint is exposed at: net.tcp://localhost:9000/servicemodelsamples/service  -->
            <endpoint address="net.tcp://localhost:9000/ckth/service"
                      binding="netTcpBinding"
                      bindingConfiguration="Binding1" 
                      contract="ConsoleApplication1.IDataManager" />
            <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
            <endpoint address="mex" 
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />
          </service>
        </services>    <bindings>
          <!-- 
                Following is the expanded configuration section for a NetTcpBinding.
                Each property is configured with the default values.
                See the Message Security, and Transport Security samples in the WS binding samples
                to learn how to configure these features.
             -->
          <netTcpBinding>
            <binding name="Binding1" 
                     closeTimeout="00:01:00"
                     openTimeout="00:01:00" 
                     receiveTimeout="00:10:00" 
                     sendTimeout="00:01:00"
                     transactionFlow="false" 
                     transferMode="Buffered" 
                     transactionProtocol="OleTransactions"
                     hostNameComparisonMode="StrongWildcard" 
                     listenBacklog="10"
                     maxBufferPoolSize="1073741824" 
                     maxBufferSize="1073741824" 
                     maxConnections="10"
                     maxReceivedMessageSize="1073741824">
              <readerQuotas maxDepth="32" 
                            maxStringContentLength="1073741824" 
                            maxArrayLength="16384"
                            maxBytesPerRead="4096" 
                            maxNameTableCharCount="16384" />
              <reliableSession ordered="true" 
                               inactivityTimeout="00:01:00"
                               enabled="false" />
              <security mode="None">
                <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
              </security>
            </binding>
          </netTcpBinding>
        </bindings>    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
        <behaviors>
          <serviceBehaviors>
            <behavior name="DataManagerBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="True" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>