在Windows服务(TCP)中托管WCF服务的麻烦

时间:2016-05-10 22:16:25

标签: c# .net wcf windows-services

我想创建WCF服务并通过Windows服务托管它。

我根据教程https://msdn.microsoft.com/en-us/library/ff649818.aspx

创建了所有内容

以下是我的WCF services library App.config 。我已经改变它使用TCP协议

<system.serviceModel>
    <services>
      <service name="WCFServiceLibrary.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WCFServiceLibrary.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:3000/Service1" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

然后我将App.config复制到WindowsService项目。 我还添加了WindowsService的安装程序,然后我更改了属性:

StartType - Automatic
Account - NetWorkService

然后我建立了这个项目 使用Installutil WindowsService.exe

成功安装了它

但是,当我尝试在 WPFClientProject

中添加服务( net.tcp:// localhost:3000 / Service1 )引用时

它出现了:

  

无法识别URI前缀。元数据包含一个引用   无法解决:'net.tcp:// localhost:3000 / Service1'。不能   连接到net.tcp:// localhost:3000 / Service1。连接尝试   持续时间为00:00:02.0641450。 TCP错误代码10061:

看起来我没有主持人。 我做错了什么?

Windows服务代码

public partial class Service1: ServiceBase
{
    internal static ServiceHost myServiceHost = null; 

    public Service1()
    {
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
        if (myServiceHost != null)
        {
            myServiceHost.Close();
        }
        myServiceHost = new ServiceHost(typeof(Service1));
        myServiceHost.Open();
    }
    protected override void OnStop()
    {
        if (myServiceHost != null)
        {
            myServiceHost.Close();
            myServiceHost = null;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我遇到与教程相同的问题。

我解决了它验证Windows服务正在运行,因为登录凭据而未运行。我将Windows服务凭据更改为本地系统帐户,它可以工作。