简体   繁体   English

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

[英]WCF works as application, but not as service

I have a WCF server that I can run as a service or as a windows forms application.我有一个 WCF 服务器,可以作为服务或 Windows 窗体应用程序运行。 When I run it as a Windows Forms application I can connect to it via my client application.当我将它作为 Windows 窗体应用程序运行时,我可以通过我的客户端应用程序连接到它。 However when I run it as a service using the same code, I cannot connect to it.但是,当我使用相同的代码将它作为服务运行时,我无法连接到它。 I have confirmed that the service is running and doing its work.我已经确认该服务正在运行并完成其工作。 Below is the server's config file.下面是服务器的配置文件。

<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>

and its hosting code, called 100 milliseconds after OnStart is called:及其托管代码,在调用 OnStart 后 100 毫秒调用:

if (this.serviceHost != null)
{
    this.serviceHost.Close();
}

this.serviceHost = new ServiceHost(typeof(ManagementService));
this.serviceHost.Open();

and the client's config file:和客户端的配置文件:

<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>

Could you post the rest of your code for hosting the service?你能发布你的其余代码来托管服务吗?

Your class that starts the service should be inheriting from "ServiceBase" and should implement the "OnStart" and "OnStop" methods.启动服务的类应该继承自“ServiceBase”,并且应该实现“OnStart”和“OnStop”方法。 These methods are invoked by the service console to start and stop the service process, so your ServiceHost should be opened/closed in these methods.这些方法由服务控制台调用来启动和停止服务进程,因此您的 ServiceHost 应该在这些方法中打开/关闭。 Just wondering if maybe you're not doing that.只是想知道您是否不这样做。

What account is the service running as?服务以什么帐户运行? I wonder if the service is failing to start, probably due to not having permissions to open the port.我想知道服务是否无法启动,可能是由于没有打开端口的权限。

Try running the service in your own identity (but as a service).尝试以您自己的身份(但作为服务)运行该服务。 If it works, it is a permissions issue.如果它有效,则是权限问题。 The most likely is the HTTP.SYS permissions.最有可能的是 HTTP.SYS 权限。

To assign access, you use netsh on vista/window 7, or httpcfg on xp.要分配访问权限,请在 vista/window 7 上使用netsh ,或在 xp 上使用 httpcfg。

Where did you take the code that creates the service host from?您从何处获取创建服务主机的代码? My fist guess would be that when you run it as a service you either don't create the ServiceHost, or you don't keep the reference to it (so it gets garbage-collected)我的第一个猜测是,当您将它作为服务运行时,您要么不创建 ServiceHost,要么不保留对它的引用(因此它会被垃圾收集)

If you are on the same machine, I would suggest using the NetNamedPipeBinding instead of WSHttpBinding.如果您在同一台机器上,我建议使用 NetNamedPipeBinding 而不是 WSHttpBinding。 It's faster.它更快。 You can always change back to the ws-http if you need cross-machine usage down the road.如果您以后需要跨机器使用,您可以随时改回 ws-http。

Make sure your service is actually running via TaskManager.确保您的服务实际上是通过 TaskManager 运行的。 If not, put a Debugger.Break() statement in your service's constructor and step through to find where it fails to start.如果没有,请将 Debugger.Break() 语句放入您的服务的构造函数中,并逐步查找无法启动的位置。 Here is a concise step-by-step for creating a Windows NT service in C# (if you need it). 是在 C# 中创建 Windows NT 服务的简明分步(如果需要)。

Nothing in the event log about failing to register the address?事件日志中没有关于未能注册地址的内容?

did you try to debug the service (using visual studio attach to process)?您是否尝试调试服务(使用 Visual Studio 附加到进程)?

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

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

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