gpt4 book ai didi

c# - WCF 端口不监听

转载 作者:可可西里 更新时间:2023-11-01 02:53:47 24 4
gpt4 key购买 nike

更新

该服务正在运行并且在任务管理器中有一个 pid 但是当我运行时 netstat -ano | find pid#什么也没有返回 - 端口没有在监听。

如果我运行 netstat -ap tcp该端口未列出。

我真的不知道如何解决这个问题。总之,你不必阅读我以前的帖子:

  • 我的 WCF 服务在 localhost 下完美运行(即同一服务器上的服务和客户端)
  • 当我将服务发布到生产服务器时,服务安装正常但 8523 端口不监听
  • 我已经审查了几个类似的帖子,但没有一个对我有用
  • 这是我尝试的第一个 WCF 项目,所以我已经用尽了我的知识,我无法在互联网上找到更多示例来尝试
  • 在这一点上,我将非常感谢任何指点

  • 环境
  • 开发:Windows 7
  • Visual Studio:2015
  • 生产服务器:Windows Server 2008 R2 Datacenter
  • IIS 版本 7.5.7600.16385

  • 介绍

    我已经研究了这个问题几天了,虽然我在 Stack Overflow 上发现了许多类似的问题,但这些问题都没有解决我的问题;因此,我不认为这是重复的,如果有人像我一样尝试遵循引用的 Microsoft 教程,这将特别有帮助

    问题详情

    我在 https://msdn.microsoft.com/en-us/library/ff649818.aspx 上关注了这篇 Microsoft 文章“如何:使用 TCP 在 Windows 服务中托管 WCF” .

    创建解决方案后,它在我的开发机器上的 Visual Studio localhost 环境中完美运行。本教程建议您创建一个测试客户端来访问 WCF 解决方案,并且测试客户端可以成功运行

    当我在生产机器 (Windows Server 2008) 中发布 WCF 服务和 Windows 服务时,它再次安装而没有错误。以下是我遵循的步骤:

    IIS 设置
  • 将 net.tcp 添加到启用的协议(protocol)
  • 激活“Windows Communication Foundation 非 HTTP 激活”
  • 将 net.tcp 的绑定(bind)更改为 8532
  • 确保“Net.Tcp 监听器适配器”正在运行

  • 在防火墙中打开8532端口

    浏览到WindowsService1.exe所在项目的bin目录,以管理员身份运行exe

    以管理员身份打开命令提示符并运行 Installutil WindowsService1.exe

    Localhost(Visual Studio 中的开发环境)与生产环境的区别

    localhost WCF 系统和生产系统之间的唯一变化是我将 app.conf 中的 baseAddress 从
  • "net.tcp://localhost:8523/CustomWCFService"


  • "net.tcp://10.2.1.1:8523/CustomWCFService"

  • 这是部署的 app.conf

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    </appSettings>
    <system.web>
    <compilation debug="true" />
    </system.web>
    <system.serviceModel>
    <services>
    <service name="MyCustomServiceLib.CustomServiceLib">
    <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
    contract="MyCustomServiceLib.ICustomServiceLib">
    <identity>
    <dns value="localhost" />
    </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
    contract="IMetadataExchange" />
    <host>
    <baseAddresses>
    <add baseAddress="net.tcp://10.2.1.1:8523/CustomWCF" />
    </baseAddresses>
    </host>
    </service>
    </services>
    <behaviors>
    <serviceBehaviors>
    <behavior name="">
    <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
    <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    </serviceBehaviors>
    </behaviors>
    </system.serviceModel>
    </configuration>


    添加服务引用

    当我尝试在将使用 WCF 服务的应用程序中添加服务引用时。我收到此错误:

    There was an error downloading metadata from the address. Please verify that you have entered a valid address



    当我点击“详细信息”时,我看到:

    The URI prefix is not recognized. Metadata contains a reference that cannot be resolved: 'net.tcp://10.2.1.1:8523/CustomWCF'. Could not connect to net.tcp://10.2.1.1:8523/CustomWCF. The connection attempt lasted for a time span of 00:00:01.0312368. TCP error code 10061: No connection could be made because the target machine actively refused it 10.2.1.1:8523. No connection could be made because the target machine actively refused it 10.2.1.1:8523 If the service is defined in the current solution, try building the solution and adding the service reference again.



    我试过的

    在论坛上提出的许多其他 WCF 问题似乎与让服务在 localhost 中工作有关,这不适用于我,因为我创建的解决方案在开发环境中完美运行。

    一些答案建议在项目中启用 tcp 端口共享,但是当我尝试在我的解决方案中添加此代码时,我收到一条错误消息,提示“不允许”。

    其他问题表明该 url 必须在生产服务器上注册,但这不起作用。

    在尝试了我能找到的所有论坛建议并尝试纠正我认为错误的所有内容后,我仍然没有运气。

    任何建议或指示将不胜感激

    最佳答案

    过去我没有让 WCF 与配置文件一起工作。我一直在代码中创建绑定(bind)。

    gist确实有效。

    重要的部分是 NetTcpBinding

     netTcpBinding = new NetTcpBinding
    {
    MaxReceivedMessageSize = int.MaxValue,
    MaxBufferPoolSize = int.MaxValue,
    Security = new NetTcpSecurity
    {
    Mode = SecurityMode.None,
    Transport = new TcpTransportSecurity()
    }
    };

    此外,我不会指定 IP,而是让 WCF 将其绑定(bind)到机器上的每个 IP 地址。
    serviceHost.AddServiceEndpoint(typeof(ITestService), netTcpBinding, string.Format("{1}://0.0.0.0:{0}/", port, "net.tcp"));

    这不会阻止您输入特定的 IP,我通常不会。

    如果您想在 IIS 中运行它,您必须做更多的工作。 IIS 并不真的希望您托管不受 http 绑定(bind)的 WCF 服务器。

    创建 ServiceHost 后,您必须做一些额外的工作
     var serviceBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();

    if (serviceBehavior == null) return;

    serviceBehavior.HttpGetEnabled = false;
    serviceBehavior.HttpsGetEnabled = false;

    关于c# - WCF 端口不监听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43376260/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com