如何托管WCF服务在托管Windows服务? [英] How to Host a WCF Service in a Managed Windows Service?

查看:184
本文介绍了如何托管WCF服务在托管Windows服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 3 project in my solution.


  • 测试客户端=>通过TCP IP ADD参考,并访问

  • WcfServiceLibruary1 =执行我的methots>

  • WindowsService1 =安装和功放>;运行Windows服务(帐户:网络服务,StartType:自动)

我用所有相同的codeS MSDN上的样本

I used all same codes on msdn sample

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

我使用具有2 methots.I WCF服务要使用管理窗口这个WCF服务service.I增加了一个窗口服务,我的解决方案,并设置引用的东西。

I use a wcf service that has 2 methots.I want to use this wcf service in managed windows service.I added a windows service to my solution and set references stuff.

我用这个地址全球化志愿服务青年上我的WCF - 的app.config:

I use this address referance on my wcf - app.config:

net.tcp://localhost:2023/Service1

现在的问题是:

我的成功使用add参考我的测试客户端项目

I success to add reference to my test client project using

的net.tcp://本地主机:2023 /服务1

但这个全球化志愿服务青年地址不上安装Windows服务使用!
当我将它安装为Windows服务,我不能访问这个地址,
而我得到这个错误:无连接可以作出,因为目标机器积极地拒绝它

But this referance address is not be used on install as windows service !!! When i install it as windows service,i cant access this address, And i got this error: No connection could be made because the target machine actively refused it

WcfServiceLibruary的app.config:

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>

我WindowsService:

My WindowsService :

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

一切都工作得很好,当我的VisualStudio服务主机上启动:

Everything works well when i start on visualstudio service host:

推荐答案

阅读这篇文章

在此构建服务,并提供托管code

In this Construct the service and provide the hosting code

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

这篇关于如何托管WCF服务在托管Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