主机内部的Windows服务WCF服务 [英] Hosting a WCF service inside a Windows Service

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

问题描述

我有一些麻烦托管内的Windows服务WCF服务。 我可以在VS2008开始我的WCF服务,并通过导航到基地址在我的app.config

I am having some trouble hosting a WCF service inside a Windows Service. I can start my WCF service in VS2008 and by navigating to the base address in my app.config

<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="WCF.IndexerBehavior"
        name="WCF.Indexer">
        <endpoint address="" binding="wsHttpBinding" contract="WCF.IIndexer">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" 
contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/WCFService/Action/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WCF.IndexerBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我可以看到它工作得很好,我得到的网页说,我创建了如何使用它显示一个服务和code样本。

I can see it works fine, I get the page saying I created a service and code samples on how to use it are shown.

现在我的下一个步骤是创建一个Windows服务来承载上面显示我的WCF。

Now my next step was to create a Windows Service to host my WCF shown above.

我只是用TE窗口服务模板,它给了我一个Program.cs文件和Service1.cs我改名为WindowsServiceHost.cs。在这里面我有:

I just used te windows service template, it gave me a Program.cs and Service1.cs which I renamed to WindowsServiceHost.cs. In it I have:

private ServiceHost host;

        public WindowsServiceHost()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                var serviceType = typeof(Indexer.WCF.Indexer);
                host = new ServiceHost(serviceType);
                host.Open();
            }
            catch (Exception ex)
            {

            }
        }

        protected override void OnStop()
        {
            if (host != null)
            {
                host.Close();
            }
        }

一切都编译好,我可以运行InstallUtil(我定义的安装程序)。 用于启动和立即但是禁用的Windows Defender停止服务摆脱了这一点。 现在在服务启动(作为一个网络服务),并保持了(我认为),但是当我浏览到的基地址,我得到找不到网页。 另一个奇怪的事情是,当我试图停止服务(这仍显示为正在运行)我得到:

Everything compiles fine, I can run InstallUtil (I defined an installer). The service used to start and stop immediately but disabling Windows Defender got rid of this. Now the service starts (As a network service) and stays up (I think), but when I navigate to the base address, I get the not found page. Another weird thing is when I try to stop the service (which is still displayed as running) I get:

错误1061:此时服务无法接受控制消息

Error 1061: The service cannot accept control messages at this time

我已经竭尽所能,但很茫然。

I've tried everything but am at a loss.

推荐答案

不是100%肯定是什么原因真的是 - 只是为了确认,在Windows中,我们自托管的WCF服务服务所有的时间,所以一般工作完全正常。

Not 100% sure what the reason really is - just to confirm, we self-host WCF services in Windows services all the time and it generally works perfectly fine.

二点,你可以尝试 - 只是为了让与行为的感觉和对问题的一个潜在的线索:

Two points you could try - just to get a feeling for the behavior and a potential clue for the problem:

1)我注意到你的服务只是类型打开ServiceHost的 - 这样的作品,但你可能还是要添加基址甚至到了新的ServiceHost的调用() - 是这样的:

1) I notice you open the ServiceHost with just the type of the service - that works, but you might still want to add a base address even to the call of the new ServiceHost() - like this:

host = new ServiceHost(serviceType, 
                       new Uri("http://localhost:8181/WCFService/Action/");

您可以导航到该地址并获得服务页面??

Can you navigate to that address and get the service page??

2)的另一件事我注意到的是,你的服务好像是叫 Indexer.WCF.Indexer 作为包含在typeof()打开主机前指定的,但在配置文件,在名称= &LT;服务&GT; 标签是唯一的WCF.Indexer

2) The other thing I noticed is that your service seems to be called Indexer.WCF.Indexer as specified in the typeof() before opening the host, but in the config file, the name= on the <service> tag is only "WCF.Indexer".

你能不能尝试更改标签阅读:

Could you possibly try to change that tag to read:

<service behaviorConfiguration="WCF.IndexerBehavior"
         name="Indexer.WCF.Indexer">

帮助吗?您现在可以看到导航到它时,在浏览器?服务页面

Does that help? Are you now able to see the service page when navigating to it in the browser?

马克·

这篇关于主机内部的Windows服务WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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