在Web应用程序设置TCP WCF服务 [英] Setting up WCF TCP service in a web application

查看:327
本文介绍了在Web应用程序设置TCP WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在争夺这个好几天,经过百余条给予了如何建立一个WCF基于TCP服务的Web应用程序部分准则字面上去。如果有人能帮助我,我就使这个问题成为一个完成方针。

I've been battling with this for days, literally going through a hundred articles giving partial guidelines on how to set up a WCF TCP based service in a web application. If someone can help me, I will make this question into a complete guideline.

当前状态

本的net.tcp连接工作我的机器上。它也被部署到Windows Server 2008 R2后,本地工作。但是,它不远程工作,即使它可能telnet到808端口的服务器上远程。滚动到的详细信息问题的底部。如果可以请帮帮忙。

The net.tcp connection works on my development machine. It also works locally after being deployed to a Windows Server 2008 R2. However, it doesn't work remotely, even though it's possible to telnet to port 808 on the server remotely. Scroll to the bottom of the question for details. Please help if you can.

我将为这个细节一个新的问题,如果我得到一个结果出来的更新与回答这个问题。

I will create a new question for this detail and update this question with the answer if I get a result out of it.

code

我创建 ServerHubService.svc 包含以下内容:

namespace Manage.SignalR
{
    [ServiceContract]
    public class ServerHubService
    {
        [OperationContract]
        public void UpdateServerStatus(string serverStatus)
        {
            // Do something
        }
    }
}

的托管服务的应用程序的配置

以下的在线教程,我增加了以下到我的web.config(我尝试了许多不同的变化)。这是Web应用程序托管服务,我以后要使用TCP连接的Web.config。

Following an online tutorial, I added the following to my Web.config (I tried MANY different variations). This is the Web.config of the web application hosting the service that I later want to connect to with TCP.

<configuration>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServerHubBehavior"
      name="Manage.SignalR.ServerHubService">
        <endpoint address=""
              binding="netTcpBinding"
              bindingConfiguration="portSharingBinding"
              name="MyServiceEndpoint"
              contract="Manage.SignalR.ServerHubService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

        <endpoint address="mex"
              binding="mexTcpBinding"
              bindingConfiguration=""
              name="MyServiceMexTcpBidingEndpoint"
              contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://test.mydomain.com:808/SignalR/ServerHubService.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServerHubBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding name="portSharingBinding" portSharingEnabled="true"/>
      </netTcpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      minFreeMemoryPercentageToActivateService="0" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

httpGetEnabled =真正的,因为否则你无法创建一个服务参考服务是非常重要的。

httpGetEnabled="true" is important because otherwise you cannot create a service reference to the service.

Web应用程序正在运行,其中服务器的配置

Configuration of the server where the web application is running

我有我的开发机器上设置此功能的IIS 7.5

I am setting this up on my development machine which has IIS 7.5

我读了IIS防爆preSS(内置到Visual Studio)不支持的net.tcp,所以我成立了一个新的网站在IIS 7.5中使用.NET 4.5,并具有net.tcp绑定

I read that IIS Express (built into Visual Studio) doesn't support net.tcp, so I set up a new website in IIS 7.5 using .NET 4.5 and that has a net.tcp binding

我还去了高级设置的网站,并启用一套议定书 HTTP,的net.tcp

I also went to Advanced Settings for the website and set Enabled Protocols to http,net.tcp

我确保了Windows功能非HTTP激活启用(并重新启动)。这是一个Windows功能以便寻找启用Windows功能打开或关闭发现这一点。

I ensured that the Windows Feature Non-HTTP Activation is enabled (and restarted). It's a Windows Feature so look for "Turn Windows features on or off" to find this.

运行确认Web应用程序

该网站工作正常的Web应用程序的其余部分。我成立了test.mydomain.com点在我的主机文件127.0.0.1。我甚至可以访问<一个href=\"http://test.mydomain.com/SignalR/ServerHubService.svc\">http://test.mydomain.com/SignalR/ServerHubService.svc它会告诉我从.NET一个很好的自动生成的页面,解释如何使用这项服务。

The website works fine for the rest of the web application. I set up test.mydomain.com to point to 127.0.0.1 in my hosts file. I can even visit http://test.mydomain.com/SignalR/ServerHubService.svc and it will show me a nice auto generated page from .NET, explaining how to use this service.

到目前为止好。

由.NET生成的页面告诉我使用这个地址来生成我的连接服务:

The page generated by .NET tells me to use this address to generate a connection to my service:

net.tcp://computername/SignalR/ServerHubService.svc/mex

试图连接到服务作为客户端

如果您不记得来设置 httpGetEnabled =真正的试图建立一个服务引用它时,你会得到一个错误。如果您使用WCF测试客户端(也包含在Visual Studio工具)并没有设置httpGetEnabled,你会得到像下面这样的错误:

