.net 服务具有零个应用程序(非基础设施)端点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2328840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Service has zero application (non-infrastructure) endpoints
提问by user280626
I recently created a WCF service (dll) and a service host (exe). I know my WCF service is working correctly since I am able to successfully add the service to WcfTestClient.
我最近创建了一个 WCF 服务 (dll) 和一个服务主机 (exe)。我知道我的 WCF 服务工作正常,因为我能够成功地将服务添加到 WcfTestClient。
However, I seem to be running into an issue when I comes to utlizing my WCF from a service host (exe). I can add a reference to the WCF (dll) to my service host (exe) and create the necessary componets to the exe; such as the service installer, service host, and the app.config, compile and then finally install the exe using InstallUtil. But, when I tried to start the service in the Microsoft Management Console, the service immediately stops after being started.
但是,当我从服务主机 (exe) 使用 WCF 时,我似乎遇到了问题。我可以向我的服务主机 (exe) 添加对 WCF (dll) 的引用,并为 exe 创建必要的组件;如服务安装程序、服务主机和app.config,编译然后最后使用InstallUtil 安装exe。但是,当我尝试在 Microsoft 管理控制台中启动该服务时,该服务在启动后立即停止。
So I began investigating what could exactly be causing this issue an came up with this error from the Application Log in the Event Viewer.
所以我开始调查究竟是什么导致了这个问题,并从事件查看器中的应用程序日志中发现了这个错误。
Description:
描述:
Service cannot be started. System.InvalidOperationException: Service 'Service' 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.
服务无法启动。System.InvalidOperationException:服务“服务”的应用程序(非基础设施)端点为零。这可能是因为没有为您的应用程序找到配置文件,或者因为在配置文件中找不到与服务名称匹配的服务元素,或者因为没有在服务元素中定义端点。
This error is actually generated in the OnStart; of my exe, when I perform this call ServiceHost.Open(). I've seen numerous posts where other individuals have run into this issue, however most if not all of them, claim that the service name or contract; namespace and class name, are not being specified. I checked both of these entries in my config file; in the exe as well as in the dll, and they match up PERFECTLY. I've had other people in the office double check behind me to make sure I wasn't going blind at one point, but of course they came to the same conclusion as me that everything looked like it was specified correctly. I am truly at a lost as to what is going on at this point. Could anyone help me with this issue?
这个错误实际上是在OnStart; 我的 exe 文件,当我执行此调用时ServiceHost.Open()。我看过很多帖子,其他人也遇到过这个问题,但大多数(如果不是全部)都声称服务名称或合同;未指定命名空间和类名。我在我的配置文件中检查了这两个条目;在 exe 和 dll 中,它们完美匹配。我让办公室里的其他人在我身后仔细检查,以确保我没有一度失明,但当然他们得出的结论与我相同,一切看起来都被正确指定。我真的不知道在这一点上发生了什么。有人可以帮我解决这个问题吗?
Another thing that came up as a possible reason this may be happening is that the app.config is never being read; at least not the one I think should be getting read. Could this be the issue? If so, how can I go about addressing this issue. Again, ANY help would be appreciated.
出现这种情况的另一个可能原因是 app.config 从未被读取;至少不是我认为应该阅读的那个。这可能是问题吗?如果是这样,我该如何着手解决这个问题。再次,任何帮助将不胜感激。
回答by SteveCav
I just had this problem and resolved it by adding the namespace to the service name, e.g.
我刚刚遇到了这个问题并通过将命名空间添加到服务名称来解决它,例如
<service name="TechResponse">
became
变成了
<service name="SvcClient.TechResponse">
I've also seen it resolved with a Web.config instead of an App.config.
我还看到它用 Web.config 而不是 App.config 解决。
回答by Asif Khan
The endpoint should also have the namespace:
端点还应该具有命名空间:
<endpoint address="uri" binding="wsHttpBinding" contract="Namespace.Interface" />
回答by Kevin Won
One thing to think about is: Do you have your WCF completely uncoupled from the WindowsService (WS)? A WS is painful because you don't have a lot of control or visibility to them. I try to mitigate this by having all of my non-WS stuff in their own classes so they can be tested independently of the host WS. Using this approach might help you eliminate anything that is happening with the WS runtime vs. your service in particular.
需要考虑的一件事是:您的 WCF 是否与 WindowsService (WS) 完全分离?WS 是痛苦的,因为您对它们没有很多控制或可见性。我试图通过将我所有的非 WS 内容放在它们自己的类中来缓解这种情况,以便它们可以独立于主机 WS 进行测试。使用这种方法可能会帮助您消除 WS 运行时发生的任何事情,尤其是您的服务。
John is likely correct that it is a .config file problem. WCF will always look for the execution context .config. So if you are hosting your WCF in different execution contexts (that is, test with a console application, and deploy with a WS), you need to make sure you have WCF configuration data moved over to the proper .config file. But the underlying issue to me is that you don't know what the problem is because the WS goo gets in the way. If you haven't refactored to that yet so that you can run your service in any context (that is, unit test or console), then I'd sugget doing so. If you spun your service up in a unit test, it would likely fail the same way that you are seeing with the WS which is much easier to debug rather than attempting to do so with the yucky WS plumbing.
约翰很可能是正确的,这是一个 .config 文件问题。WCF 将始终寻找执行上下文 .config。因此,如果您在不同的执行上下文中托管 WCF(即,使用控制台应用程序进行测试,并使用 WS 进行部署),则需要确保将 WCF 配置数据移至正确的 .config 文件。但对我来说潜在的问题是你不知道问题是什么,因为 WS goo 妨碍了。如果您还没有重构,以便您可以在任何上下文(即单元测试或控制台)中运行您的服务,那么我建议您这样做。如果您在单元测试中启动您的服务,它可能会以与您在 WS 中看到的相同的方式失败,这更容易调试,而不是尝试使用令人讨厌的 WS 管道来调试。
回答by usufhamad
Just copy the App.config file from the service project to the console host application and paste here and then delete it from the service project.
只需将 App.config 文件从服务项目复制到控制台主机应用程序并粘贴到此处,然后从服务项目中删除它。
回答by LCJ
I got a more detailed exception when I added it programmatically - AddServiceEndpoint:
当我以编程方式添加它时,我得到了一个更详细的异常 - AddServiceEndpoint:
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(MyNamespace.IService),
new BasicHttpBinding(), baseAddress);
host.Open();
回答by U?ur Aldanmaz
To prepare the configration for WCF is hard, and sometimes a service type definition go unnoticed.
为 WCF 准备配置很困难,有时服务类型定义会被忽视。
I wrote only the namespace in the service tag, so I got the same error.
我在服务标签中只写了命名空间,所以我得到了同样的错误。
<service name="ServiceNameSpace">
Do not forget, the service tag needs a fully-qualified service class name.
不要忘记,服务标签需要一个完全限定的服务类名。
<service name="ServiceNameSpace.ServiceClass">
For the other folks who are like me.
对于其他和我一样的人。
回答by ash
Today i ran into same issue, posting here my mistake and correction of it so that it may help someone.
今天我遇到了同样的问题,在这里发布我的错误并更正它,以便它可以帮助某人。
While Re-structuring code, I had actually changed Service class and IService names and changed ServiceHost to point to this new Service class name (as shown in code snippet) but in my host applications App.Config file i was still using old Service class name.(refer config section's name field in below snippet)
在重构代码时,我实际上更改了服务类和 IService 名称并将 ServiceHost 更改为指向这个新的服务类名称(如代码片段所示),但在我的主机应用程序 App.Config 文件中,我仍在使用旧的服务类名称.(参考下面代码段中配置部分的名称字段)
Here is the code snippet,
这是代码片段,
ServiceHost myServiceHost = new ServiceHost(typeof(NewServiceClassName));
and in App.config file under section servicesi was referring to old serviceclass name , changing it to New ServiceClassName fixed issue for me.
在 App.config 文件中的服务部分下,我指的是旧的 serviceclass name ,将其更改为 New ServiceClassName 为我解决了问题。
<service name="ProjectName.OldServiceClassName">
<endpoint address="" binding="basicHttpBinding" contract="ProjectName.IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress=""/>
</baseAddresses>
</host>
</service>
回答by Bo Christian Skj?tt
I had the same problem. Everything works in VS2010 but when I run the same project in VS2008 I get the mentioned exception.
我有同样的问题。在 VS2010 中一切正常,但是当我在 VS2008 中运行相同的项目时,我得到了提到的异常。
What I did in my VS2008 project to make it work was adding a call to the AddServiceEndpointmember of my ServiceHost object.
我在我的 VS2008 项目中所做的工作是添加一个AddServiceEndpoint对我的 ServiceHost 对象成员的调用。
Here is my code snippet:
这是我的代码片段:
Uri baseAddress = new Uri("http://localhost:8195/v2/SystemCallbackListener");
ServiceHost host = new ServiceHost(typeof(SystemCallbackListenerImpl), baseAddress);
host.AddServiceEndpoint(typeof(CsfServiceReference.SystemCallbackListener),
new BasicHttpBinding(),
baseAddress);
host.Open();
I didn't modify the app.config file. But I guess the service endpoint could also have been added in the .config file.
我没有修改 app.config 文件。但我想服务端点也可以添加到 .config 文件中。
回答by letsgetsilly
I just worked through this issue on my service. Here is the error I was receiving:
我刚刚在我的服务中解决了这个问题。这是我收到的错误:
Service 'EmailSender.Wcf.EmailService' 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.
服务“EmailSender.Wcf.EmailService”的应用程序(非基础设施)端点为零。这可能是因为没有为您的应用程序找到配置文件,或者因为在配置文件中找不到与服务名称匹配的服务元素,或者因为没有在服务元素中定义端点。
Here are the two steps I used to fix it:
这是我用来修复它的两个步骤:
Use the correct fully-qualified class name:
<service behaviorConfiguration="DefaultBehavior" name="EmailSender.Wcf.EmailService">Enable an endpoint with mexHttpBinding, and most importantly, use the IMetadataExchange contract:
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
使用正确的完全限定类名:
<service behaviorConfiguration="DefaultBehavior" name="EmailSender.Wcf.EmailService">使用 mexHttpBinding 启用端点,最重要的是,使用 IMetadataExchange 合同:
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
回答by wasted30minOnThis
I just ran into this issue and checked all of the above answers to make sure I wasn't missing anything obvious. Well, I had a semi-obvious issue. My casing of my classname in code and the classname I used in the configuration file didn't match.
我刚刚遇到了这个问题并检查了上述所有答案,以确保我没有遗漏任何明显的内容。好吧,我有一个半明显的问题。我在代码中的类名的大小写与我在配置文件中使用的类名不匹配。
For example: if the class name is CalculatorService and the configuration file refers to Calculatorservice ... you will get this error.
例如:如果类名是 CalculatorService 并且配置文件是指 Calculatorservice ...你会得到这个错误。

