在windows服務中寄宿wcf服務

來源:互聯網
上載者:User

在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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.