Lijo
Lijo

Reputation: 375

WCF using windows service

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"/>
          </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

Upvotes: 3

Views: 2257

Answers (2)

M Jun
M Jun

Reputation: 19

Make Sure you specify same app.config both in WCF service and Windows Service . If this also does not help delete app.config from WCf service , configure aap.config manually in windows service and copy it to WCf service . This will surely solve your problem...

Upvotes: 0

marc_s
marc_s

Reputation: 755297

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.

Upvotes: 6

Related Questions