代码之家  ›  专栏  ›  技术社区  ›  Orion Adrian

WCF作为应用程序工作,但不作为服务工作

  •  4
  • Orion Adrian  · 技术社区  · 16 年前

    我有一个可以作为服务或Windows窗体应用程序运行的WCF服务器。当我作为Windows窗体应用程序运行它时,我可以通过我的客户端应用程序连接到它。但是,当我使用相同的代码作为服务运行它时,我无法连接到它。我已确认该服务正在运行并正在执行其工作。下面是服务器的配置文件。

    <system.serviceModel>
      <services>
        <service name="Cns.TrafficCopService.ManagementService">
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:8000/TrafficCop/ManagementService" />
            </baseAddresses>
          </host>
          <endpoint address="" binding="wsHttpBinding" contract="Cns.TrafficCopService.IManagementService" />
        </service>
      </services>
    </system.serviceModel>
    

    它的宿主代码,在调用OnStart后100毫秒调用:

    if (this.serviceHost != null)
    {
        this.serviceHost.Close();
    }
    
    this.serviceHost = new ServiceHost(typeof(ManagementService));
    this.serviceHost.Open();
    

    以及客户端的配置文件:

    <system.serviceModel>
      <bindings>
        <wsHttpBinding>
          <binding name="WSHttpBinding_IManagementService" />
        </wsHttpBinding>
      </bindings>
      <client>
        <endpoint
            address="http://localhost:8000/TrafficCop/ManagementService"
            binding="wsHttpBinding"
            bindingConfiguration="WSHttpBinding_IManagementService"
            contract="IManagementService"
            name="WSHttpBinding_IManagementService">
        </endpoint>
      </client>
    </system.serviceModel>
    
    6 回复  |  直到 15 年前
        1
  •  2
  •   Andy White    16 年前

    你能把剩下的托管服务的代码贴出来吗?

    启动服务的类应继承自“ServiceBase”,并应实现“OnStart”和“OnStop”方法。服务控制台调用这些方法来启动和停止服务进程,因此应该在这些方法中打开/关闭服务主机。只是想知道你是否不这么做。

        2
  •  1
  •   Community CDub    7 年前

    服务的运行方式是什么?我想知道服务是否无法启动,可能是因为没有打开端口的权限。

    尝试以您自己的身份运行服务(但作为服务)。如果有效,这是一个权限问题。最有可能的是http.sys权限。

    要分配访问权限,请使用 netsh 在Vista/Windows7上,或在XP上为httpcfg。

        3
  •  0
  •   Grzenio    16 年前

    您从何处获取创建服务主机的代码?我的第一个猜测是,当您将它作为服务运行时,您要么不创建servicehost,要么不保留对它的引用(因此它会被垃圾收集)。

        4
  •  0
  •   Community CDub    7 年前

    如果您在同一台计算机上,我建议使用netnamedpipebinding而不是wshttpbinding。它更快。如果您需要跨机器使用,可以随时更改回WS-HTTP。

    确保您的服务实际上是通过TaskManager运行的。如果没有,请将debugger.break()语句放到服务的构造函数中,并单步执行以查找它未能启动的位置。 Here 是在C(如果需要)中创建Windows NT服务的一步一步的简洁方法。

        5
  •  0
  •   Philippe    16 年前

    事件日志中没有关于注册地址失败的内容?

    是否尝试调试服务(使用Visual Studio附加到进程)?

        6
  •  0
  •   David M    16 年前

    您检查过WinForms应用程序和服务的配置文件中都定义了该配置吗?