"服务X具有零应用终端及QUOT;除非我想补充一个端点在code - 为什么?我想、终端、QUOT、code

2023-09-03 12:22:19 作者:幼稚园帮主

我跟着这个MSDN文章创建托管在托管NT服务彻底WCF服务

I followed this MSDN article to create a WCF service hosted in a managed NT service thoroughly.

当我点击开始,在服务控制台然后我看到在事件查看器中的以下内容:

When I click "Start" in services console I then see the following in Event Viewer:

服务将无法启动。 System.InvalidOperationException:服务MyServiceNamespace.RequestProcessorImpl有零应用(非基础结构)终结点。这可能是因为没有配置文件找到您的应用程序,或者是因为没有服务元素相匹配的服务名称可以在配置文件中找到,或者因为没有终点的服务元素定义的。

Service cannot be started. System.InvalidOperationException: Service 'MyServiceNamespace.RequestProcessorImpl' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

我试图检查所有可能的原因,我能找到。下面是在app.config文件服务描述:

I tried to check all possible reasons I could find. Here's the service description in App.Config file:

 <service name="MyServiceNamespace.RequestProcessorWindowsService"
           behaviorConfiguration="RequestProcessorServiceBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8095/RequestProcessorService"/>
      </baseAddresses>
    </host>
    <endpoint address= ""
              binding="wsHttpBinding"
              contract="MyServiceNamespace.IRequestProcessor" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>

所有的实体是named他们的命名空间的,所以这不是问题。 app.config文件放入斌\调试 - 究竟在何处NT服务的启动

All entities are named with their namespaces, so that's not the problem. The App.Config file is placed into bin\Debug - exactly where the NT service starts from.

但是,当我改变我的 ServiceBase的的后裔的OnStart()从原来的实现:

But when I change my ServiceBase descendant OnStart() from the original implementation:

public class RequestProcessorWindowsService : ServiceBase {
    public ServiceHost serviceHost = null;
    //other methods skipped 
    protected override void OnStart(string[] args)
    {
        if( serviceHost != null ) {
       serviceHost.Close();
        }
        serviceHost = new ServiceHost( typeof(RequestProcesssorImpl) );
        serviceHost.Open();
    }
}

下面的一个,这样它调用 AddServiceEndpoint()服务启动好(但我不能添加引用它,所以我想别的东西出了问题):

to the following one so that it calls AddServiceEndpoint() the service starts okay (but I can't add the reference to it, so I guess something else goes wrong):

public class RequestProcessorWindowsService : ServiceBase {
    public ServiceHost serviceHost = null;
    //other methods skipped 
    protected override void OnStart(string[] args)
    {
        if( serviceHost != null ) {
       serviceHost.Close();
        }
        Uri baseAddress = new Uri("http://localhost:8095/RequestProcessorService");
        serviceHost = new ServiceHost( typeof(RequestProcesssorImpl), baseAddress );
        serviceHost.AddServiceEndpoint( typeof(IRequestProcessor), new BasicHttpBinding(), baseAddress );
        serviceHost.Open();
    }
}

为什么我的服务启动时通过的app.config配置?

Why doesn't my service start when configured through App.Config?

推荐答案

在配置文件中的服务名称不匹配的服务实现类。

The service name in the configuration file does not match the service implementation class.

该配置文件应包含:

<service name="MyServiceNamespace.RequestProcesssorImpl"