WCF - Windows Service Hosting

來源:互聯網
上載者:User

標籤:

WCF - Windows Service Hosting

The operation of Windows service hosting is a simple one. Given below are the steps with requisite coding and screenshots that explain the process in an easy way.

在windows服務上託管wcf是一個簡單的操作。

Step-1: Now let’s create a WCF service. Open Visual Studio 2008 and click New → Project and select Class Library from the template.

 

Step-2: Add reference System.ServiceModel to the project. This is the core assembly used for creating the WCF service.

 

Step-3: Next, we can create the ISimpleCalculator interface. Add the Service and Operation Contract attribute as shown below:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.ServiceModel;namespace WCFHostedWindowsService{    [ServiceContract]    interface ISimpleCalculator    {        [OperationContract]        int Sum(int number1, int number2);        [OperationContract]        int Subtract(int number1, int number2);        [OperationContract]        int Multiply(int number1, int number2);        [OperationContract]        double Divide(int number1, int number2);    }}

 

Step-4: Implement the ISimpleCalculator interface as shown below:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace WCFHostedWindowsService{    class SimpleCalculator : ISimpleCalculator    {        public int Sum(int number1, int number2)        {            return number1 + number2;        }        public int Subtract(int number1, int number2)        {            return number1 - number2;        }        public int Multiply(int number1, int number2)        {            return number1 * number2;        }        public double Divide(int number1, int number2)        {            if (number2 != 0)            {                return number1 / number2;            }            else            {                return 0;            }        }    }}

 

Step-5: Build the Project and get the dll. Now, we are ready with the WCF service. We are going to see how to host the WCF service in Windows service.

Note: In this project, it is mentioned that we are creating both Contract and Service (implementation) in the same project. However it is always a good practice if you have both in different projects.

 

Step-6: Open Visual Studio 2008 and Click New → Project and select Windows Service.

 

Step-7: Add ‘WindowsServiceHostedService.dll‘ as reference to the project. This assembly is going to act as service.

 

Step-8: The OnStart method of the service can be used to write the hosting code for WCF. We have to make sure that we are using only one service host object. OnStop method is used to close the Service Host. The following code shows how to host the WCF service in Windows service.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.ServiceModel;using System.ServiceModel.Description;using System.ServiceProcess;using WindowsServiceHostedService;namespace WCFHostedWindowsService{    class WCFHostedWindowsService : ServiceBase    {        ServiceHost serviceHost;        public WCFHostedWindowsService()        {            InitializeComponent();        }        protected override void OnStart(string[] args)        {            if (serviceHost != null)            {                serviceHost.Close();            }            //Create a URI to serve as the base address            Uri httpUrl = new Uri("http://localhost:8090/WindowsServiceHostedService/SimpleCalculator");            //Create ServiceHost            serviceHost = new ServiceHost(typeof(SimpleCalculator), httpUrl);            //Add a service endpoint            serviceHost.AddServiceEndpoint(typeof(ISimpleCalculator), new WSHttpBinding(), "");//ISimpleCalulator  //ISimpleCalulator            //Enable metadata exchange            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();            smb.HttpGetEnabled = true;            serviceHost.Description.Behaviors.Add(smb);            //Start the Service            serviceHost.Open();        }        protected override void OnStop()        {            if (serviceHost != null)            {                serviceHost.Close();                serviceHost = null;            }        }        /// <summary>         /// 必需的設計器變數。        /// </summary>        private System.ComponentModel.IContainer components = null;        /// <summary>        /// 清理所有正在使用的資源。        /// </summary>        /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param>        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region 組件設計器產生的程式碼        /// <summary>         /// 設計器支援所需的方法 - 不要        /// 使用代碼編輯器修改此方法的內容。        /// </summary>        private void InitializeComponent()        {            components = new System.ComponentModel.Container();            this.ServiceName = "Service1";        }        #endregion    }}

 

Step-9: In order to install the service, we need to have the Installer class for the Windows service. So add a new Installer class to the project, which is inherited from the Installer class. Given below is the code that shows the Service name, StartUp type, etc. of the service.

 

Step-10: Build the project to get the executable file WCFHostedWindowsService.exe. Next, we need to install the service using the Visual Studio Command Prompt.

So open the command prompt by clicking Start→All Programs→Microsoft Visual Studio 2008→Visual Studio Tools→ Visual Studio Command Prompt. Using the install util utility application, you can install the service as shown below.

WCF - Windows Service Hosting

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.