【问题标题】:Can't add service reference to net tcp wcf service无法将服务引用添加到 net tcp wcf 服务
【发布时间】:2012-02-15 22:45:14
【问题描述】:

我有一个 WCF 服务作为 Windows 服务托管在我的本地计算机上。现在我正在尝试在客户端中添加服务引用(再次在我的本地开发机器上),但出现错误

元数据包含无法解析的引用: 'net.tcp://localhost:8523/Service1'。无法连接到 net.tcp://localhost:8523/Service1。连接尝试持续了 时间跨度为 00:00:02.0011145。 TCP 错误代码 10061:无连接 可能是因为目标机器主动拒绝了它

我检查了 Windows 防火墙没有阻止端口 8523。我什至在 Windows 防火墙中创建了一个新规则以允许运行 8523 端口。但是当我运行netstat -sp tcp 时,我似乎找不到端口 8523。但我可以看到 Serice1 服务的状态在服务中设置为 START。这是配置文件

服务库

<system.serviceModel>
  <services>
    <service 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" />
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="false" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

Windows 服务项目中的配置看起来相同。

【问题讨论】:

  • 如果托管在控制台应用程序中,该服务是否具有相同的配置并按预期工作?您是否检查了事件日志中可能来自 Windows 服务的错​​误消息?您也可以尝试完全禁用防火墙,以确保它不会造成问题。
  • 一旦您启动该服务,您确定它不会因错误而再次停止(Clemens 建议与事件日志将显示这一点)。刷新服务视图可能会显示它再次停止
  • @Clemens 我检查了事件日志,不存在错误消息。它只是显示服务成功启动的信息。我完全禁用了 Windows 防火墙,不幸的是仍然出现同样的错误。
  • @RichardBlewett 正如我在上面的评论中所说,服务运行成功,并且没有任何问题继续运行。
  • 只是因为我在活动 tcp 连接列表中看不到 8523 端口,而我的服务正在运行,并且我在事件日志中看不到错误消息,这可能是配置有问题,它会阻止服务在 8523 端口下运行???有什么方法可以从 Services 中查看我的服务下运行的端口是什么?

标签: wcf tcp nettcpbinding


【解决方案1】:

Michael,这是我通常用于 WCF 服务的 net.tcp 绑定。

    <bindings>
        <netTcpBinding>
            <binding name="TcpBinding"
                     receiveTimeout="Infinite"
                     sendTimeout="Infinite"
                     maxBufferSize="2147483647"
                     maxReceivedMessageSize="2147483647">
                <reliableSession inactivityTimeout="Infinite"/>
                <security mode="None"/>
            </binding>
        </netTcpBinding>
    </bindings>

尝试将其添加到您的配置中:

<service name="WcfServiceLibrary1.Service1">   
    <endpoint    
        address=""    
        binding="netTcpBinding" bindingConfiguration="TcpBinding"   
        contract="WcfServiceLibrary1.IService1">   
    ...

另外,我的服务在ServiceAccount.LocalSystem 下运行。

检查

netstat -ap tcp

如果服务正在监听。

编辑:下面是我的服务类。请注意,Windows 服务的当前目录以编程方式设置为BaseDirectory,即可执行文件的目录。

public class Service : ServiceBase
{
    public static void Main()
    {
        Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
        ServiceBase.Run(new Service());
    }

    ...
}

【讨论】:

  • 抱歉回复晚了,克莱门斯也试过了,但似乎仍然无法为服务创建客户端。
  • 你看到服务在监听 8523 端口了吗?
  • 不,Clemens,这就是问题所在,我似乎无法运行 8523 端口,所以我用控制台应用程序更改了 windows 服务,特别是现在控制台应用程序正在托管服务,这可以作为预期的,但我不明白为什么,因为 Windows 服务和控制台应用程序都使用相同的服务和相同的配置。那么为什么我可以使用 Console Application 托管我的服务,但不能通过 Windows Service 托管它呢?有什么线索吗?
  • 服务是否在ServiceAccount.LocalSystem下运行?
  • 目前它在 NetworkService 帐户下运行,但我按照您的方法,将其更改为在本地系统下运行,但没有任何改变。我什至将其更改为在我的帐户下运行(我是我的开发机器上的管理员),但到目前为止还没有运气。
【解决方案2】:

你已经指定了一个行为,但没有给它一个名字。为行为命名并使用服务中的 behaviorConfiguration 指向它。

<service name="WcfServiceLibrary1.Service1" behaviorConfiguration="svc1behavior">

...

<serviceBehaviors>
<behavior name="svc1behavior">
  <serviceMetadata httpGetEnabled="false" />
  <serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>

【讨论】:

  • 如果您使用 .NET4,则不需要命名行为块 - 未命名的行为块充当服务的默认行为配置
【解决方案3】:

检查 Net.Tcp 侦听器适配器服务是否正在运行(尽管如果它在控制台应用程序中托管服务时工作,它应该正在运行)。

要查找您的服务正在使用的端口,

  1. 打开任务管理器
  2. 选择视图 -> 选择列 -> 检查 PID -> 确定
  3. 找到您的服务进程名称,记下 PID
  4. 从命令提示符运行以下命令:netstat -ano | findstr *PID*

【讨论】:

    【解决方案4】:

    有同样的问题。经过大量故障排除后,我发现我在服务项目的 Web.config 中缺少对服务的引用,因此我将其添加到 Web.config:

    <system.serviceModel>
      <services>
        <service name="serviceName" behaviorConfiguration="BasicServiceBehavior" >
          <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
          <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicServiceBasicHttpBinding" contract="(I<nameofservice>)"/>
        </service>
      </services>
    </system.serviceModel>
    

    在此之后重建解决方案(创建界面)并重新发布。然后我终于可以从我的其他项目中添加一个服务引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 2014-05-08
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 2011-01-23
      相关资源
      最近更新 更多