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开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...
随机推荐
- asp:第三平台登陆
第三平台登陆接口申请网址: http://open.51094.com/ 文档: 第三方合作登录平台使用说明 为方便更多的开发朋友,本人特将当前市面上所有支持第三方联合登录的接口集为一体,以前需要多次 ...
- ios专题 - objc runtime 动态增加属性
objective-c中,有类别可以在不修改源码的基础上增加方法:近排在看别人的开源代码时,发现还可以动态增加属性.而且是在运行时,太牛B了. 使用运行时库,必须要先引入 objc/runtime.h ...
- 方法 :PHP开发环境搭建(phpstorm + xampp+mongodb)
phpstorm 安装下载 百度网盘资源 phpstorm 9.0.1(有序列号) http://pan.baidu.com/s/1kTvX0jl xampp 安装下载 ...
- [PR & ML 5] [Introduction] Decision Theory
- Java_log4j
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件,甚至是套接口服务器.事件记录器等:我们也可以控制每一条日志的输出格式: ...
- Entity Framework 插入数据 解决主键非自增问题
http://blog.csdn.net/educast/article/details/8632806 与Entity Framework相伴的日子痛并快乐着.今天和大家分享一下一个快乐,两个痛苦. ...
- python(一)入门
1.软件环境安装和配置 首先下载属于你的操作系统的对应的python安装包 2.傻瓜化下一步下一步 我直接勾选了配置python到path变量 然后完成 3.cmd命令行中测试一把 表示环境配置成功 ...
- mac下如何查看指定端口被谁占用并且杀死该进程
在本地部署 Web 应用时我有遇到过某网络端口已经被其他程序占用的情况,这时候就需要先退出占用该端口的进程,我们可以通过“终端”来实现结束占用某特定端口的进程 1.打开终端,使用如下命令: lsof ...
- ASP.NET MVC 搭建简单网站 --1.前端页面布局和基本样式实现
学技术这件事儿本来就是学习现有的东西,然后变成自己的,本文当然也是借鉴的别人的东西,写出来作为一个对知识的巩固. 1.网站用的是MVC模式,新建一个MVC项目,建立一个APP1Controller, ...
- Django环境搭建和项目创建
1.下载安装python 2.打开shell(windows下cmd),安装虚拟环境工具: "pip install virtualenv".(可以通过“python -m pi ...