简体   繁体   English

Windows服务托管TCP WCF服务

[英]Windows Service Hosted TCP WCF Service

I am trying to host a WCF service on a windows 2008 R2 server as a windows service. 我试图在Windows 2008 R2服务器上托管WCF服务作为Windows服务。 I followed the directions provided by msdn ( found here ). 我按照msdn( 在这里找到 )提供的指示。 Everything works fine as long as everything is part of the same solution in visual studio. 只要所有内容都是Visual Studio中同一解决方案的一部分,一切都可以正常工作。 However, I tried creating a client in a different solution (on the same machine) and it can't find the service. 但是,我尝试在不同的解决方案(在同一台机器上)创建客户端,但无法找到该服务。 I get an 'Add Service Reference Error' shown below. 我收到如下所示的“添加服务引用错误”。

在此输入图像描述

My goal is to be able to access the wcf service remotely, but I can't seem to even access it locally unless the client was created within the same client. 我的目标是能够远程访问wcf服务,但我似乎甚至无法在本地访问它,除非客户端是在同一客户端内创建的。 Is there any guides, tutorials, or helpful hints that anyone can give me to get this to work? 是否有任何指南,教程或有用的提示,任何人都可以让我让这个工作?

Update: It seems that even though the windows service is running, the WCF service doesn't seem to be listening to any ports. 更新:似乎即使Windows服务正在运行,WCF服务似乎也没有监听任何端口。 Which would suggest that it isn't running. 这表明它没有运行。 This also explains why everyone's first thought I didn't have the service running. 这也解释了为什么每个人都认为我没有运行该服务。 I had assumed that since the windows service was running and that the same solution client worked, that the WCF service was working as well. 我曾经假设,因为Windows服务正在运行并且同一个解决方案客户端工作,所以WCF服务也正常运行。 Turns out that Visual Studio was starting up a WCF service whenever I ran the same solution client. 事实证明,每当我运行相同的解决方案客户端时,Visual Studio就会启动WCF服务。

So, why isn't the windows service starting the WCF service? 那么,为什么Windows服务不启动WCF服务呢? Any ideas? 有任何想法吗?

It turns out that there was a problem with the tutorial provided by MSDN (provided in the question above). 事实证明,MSDN提供的教程存在问题(在上面的问题中提供)。 They named both the windows service and the WCF service Service1 which is the default name for both of them. 他们将Windows服务和WCF服务Service1命名为两者的默认名称。

The windows service was suppose to start the WCF service, however, it was actually trying to start itself again because both services had the same name. Windows服务假设启动WCF服务,但它实际上是尝试再次启动,因为两个服务具有相同的名称。

myServiceHost = new ServiceHost(typeof(Service1));

To fix this problem, you can rename one of the services or fully qualify the WCF service when referenced in the windows service. 要解决此问题,您可以在Windows服务中引用时重命名其中一个服务或完全限定WCF服务。

myServiceHost = new ServiceHost(typeof(WcfServiceLibrary1.Service1));

The funny thing is that the code provided still looks like it works because Visual Studio is smart enough to notice that the WCF service isn't running and it starts an instance up behind the scenes. 有趣的是,提供的代码看起来仍然有效,因为Visual Studio足够聪明,可以注意到WCF服务没有运行,它在幕后启动实例。

It was a simple bug to fix, but because Visual Studio was hiding the problem from me, I don't think I would have found it without the help from Espen Burud. 修复这是一个简单的错误,但由于Visual Studio隐藏了我的问题,我认为如果没有Espen Burud的帮助我就不会找到它。

There are two ways for Add Service Reference to learn about a service: 添加服务引用有两种方法可以了解服务:

Discover button: searches the projects in the current solution. Discover按钮:搜索当前解决方案中的项目。
Go button: connects to the service in the Address box and retrieves the metadata. Go按钮:连接到“地址”框中的服务并检索元数据。

You need to actually have the service running before you click Go . 在单击Go之前,您需要实际运行该服务。

EDIT 编辑

I just noticed from your screenshot that you're trying to connect to a net.tcp URL. 我刚刚从您的屏幕截图中注意到您正在尝试连接到net.tcp网址。 I think it's more common to use http for MEX. 我认为将http用于MEX更为常见。 Your app.config would look something like: 你的app.config看起来像这样:

<services>
  <service behaviorConfiguration="WcfServiceLibrary1.Service1Behavior"
    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:8523/Service1" />
        <add baseAddress="http://localhost:8524/Service1" />
      </baseAddresses>
    </host>
  </service>
</services>

Note the different port number for the http base address. 请注意http基址的不同端口号。 You would then use "http://localhost:8524/Service1" in the Add Service Reference tool. 然后,您将在“添加服务引用”工具中使用“http:// localhost:8524 / Service1”。 You should also be able to connect to it with your web browser. 您还应该能够使用Web浏览器连接到它。

To allow metadata exchange via http GET (eg from a browser), you also need to enable it via a behavior: 要允许通过http GET进行元数据交换(例如,从浏览器),您还需要通过以下行为启用它:

<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

I'm not sure if the Add Service Reference tool cares about that. 我不确定添加服务引用工具是否关心它。

Even if you don't want to allow http get access (httpGetEnabled="False"), you still need to include this behavior to enable MEX (unless you're adding it programatically). 即使您不想允许http get访问(httpGetEnabled =“False”),您仍然需要包含此行为以启用MEX(除非您以编程方式添加它)。

I have tested the MSDN article in the and it works without modifications. 我已经测试了MSDN中的文章,它无需修改即可使用。 If the firewall is enabled on the server, I think you will need to add some rules for your service. 如果在服务器上启用了防火墙,我认为您需要为您的服务添加一些规则。

To verify that the service are listening on the correct tcp port, you can use command: netstat -a . 要验证服务是否正在侦听正确的tcp端口,可以使用命令: netstat -a If the service are listening on the correct port, this command will return: 如果服务正在侦听正确的端口,则此命令将返回:

Proto  Local Address          Foreign Address        State
TCP    0.0.0.0:8523           machinename:0          LISTENING

I managed to figure out the issue. 我设法找出问题所在。 My service didn't know about the endpoints because I hadn't copied the service configuration from the app.config in the WCF project into the app.config of the actual windows service. 我的服务不了解端点,因为我没有将服务配置从WCF项目中的app.config复制到实际Windows服务的app.config中。 Once I did that it functioned correctly. 一旦我这样做,它运作正常。

This was not made clear in the original MSDN article which I had also followed although it is mentioned in a comment in the WCF app.config. 虽然在WCF app.config的评论中提到了这篇文章,但我在MSDN上的文章中也没有明确说明这一点。

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

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