94

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.

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.

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.

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?

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user280626
  • 971
  • 1
  • 7
  • 5

17 Answers17

101

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
SteveCav
  • 6,649
  • 1
  • 50
  • 52
  • Yeah, we changed namespaces so it couldn't find the service to match the one in the .svc file (an underscore got switched with a dot!) A quick check of the service names revealed what was up. –  Mar 14 '12 at 15:25
  • 1
    Solved my problem as well, love these quick and easy fixes! – vfilby Sep 10 '12 at 15:02
  • Adding a web.config helped resolve this error for me. – Ryan Rodemoyer Feb 03 '14 at 23:11
  • 2
    This solved my problem!!! Thanks a lot! For all newbyes like me: suggest this really clear tutorial: http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/#more-102 – Homer1982 May 12 '17 at 16:38
13

The endpoint should also have the namespace:

 <endpoint address="uri" binding="wsHttpBinding" contract="Namespace.Interface" />
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Asif Khan
  • 139
  • 1
  • 2
  • I have this in the app.config but I want to change it in the code without removing it from the app.config. Using new ServiceHost with a new endpoint address gives an error. – Paul McCarthy Jun 28 '19 at 13:45
10

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
usufhamad
  • 101
  • 1
  • 2
9

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.

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin Won
  • 7,156
  • 5
  • 36
  • 54
  • 1
    Thanks for responding so quickly. I did copy over my app.config from my WCF (dll), so I to do not think that is the issue. But I do just find it odd that I am able to bring my WCF (dll) up using the WcfTestclient.exe without any issues. It seems to me if anything was a mist with the config file it should have failed there as well, not just when I try to run it in a Windows Service Host (exe). I apologize if I sound a "bit" lost, I am still a newbie at WCF and services period unfortunately. Any other suggestions? – user280626 Feb 24 '10 at 21:14
5

I got a more detailed exception when I added it programmatically - 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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 1
    Thanks! passing the address this way showed me more details of the exception. In my case it was simply that the port was used by another process :) – Hernan Veiras Jul 21 '14 at 14:18
  • I have have the same problem is the base address the same as the address when you click discover in the add Service reference? – ZoomVirus Sep 22 '14 at 09:49
5

To prepare the configration for WCF is hard, and sometimes a service type definition go unnoticed.

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Uğur Aldanmaz
  • 1,018
  • 1
  • 11
  • 16
4

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)

Here is the code snippet,

 ServiceHost myServiceHost = new ServiceHost(typeof(NewServiceClassName)); 

and in App.config file under section services i was referring to old serviceclass name , changing it to New ServiceClassName fixed issue for me.

  <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>
mabiyan
  • 667
  • 7
  • 25
  • Same here. I changed the capitalization of my class and contract name and everything worked. Thanks. – Chazaq Nov 30 '18 at 18:16
3

I had the same problem. Everything works in VS2010 but when I run the same project in VS2008 I get the mentioned exception.

What I did in my VS2008 project to make it work was adding a call to the AddServiceEndpoint member of my ServiceHost object.

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.

  • When I use this method I get AddressAccessDeniedException even though i can use this address for thee addServiceReferance address. – ZoomVirus Sep 22 '14 at 09:57
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Just experienced the same thing. Can be tough to find, especially when refactoring large code bases. _Remember to update namespaces in WCF config when moving things around._ – Arve Systad Oct 09 '19 at 07:18
  • In my case I accidentally renamed service because of "S101:Types should be named in PascalCase" and "classname in code and the classname I used in the configuration file didn't match" obviously... – 0Pat Apr 14 '22 at 08:50
2

This error will occur if the configuration file of the hosting application of your WCF service does not have the proper configuration.

Remember this comment from configuration:

When deploying the service library project, the content of the config file must be added to the host's app.config file. System.Configuration does not support config files for libraries.

If you have a WCF Service hosted in IIS, during runtime via VS.NET it will read the app.config of the service library project, but read the host's web.config once deployed. If web.config does not have the identical <system.serviceModel> configuration you will receive this error. Make sure to copy over the configuration from app.config once it has been perfected.

atconway
  • 20,624
  • 30
  • 159
  • 229
2

I ran Visual Studio in Administrator mode and it worked for me :) Also, ensure that the app.config file which you are using for writing WCF configuration must be in the project where "ServiceHost" class is used, and not in actual WCF service project.

DfrDkn
  • 1,270
  • 2
  • 16
  • 23
2

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.

Here are the two steps I used to fix it:

  1. Use the correct fully-qualified class name:

    <service behaviorConfiguration="DefaultBehavior" name="EmailSender.Wcf.EmailService">
    
  2. Enable an endpoint with mexHttpBinding, and most importantly, use the IMetadataExchange contract:

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    
abatishchev
  • 98,240
  • 88
  • 296
  • 433
letsgetsilly
  • 1,156
  • 2
  • 11
  • 27
1

One crucial thing to remember for those working with a Console application to host the WCF service is that the Web.config file in the WCF project is completely ignored. If your system.serviceModel configuration is there, then you need to move that section of config to the App.config of your Console project.

This is in addition to the answers concerning ensuring the namespace is specified in the right places.

Neo
  • 4,145
  • 6
  • 53
  • 76
1

As another clue, that indeed fixed this issue in my case.

I'm migrating some WCF services from a console application (that configures in code few WCF services) to an Azure WebRole to publish them in Azure. Every time I add a new service VS edits my web.config and adds this line:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">

Well, with all the advices and answers above I couldn't make it work until I removed all the attributes in the serviceHostingEnvironment element. As you can see I'm not a WCF rockstar but I made it to work with the first Service just by configuring it as:

<service name="FirstService" behaviorConfiguration="metadataBehavior">
                <endpoint address=""
                 binding="wsHttpBinding"
                 bindingConfiguration="WSHttpBinding_WcfServicesBinding"
                 contract="IFirstService" />

            </service>

but when I added the second Service it stoped working and I realized that those attributes where there again.

I hope it saves you time.

Juan
  • 2,156
  • 18
  • 26
1

My problem was when I renamed my default Service1 class for .svc file to a more meaningful name, which caused web.config behaviorConfiguration and endpoint to correspond to old naming convention. Try to fix your web.config.

Michael
  • 11
  • 1
0

I had this error in a Windows Service when my WCF Service Library that I created was not connected for hosting, but was connected for connection. I was missing an endpoint. (I wanted both connection and hosting in my Windows Service so that I could serve up the WCF Service to other connections, as well as have the main process of my Windows Service use it as well to do various tasks on a timer/schedule.)

The fix was that I rightlcicked my App.config file and chose Edit WCF Configuration. Then, I did the steps for Create Service so that I could connect to my WCF Service. Now I had two endpoints in my App.config, not just one. One endpoint was for the connection to the WCF Service Library, and another was for the hosting of it.

Volomike
  • 23,743
  • 21
  • 113
  • 209
0

As others have mentioned, my problem was that I didn't have the system.serviceModel in my hosting application's app.config. For example:

<configuration>
  ...
  <system.serviceModel>
    <services>
      <service name="Services.MyServiceManager">
        <endpoint address="net.tcp://localhost:8009/MyService"
                  binding="netTcpBinding"
                  contract="Contracts.IMyService" />
      </service>
    </services>
  </system.serviceModel>
</configuration>
user8128167
  • 6,929
  • 6
  • 66
  • 79