在windows服务中寄宿wcf服务

在windows服务中寄宿wcf服务,需要继承ServiceBase,此外,还须要继承Installer以安装服务.以下为具体实现步骤:
1.创建文件winservice.cs,输入代码
namespace windowswcfservice
{
    using System.ServiceProcess;
    using System.ServiceModel;
    using System.Configuration.Install;
    using System.Configuration;
    using System.ComponentModel;

    [ServiceContract(Namespace="http://mysample")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }
    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    }
    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;
        public ProjectInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            service = new ServiceInstaller();
            service.ServiceName = "WCFWindowsServiceSample";
            Installers.Add(process);
            Installers.Add(service);
        }
    }

    public class WindowsCalculatorService : ServiceBase
    {
        public ServiceHost serviceHost = null;
        public static void Main()
        {
            ServiceBase.Run(new WindowsCalculatorService());
        }
        protected override void OnStart(string[] args)
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
            }
            try
            {
                serviceHost = new ServiceHost(typeof(CalculatorService));
                serviceHost.Open();
            }
            catch(System.Exception err)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", err.Message);
            }
        }

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

2.用csc编译文件winservice.cs
csc /t:exe winservice.cs /r:"C:/WINDOWS/Microsoft.NET/Framework/v3.0/Windows Communication Foundation/System.ServiceModel.dll" /r:C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/System.ServiceProcess.dll /r:System.Configuration.Install.dll

3.创建winservice.exe.config,内容如下:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
    <services>
    <service name="windowswcfservice.CalculatorService" behaviorConfiguration="Metadata" >
    <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8000/service"/>
        </baseAddresses>
    </host>
    <endpoint address="*" binding="wsHttpBinding" contract="windowswcfservice.ICalculator" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Metadata">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
 </system.serviceModel>
</configuration>
4.利用工具C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/installutil.exe安装windows 服务.
   installutil.exe winservice.exe
5.启动服务:net start WCFWindowsServiceSample
如果服务不能启动,可查看事件日志以定位错误.
6.访问wcf服务:http://localhost:8000/service
7.停止wcf服务:net stop WCFWindowsServiceSample
8.卸载windows服务:installutil.exe /u winservice.exe

更多>> http://horsehill.blog.sohu.com/95363212.html

http://msdn.microsoft.com/zh-cn/library/ms733069.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值