繁体   English   中英

如何在托管Windows服务中托管WCF服务?

[英]How to Host a WCF Service in a Managed Windows Service?

我的解决方案中有3个项目。

  • 测试客户端=>以添加引用并通过tcp ip访问
  • WcfServiceLibruary1 =>用于执行我的methots
  • WindowsService1 =>用于作为Windows服务安装并运行(帐户:网络服务,启动类型:自动)

在此处输入图片说明

我在msdn示例中使用了所有相同的代码

http://msdn.microsoft.com/en-us/library/ff649818.aspx

我使用的wcf服务有2个methots。我想在托管Windows服务中使用此wcf服务。我在解决方案中添加了Windows服务并设置了引用内容。

我在wcf-app.config上使用此地址引用:

net.tcp://localhost:2023/Service1

现在的问题是:

我成功使用添加了对我的测试客户端项目的引用

net.tcp://localhost:2023/Service1

但是此引用地址不能在Windows服务中安装!!! 当我将其安装为Windows服务时,无法访问该地址,并且出现此错误: No connection could be made because the target machine actively refused it

WcfServiceLibruary app.config:

<?xml version="1.0"?>
  <configuration>
    <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract="WcfServiceLibrary1.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:2023/Service1"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

我的Windows服务:

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

当我在visualstudio服务主机上启动时,一切工作正常: 在此处输入图片说明

阅读本文

在此构建服务并提供托管代码

http://msdn.microsoft.com/en-us/library/ms733069.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM