tags:

views:

71

answers:

2

I am creating a WCF service which is to be hosted in Windows Service. I created a console application as follows

I went to management console (services.msc) and started the service. But I got the following error

The LijosWindowsService service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service

I went to the event viewer and got the following

Service cannot be started. System.InvalidOperationException: Service 'Lijo.Samples.WeatherService' 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.

Could you please let me know what is the missing link here?

File name [LijosService.cs]

using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;

namespace Lijo.Samples
{
   [ServiceContract(Namespace = "http://Lijo.Samples")]
   public interface IWeather
   {
      [OperationContract]
      double Add(double n1, double n2);
   }

   public class WeatherService : IWeather
   {
       public double Add(double n1, double n2)
       {
           double result = n1 + n2;
           return result;
       }
   }

   public class MyWindowsService : ServiceBase
   {
       public ServiceHost serviceHost = null;

       public MyWindowsService()
       {
           // Windows Service name
           ServiceName = "LijosWindowsService";
       }

       public static void Main()
       {
           ServiceBase.Run(new MyWindowsService());
       }

       protected override void OnStart(string[] args)
       {
           if (serviceHost != null)
           {
               serviceHost.Close();
           }
           serviceHost = new ServiceHost(typeof(WeatherService));
           serviceHost.Open();
       }

       protected override void OnStop()
       {
           if (serviceHost != null)
           {
               serviceHost.Close();
               serviceHost = null;
           }
       }
   }

    // ProjectInstaller 
    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private ServiceProcessInstaller myProcess;
        private ServiceInstaller myService;

        public ProjectInstaller()
        {
            myProcess = new ServiceProcessInstaller();
            myProcess.Account = ServiceAccount.LocalSystem;

            myService = new ServiceInstaller();
            myService.ServiceName = "LijosWindowsService";

            Installers.Add(myProcess);
            Installers.Add(myService);
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Lijo.Samples.WeatherService"
               behaviorConfiguration="WeatherServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/ServiceModelSamples/LijosService"/&gt;
          </baseAddresses>
        </host>
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Lijo.Samples.IWeather" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WeatherServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Thanks

Lijo

A: 

Usually the Exception details are accurate and helpful (although they seem quite cryptic at first). It's a very common exception.

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.

The most likely answer is that your application cannot find the configuration file.

WCF in conjunction with Windows Services can be quite tricky to debug and setup. When I've had to do this, or perform other non-trivial tasks inside Windows Services, I usually write the project as a Console Application to check that everything is working as expected. It's then easy enough to move your tested code into a Windows Service.

Kirk Broadhurst
+2  A: 

Your config and code looks fine - are you sure that there is a (yourapplication).exe.config file in the same directory where (yourapplication).exe is located, the one you launch as your service?

The error message would indicate that config file is missing. Make sure it's there - otherwise your NT service cannot set up the WCF service as needed.

marc_s