WCF使用Windows服务

3
我正在创建一个 WCF 服务,它将托管在 Windows 服务中。我创建了以下控制台应用程序。
我打开管理控制台(services.msc)并启动了该服务。但是我收到了以下错误:
“LijosWindowsService”服务在本地计算机上启动,然后停止。例如,性能日志和警报服务等一些服务会自动停止,如果它们没有工作要做。
我转到事件查看器并得到以下信息:
服务无法启动。System.InvalidOperationException:服务“Lijo.Samples.WeatherService”具有零个应用程序(非基础结构)端点。这可能是因为找不到应用程序的配置文件,或者因为在配置文件中找不到与服务名称匹配的服务元素,或者因为在服务元素中未定义端点。
请问这里缺少什么?
文件名[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>

谢谢

Lijo


2
你的配置和代码看起来没问题 - 你确定在启动服务时,与 (yourapplication).exe 文件位于同一目录下的 (yourapplication).exe.config 文件存在吗? - marc_s
1
谢谢…当我把exe.config文件放到所需的文件夹中时,一切都正常运作。 - Lijo
2个回答

6
你的配置和代码看起来没问题 - 你确定在启动服务的目录下有一个(yourapplication).exe.config文件吗?错误信息表明缺少配置文件。确保它存在 - 否则你的NT服务无法按需要设置WCF服务。

0

请确保在WCF服务和Windows服务中指定相同的app.config。如果这仍然无法解决问题,请从WCF服务中删除app.config,手动配置aap.config并将其复制到WCF服务中。这肯定会解决您的问题...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接