gpt4 book ai didi

c# - "Service X has zero application endpoints"除非我在代码中添加端点 - 为什么?

转载 作者:可可西里 更新时间:2023-11-01 08:42:13 24 4
gpt4 key购买 nike

我关注了this MSDN article彻底创建托管在托管 NT 服务中的 WCF 服务。

当我在服务控制台中单击“开始”时,我会在事件查看器中看到以下内容:

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 文件中的服务描述:

 <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 with their namespaces ,所以这不是问题。 App.Config 文件放置在 bin\Debug 中 - 正是 NT 服务启动的位置。

但是当我从原始实现更改我的 ServiceBase 后代 OnStart() 时:

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() 服务正常启动(但我无法添加对它的引用,所以我猜其他地方出了问题):

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 配置时没有启动?

最佳答案

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

配置文件应该包含:

<service name="MyServiceNamespace.RequestProcesssorImpl"

关于c# - "Service X has zero application endpoints"除非我在代码中添加端点 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5270956/

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