If you do not remember to set httpGetEnabled="true" you will get an error when trying to create a service reference to it. If you use the WCF Test Client (also a tool included in Visual Studio) and didn't set httpGetEnabled, you will get an error like the following:

错误:无法获取元数据
  的net.tcp://computername/SignalR/ServerHubService.svc/mex

Error: Cannot obtain Metadata from net.tcp://computername/SignalR/ServerHubService.svc/mex

如果这是Windows(R)通信基础服务,您
  有机会,请检查您是否已经启用元数据发布在
  指定的地址。如需帮助实现元数据发布,请
  是指在MSDN文档
  <一href=\"http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata\">http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata

If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata

Exchange错误URI:
  的net.tcp://computername/SignalR/ServerHubService.svc/mex元
  包含无法解析的引用:
  '的net.tcp://computername/SignalR/ServerHubService.svc/mex。不能
  连接到的net.tcp://computername/SignalR/ServerHubService.svc/mex。
  连接尝试持续了00一个时间跨度:00:04.0032289

Exchange Error URI: net.tcp://computername/SignalR/ServerHubService.svc/mex Metadata contains a reference that cannot be resolved: 'net.tcp://computername/SignalR/ServerHubService.svc/mex'. Could not connect to net.tcp://computername/SignalR/ServerHubService.svc/mex. The connection attempt lasted for a time span of 00:00:04.0032289.

TCP错误code 10061:无法连接,因为目标提出
  计算机主动拒绝[2001:0:4137:9e76:C81:A4C:A547:b2fd]:808。
  无法连接,因为目标机器积极地进行
  拒绝了[2001:0:4137:9e76:C81:A4C:A547:b2fd]:808

TCP error code 10061: No connection could be made because the target machine actively refused it [2001:0:4137:9e76:c81:a4c:a547:b2fd]:808. No connection could be made because the target machine actively refused it [2001:0:4137:9e76:c81:a4c:a547:b2fd]:808

不过,如果你所做的一切如上,你应该能够添加一个引用到服务。

However, if you've done everything as above, you should be able to add a reference to the service.

调用的方法在服务

当试图调用一个简单的Hello World方法从WCF测试客户端的服务,它返回以下错误:

When trying to call a simple Hello World method in the service from the WCF Test Client, it returns the following error:

无法连接
  的net.tcp://computername/SignalR/ServerHubService.svc。连接
  尝试历时00一个时间跨度:00:04.0002288。 TCP错误code
  10061:无法连接,因为目标机器积极地进行
  拒绝了。

Could not connect to net.tcp://computername/SignalR/ServerHubService.svc. The connection attempt lasted for a time span of 00:00:04.0002288. TCP error code 10061: No connection could be made because the target machine actively refused it.

这是包含在与该错误内堆栈跟踪

This is inner stacktrace that is included in with the error:

无法连接,因为目标机器积极地进行
  拒绝了[2001:0:5ef5:石川铃华:3884:A:A547:b2fd]:808
  System.Net.Sockets.Socket.DoConnect(端点endPointSnapshot,
  SocketAddress的SocketAddress的)在
  System.Net.Sockets.Socket.Connect(端点remoteEP)在
  System.ServiceModel.Channels.SocketConnectionInitiator.Connect(URI
  URI,时间跨度超时)

No connection could be made because the target machine actively refused it [2001:0:5ef5:79fb:3884:a:a547:b2fd]:808 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Connect(EndPoint remoteEP) at System.ServiceModel.Channels.SocketConnectionInitiator.Connect(Uri uri, TimeSpan timeout)

如果您使用的netstat -an |找到/我听一看就知道没有什么是监听端口808,这可能是因为的net.tcp侦听器适配器服务没有运行。

If you use netstat -an |find /i "listening" to see that nothing is listening on port 808, it's probably because the Net.Tcp Listener Adapter service is not running.

确认

通话经历了,但还需要一些确认可以宣告成功之前。我需要确认这是事实上的net.tcp端口808,并没有实际的HTTP端点的通话呼叫。我想使用Wireshark做到这一点,但它并没有显示出来,可能是因为它是一个打来和我的本地机器上发生的事情。

The call goes through now, but some confirmation is needed before it can be declared a success. I need to confirm that this is in fact net.tcp call on port 808 and not actually a call on the http endpoint. I am trying to do this with Wireshark, but it doesn't show up, probably because it's a call happening from and to my local machine.

部署

征服的最后一个挑战是在Web服务器上部署此,确保在开发机器上是什么在起作用,也适用在Web服务器上。

The last challenge to conquer would be to deploy this on a web server, ensuring that what works on the development machine, also works on the web server.

它不工作后发布到Windows Server 2008 R2。它提供了常见的 SocketException:现有连接被强行关闭远程主机

