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 != )
{
return number1 / number2;
}
else
{
return ;
}
}
}
}
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的更多相关文章
- WCF Windows Service Using TopShelf and ServiceModelEx z
http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...
- 宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)
1. 创建WCF服务 在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1) IService1接口如下: using System.Ru ...
- WCF寄宿到Windows Service
WCF寄宿到Windows Service[1] 2014-06-14 WCF寄宿到Windows Service参考 WCF寄宿到Windows Service 返回 在前面创建一个简单的WCF程序 ...
- WCF寄宿到Windows Service[1]
WCF寄宿到Windows Service 返回 在前面创建一个简单的WCF程序,我们把WCF的服务寄宿到了Host这个控制台项目中了.下面将介绍如何把WCF的服务寄宿到Windows服务中(源代码) ...
- 客户端通过wcf来启动或者停止服务器上的windows service
1.设置服务器上的windows service的security,下面的命令只能用cmd.exe来运行(以管理员模式) sc sdset "LISA_43_Dev_Batch" ...
- [WCF] - 使用 bat 批处理文件将 WCF 服务部署为 Windows Service
1. 添加 Windows Service 项目 2. 添加 WCF 项目引用 3. 更新 App.config 配置文件(可以从 WCF的 Web.config 拷贝过来),设置服务地址. 4. 配 ...
- (WCF) WCF Service Hosting.
3 Options. 1. Host inside of an application. 2. Host into Windows service. 3. Host into IIS 参考: http ...
- windows service宿主web api使用"依赖注入"和“控制反转”的技术实践
前言 自从几年前抛弃wcf,使用web api 来做服务器端开发之后,就不再迷惑了.但是因为本来从事传统行业管理软件开发,一般都以分布式应用开发为主.纯BS还是比较少,于是比较喜欢用windows s ...
- 如何托管ASP.NET Core应用到Windows Service中
(此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...
随机推荐
- ###Markdown初步学习
Markdown学习/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Author: Nicolas Hery - ...
- HTML5 Canvas 绘制时钟
网上会看到很多绘制的时钟,看代码也是云里雾里,自学了下Canvas,觉得不难,就自己做了一个. 先看一下截图: 比较简陋,但是该有的都有了,样式只加了个阴影. html代码就不贴了,就一个canvas ...
- 利用C语言获取设备的MAC address
利用C语言获取设备的MAC address MAC address --> Medium Access Control layer address // // http://www.binary ...
- JavaScript 作用域和作用域链
作用域是JavaScript最重要的概念之一,想要学好JavaScript就需要理解JavaScript作用域和作用域链的工作原理.今天这篇文章对JavaScript作用域和作用域链作简单的介绍,希望 ...
- jquery图片无缝滚动代码左右 上下无缝滚动图片
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 微信公众号-5秒内不回复测试并处理方案,顺便复习php 时间执行
在index.php中 file_put_contents('has_request.txt','请求时间:'.date('YmdHis')."\n",FILE_APPEND); ...
- 写 一个PHP脚本遇到的问题总结
在项目中,因为之前的人员,基础数据没有处理好,后面需要写一个脚本来处理这个问题,经验少,总结如下: 1.在linux下直接连接跑处理MySQL数据的脚本,要用PDO的方式连接数据库,长时间在框架中处理 ...
- JavaScript中的运算符种类及其规则介绍
JavaScript中的运算符有很多,主要分为算术运算符,等同全同运算符,比较运算符,字符串运算符,逻辑运算符,赋值运算符等.这些运算符都有一些属于自己的运算规则,下面就为大家介绍一下JavaScri ...
- phpmyadmin 4.x 版本无法看到登录框的处理
由于个人dreamhost即将到期问题,购买了一台VPS. 配置了一个CentOS 6.4 Linux 服务器,用Nginx+php-fpm搭建的环境. 这些都是废话,下面是重点: 当搭建后配置php ...
- IOS中如何判断APP是否安装后首次运行或升级后首次运行
对于是否为首次安装的App可以使用如下方法来判断 [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"] ...