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开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...
随机推荐
- 对比MySQL,什么场景MongoDB更适用
原文链接: http://page.factj.com/blog/p/4078 MongoDB已经流行了很长一段时间,相对于MySQL,究竟什么场景更需要用MongoDB?下面是一些总结. 更高的写入 ...
- 十分钟搭建个人网站:Jekyll主题BoHu
最近花了三天时间制作了我的第一个jekyll theme--BoHu.一款知乎风格的模板,使用jekyll模板引擎,十分钟就能搭建属于你自己的静态博客网站. 本主题的特征为: 知乎风格 分页导航使用的 ...
- 控制 WAP 网站上输入框的默认类型
比如手机号,卡输入框应该默认显示数字键盘,邮箱输入框应该默认显示邮箱键盘.www . c s d n 1 2 3 . com/html/itweb/20130802/36036_36043_36004 ...
- c#基础知识对比(面向对象)
private,protected,public和internal private:是完全私有的,只有本类自己能用[好比自己的老婆,只有你自己可以调用,其他谁都不可以] protected:可被外界看 ...
- Visual Assist安装、破解方法
1.从各种渠道下载Visual Assist安装程序及破解补丁. 2.在路径(Win7):C:\Users\UserName\AppData\Local\Microsoft\VisualStudio\ ...
- stop() 是用于停止动画 :animated 用于判断动画是否在进行中
stop() 是用于停止动画 if($("element").is(":animated")) 用于判断动画是否在进行中
- 【BZOJ】1015: [JSOI2008]星球大战starwar
1015: [JSOI2008]星球大战starwar 题意:一个点数为N(1<= 40w),边数为M(1<=20w)的图,总共删除k个节点,问开始以及每次删除一个节点之后图的连通块数? ...
- linux环境下验证码不显示的几种情况
linux环境下验证码不显示的几种情况 gd库扩展没有安装. 查看phpinfo(),看看有没有安装gd库 yum安装gd库或者phpize安装 安装完成后记得重启php-fpm bom头的原因 在生 ...
- DB天气app冲刺第七天
今天估计得分应该是最差的了. 今天因为完全没准备所以在界面UI上面根本就是相当于没弄.结果今天的各组报告里自己做得很不好.不过好在只是项目的中间期,不碍大事. 今天也完成了任务.可以说超额了也.因为吃 ...
- final, finally 和finalize的区别
final 修饰符(关键字) 如果一个类被声明为final,意味着它不能再派生新的子类,不能作为父类被继承.因此一个类不能及被声明为abstract,又被声明为final的.将变量或方法声明为fina ...