简体   繁体   English

在Windows服务中托管WCF服务

[英]Hosting a WCF service inside a Windows Service

I am having some trouble hosting a WCF service inside a Windows Service. 我在Windows服务中托管WCF服务时遇到了一些麻烦。 I can start my WCF service in VS2008 and by navigating to the base address in my app.config 我可以在VS2008中启动我的WCF服务,并导航到我的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>

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. 我可以看到它工作正常,我得到的页面说我创建了一个服务,并显示了如何使用它的代码示例。

Now my next step was to create a Windows Service to host my WCF shown above. 现在我的下一步是创建一个Windows服务来托管我上面显示的WCF。

I just used te windows service template, it gave me a Program.cs and Service1.cs which I renamed to WindowsServiceHost.cs. 我刚刚使用了te windows服务模板,它给了我一个Program.cs和Service1.cs,我将其重命名为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();
            }
        }

Everything compiles fine, I can run InstallUtil (I defined an installer). 一切都编译得很好,我可以运行InstallUtil(我定义了一个安装程序)。 The service used to start and stop immediately but disabling Windows Defender got rid of this. 该服务用于立即启动和停止,但禁用Windows Defender摆脱了这一点。 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: 另一个奇怪的事情是,当我尝试停止服务(仍然显示为正在运行)时,我得到:

Error 1061: The service cannot accept control messages at this time 错误1061:此时服务无法接受控制消息

I've tried everything but am at a loss. 我已经尝试了一切,但我不知所措。

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. 不是100%确定原因是什么 - 只是为了确认,我们一直在Windows服务中自我托管WCF服务,它通常完美无缺。

Two points you could try - just to get a feeling for the behavior and a potential clue for the problem: 你可以尝试两点 - 只是为了了解行为和潜在的问题线索:

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: 1)我注意到你只使用服务的类型打开ServiceHost - 这是有效的,但是你甚至可能想要添加一个基地址来调用new ServiceHost() - 就像这样:

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

Can you navigate to that address and get the service page?? 你能导航到那个地址并获得服务页面吗?

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". 2)我注意到的另一件事是你的服务似乎被称为Indexer.WCF.Indexer如打开主机之前在typeof()中指定的那样,但在配置文件中, <service>标签上的name=只是“ 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? 您是否能够在浏览器中导航时看到服务页面?

Marc

Self-hosting HTTP in a Windows service may require registering the endpoint with HttpCfg.exe. Windows服务中的自托管HTTP可能需要使用HttpCfg.exe注册端点。 Take a look here . 看看这里

尝试删除catch语句,可能会出现您没有看到的错误

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

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