【问题标题】:WCF using windows service使用 Windows 服务的 WCF
【发布时间】:2010-05-22 09:37:46
【问题描述】:

我正在创建要在 Windows 服务中托管的 WCF 服务。我创建了一个控制台应用程序如下

我去了管理控制台 (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>

谢谢

李乔

【问题讨论】:

  • 您的配置和代码看起来不错 - 您确定(yourapplication).exe 所在的同一目录中有一个 (yourapplication).exe.config 文件,您将其作为您的服务?
  • 谢谢....当我将 exe.config 放入所需的文件夹时,一切正常。

标签: wcf


【解决方案1】:

您的配置和代码看起来不错 - 您确定 (yourapplication).exe 所在的同一目录中有一个 (yourapplication).exe.config 文件,该文件是您作为服务启动的吗?

错误消息表明配置文件丢失。确保它在那里 - 否则您的 NT 服务无法根据需要设置 WCF 服务。

【讨论】:

    【解决方案2】:

    确保在 WCF 服务和 Windows 服务中指定相同的 app.config。如果这也不能帮助从 WCf 服务中删除 app.config,请在 Windows 服务中手动配置 aap.config 并将其复制到 WCf 服务。这肯定会解决你的问题...

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 1970-01-01
      • 2013-09-13
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多