【问题标题】:How to Host a WCF Service in a Managed Windows Service?如何在托管 Windows 服务中托管 WCF 服务?
【发布时间】:2013-08-30 23:02:13
【问题描述】:

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

  • Test Client => 用于通过 tcp ip 添加引用和访问
  • WcfServiceLibruary1 => 用于执行我的方法
  • WindowsService1 => 用于作为 Windows 服务安装和运行(帐户:网络服务,启动类型:自动)

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

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

我使用具有 2 个方法的 wcf 服务。我想在托管的 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 服务主机上启动时一切正常:

【问题讨论】:

  • 我不太明白你的问题是什么。你是说app.config 文件一旦部署就没有配置设置吗?请注意,配置部分需要是 Windows 服务 app.config 文件的一部分,而不是服务库。
  • 我将 wcf app.config 复制到 windows 服务项目。我的 wcf 主机服务无法作为 windows 服务安装到计算机。
  • 当你说它不起作用时,你是什么意思?你有错误吗?服务没有启动吗?你不能连接到服务吗?如果服务正在运行,可以在DOS提示符下输入netstat,查看2023端口是否建立。
  • 当我将它安装为 Windows 服务时,我使用 telnet localhost 2023 检查它
  • 我得到了这个错误:无法建立连接,因为目标机器主动拒绝了它

标签: c# asp.net wcf windows-services


【解决方案1】:

阅读本文

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

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

【讨论】:

  • 该示例可能使用 http,但您可以轻松地将 net.tcp 替换为绑定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
相关资源
最近更新 更多