It doesn't work after publish to a Windows Server 2008 R2. It gives the common SocketException: An existing connection was forcibly closed by the remote host.

它的工作原理以及在本地服务器上,但它不能远程工作。

It works well locally on the server, but it doesn't work remotely.

下面是用于检查服务器的清单:

Here is the checklist used to check the server:


  • 的net.tcp侦听器适配器服务运行?是

  • 是IIS站点绑定到的net.tcp 设置为 808 * ?是

  • 在高级设置中启用协议的网站在IIS中设置为 HTTP,的net.tcp ?是

  • 是服务器侦听端口808?是的,的netstat -an检查|找到/我听

  • 是一个端口808在防火墙打开?是。

    • 防火墙被关闭在服务器上。

    • 我可以从外部远程登录到服务器上的端口808 的telnet mydomain.com 808

    • Is the Net.Tcp Listener Adapter service running? Yes
    • Is the IIS site binding to net.tcp set to 808:*? Yes
    • Is Enabled Protocols under Advanced Settings for the site in IIS set to http,net.tcp? Yes
    • Is the server listening on port 808? Yes, checked with netstat -an |find /i "listening"
    • Is a port 808 open in the firewall? Yes.
      • Firewall is turned off on the server.
      • I can telnet to the server on port 808 from outside with telnet mydomain.com 808

      • baseAddress调整到的net.tcp://mydomain.com:808 / SignalR / ServerHubService.svc

      • 这是本地主机过,但改为 mydomain.com 后,它并没有在服务器上运行: &LT;同一性GT;&LT; D​​NS值=mydomain.com/&GT;&LT; /身份&GT;

      • baseAddress is adjusted to net.tcp://mydomain.com:808/SignalR/ServerHubService.svc
      • This was localhost before, but changed to mydomain.com after it didn't work on the server: <identity><dns value="mydomain.com" /></identity>

      什么样的​​配置可能会缺少在服务器上?我是如此接近的目标。请帮助我解决此并完成的问题。

      What configuration could be missing on the server? I am so close to the goal. Please assist me in troubleshooting this and complete the question.

      下面是服务器调用服务的客户端配置:

      Here is the client configuration on the server calling the service:

      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="MyServiceEndpoint" />
          </netTcpBinding>
        </bindings>
        <client>
          <endpoint address="net.tcp://mydomain.com:808/SignalR/ServerHubService.svc"
            binding="netTcpBinding" bindingConfiguration="MyServiceEndpoint"
            contract="ServerHubService.ServerHubService" name="MyServiceEndpoint">
            <identity>
              <dns value="mydomain.com" />
            </identity>
          </endpoint>
        </client>
      </system.serviceModel>
      

      我也试过没有808在端点配置,因为这被传言是对的net.tcp连接的默认端口。然而,它仍然给出了相同的结果。

      I also tried without 808 configured in the endpoint, as this is rumored to be the default port for net.tcp connections. However, it still gives the same result.

      而不是试图很多东西随机,良好的问题也许是:一个人怎么调试这样的事情? 我可以安装服务器会告诉我到底为什么这个调用被阻止上的程序?

      Instead of trying lots of things randomly, the good question is perhaps: How does one debug something like this? Can I install a program on either server that will tell me exactly WHY this call is being blocked?

      使用Wireshark的配置就是这样,有可能看调用来在服务器上的端口808,并可能确定为什么不能正常工作。不过,我不知道如何分析这在present时间。

      With Wireshark configured like this, it's possible to look at the call coming to the server on port 808 and possibly determine why the call doesn't work. However, I have no idea how to analyze this at present time.

      好运

      我放弃了,在插座层实现这个代替。它会立即奏效。我希望这可以帮助别人,虽然。我设置一个答案,因为许多问题得到了解决,并没有在本地工作,最终,因此剩下的可能与我的具体环境中的任何问题。

      I gave up and implemented this in the socket layer instead. It worked immediately. I hope this can help someone else, though. I'm setting an answer because many of the issues were solved and it did work locally eventually, so any problem remaining is probably related to me specific environment.

      推荐答案

      您有一个基于暴露在HTTP的TCP元数据来生成代理。(请确保httpGetEnabled设置为true)。您在客户端使用的地址应为托管的地址。请参考下面的文章。

      You have to generate the Proxy based on the metadata exposed over HTTP for tcp.(make sure httpGetEnabled is set to true). The address you use at the client should be the hosted address. Please refer the below post.

      <一个href=\"http://blogs.msdn.com/b/swiss_dpe_team/archive/2008/02/08/iis-7-support-for-non-http-protocols.aspx\">http://blogs.msdn.com/b/swiss_dpe_team/archive/2008/02/08/iis-7-support-for-non-http-protocols.aspx

      这篇关于在Web应用程序设置TCP WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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