WCF使用windows服务 [英] WCF using windows service

查看:20
本文介绍了WCF使用windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我转到管理控制台 (services.msc) 并启动服务.但我收到以下错误

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

LijosWindowsService 服务本地计算机启动然后停了下来.部分服务停止自动,如果他们没有工作做,例如,性能日志和警报服务

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

服务无法启动.System.InvalidOperationException:服务Lijo.Samples.WeatherService"零申请(非基础设施)端点.这个可能是因为没有配置文件已为您的应用程序找到,或因为没有服务元素匹配服务名称可以在配置文件,或者因为没有端点在服务中定义元素.

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?

文件名 [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.config 文件位于 (yourapplication).exe 所在的同一目录中吗?定位,您作为服务启动的那个?

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?

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

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.

这篇关于WCF使用windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