Windows サービス (windowsservices.cs) を含むプロジェクトを作成し、そのクラス (windowservices.cs) に wcf コントラクトとサービス ファイルを追加しました。その app.config ファイルには
<service behaviorConfiguration="WindowsService1.Service1Behavior"
    name="AgentSvcImpl">
    <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
      name="nethttpendpoint" contract="IAgentWinService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
      name="nethttpmetadataendpoint" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8011/AgentSvcImpl" />
      </baseAddresses>
    </host>
  </service>
Windows サービスの onstart メソッドで、次のコードを入力しました。
    ServiceHost serviceHost = null;
        if (serviceHost != null)
        {
            serviceHost.Close();
        }
        //Create a URI to serve as the base address
        Uri httpUrl = new Uri("net.tcp://localhost:8011/AgentSvcImpl");
        //Create ServiceHost
        serviceHost = new ServiceHost(typeof(WindowsService1.AgentSvcImpl), httpUrl);
        //Add a service endpoint
         serviceHost.AddServiceEndpoint(typeof(WindowsService1.IAgentWinService), new NetTcpBinding(), "");
        //Enable metadata exchange
         ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
         smb.HttpGetEnabled = false;
         serviceHost.Description.Behaviors.Add(smb);
        //Start the Service
        serviceHost.Open();
このサービスをservices.mscで開始すると、うまく開始されました。しかし、サービス参照を追加しようとすると、次のエラーが発生します。
メタデータに解決できない参照が含まれています: 'net.tcp://localhost:8011/AgentSvcImpl'。ソケット接続が中止されました。これは、メッセージの処理中にエラーが発生したか、リモート ホストが受信タイムアウトを超過したか、基になるネットワーク リソースの問題が原因である可能性があります。ローカル ソケットのタイムアウトは「00:04:59.0054016」でした。既存の接続がリモート ホストによって強制的に閉じられました サービスが現在のソリューションで定義されている場合は、ソリューションを構築して、サービス参照を再度追加してみてください。
これを解決するには?