WCF是Windows平台下程序间通讯的应用程序框架。整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择。wcf程序的运行需要一个宿主ServiceHost,我们可以选用控制台应用程序,也可以选择IIS寄宿,还可以选择windows 服务寄宿。相较与控制台程序,IIS,和Windows服务比较稳定。而且大家不会时不时的去重启下IIS下的网站,或者windows服务。

在IIS下寄宿Wcf

我们新建一个类库项目

在项目下添加一个ICalculator接口类,实现wcf 服务契约,操作契约接口

using System.ServiceModel;
namespace IISServices
{
[ServiceContract(Name = 'CalculatorService')]
public interface ICalculator
{
[OperationContract]
double Add(double x, double y); [OperationContract]
double Subtract(double x, double y); [OperationContract]
double Multiply(double x, double y); [OperationContract]
double Divide(double x, double y);
}
}

新建一个服务类CalculatorService,实现服务契约接口ICalculator

namespace IISServices
{
public class CalculatorService : ICalculator
{
public double Add(double x, double y)
{
return x + y;
} public double Subtract(double x, double y)
{
return x - y;
} public double Multiply(double x, double y)
{
return x * y;
} public double Divide(double x, double y)
{
return x / y;
}
}
}

添加一个文件,文件名为CalculatorService.svc就是我们用来寻找服务对外暴漏的入口。只需要添加一行代码就可以。当我们访问服务的时候IIS会寻找我们这个svc文件来找到我们提供的服务。

 <%@ServiceHost Service='IISServices.CalculatorService'%>

添加一个web.Config文件,添加system.serviceModel节点的配置信息。里面不需要配置我们访问服务的地址,因为IIS下我们网站的地址就是我们访问服务的地址。

<?xml version='1.0' encoding='utf-8' ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name='metadataBehavior'>
<serviceMetadata httpGetEnabled='true'/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration='metadataBehavior' name='IISServices.CalculatorService'>
<endpoint binding='wsHttpBinding' contract='IISServices.ICalculator' />
</service>
</services>
</system.serviceModel>
</configuration>

项目详细如下,另外应用里面需要添加System.ServiceModel这个dll引用,wcf的大部分实现都在这个类库里面:

我们在IIS下面新建一个网站,根目录只需要添加web.Config,svc服务文件即可,bin下面放我们生成的IISServices.dll如下:

网站访问端口我们配置为82,启动网站。

在我们需要引用服务的类库或exe上添加服务引用http://localhost:82/CalculatorService.svc,就可以找到我们需要的服务了。

在Windows服务下寄宿wcf服务

我们新建一个控制台应用程序Service。添加下面这三个类库引用

System.ServiceModel.dll

System.ServiceProcess.dll

System.Configuration.Install.dll

将Programs.cs修改为Service.cs,添加代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install; namespace Microsoft.ServiceModel.Samples
{
// Define a service contract.
[ServiceContract(Namespace = 'http://Microsoft.ServiceModel.Samples')]
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);
} // Implement the ICalculator service contract in a service class.
public class CalculatorService : ICalculator
{
// Implement the ICalculator methods.
public double Add(double n1, double n2)
{
double result = n1 + n2;
return result;
} public double Subtract(double n1, double n2)
{
double result = n1 - n2;
return result;
} public double Multiply(double n1, double n2)
{
double result = n1 * n2;
return result;
} public double Divide(double n1, double n2)
{
double result = n1 / n2;
return result;
}
} public class CalculatorWindowsService : ServiceBase
{
public ServiceHost serviceHost = null;
public CalculatorWindowsService()
{
// Name the Windows Service
ServiceName = 'WCFWindowsServiceSample';
} public static void Main()
{
ServiceBase.Run(new CalculatorWindowsService());
} // Start the Windows service.
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
} // Create a ServiceHost for the CalculatorService type and
// provide the base address.
serviceHost = new ServiceHost(typeof(CalculatorService)); // Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
} protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
} // Provide the ProjectInstaller class which allows
// the service to be installed by the Installutil.exe tool
[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);
}
}
}

在App.Config里面添加配置节点ServiceModel如下:

 <system.serviceModel>
<services>
<!-- This section is optional with the new configuration model
introduced in .NET Framework 4. -->
<service name='Microsoft.ServiceModel.Samples.CalculatorService'
behaviorConfiguration='CalculatorServiceBehavior'>
<host>
<baseAddresses>
<add baseAddress='http://localhost:8000/ServiceModelSamples/service'/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service -->
<endpoint address=''
binding='wsHttpBinding'
contract='Microsoft.ServiceModel.Samples.ICalculator' />
<!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address='mex'
binding='mexHttpBinding'
contract='IMetadataExchange' />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name='CalculatorServiceBehavior'>
<serviceMetadata httpGetEnabled='true'/>
<serviceDebug includeExceptionDetailInFaults='False'/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

生成文件,注意看程序的入口main函数ServiceBase.Run(new CalculatorWindowsService());在main函数里面程序调用了windows服务。我们这个项目本质上是一个windows服务。windows服务不可以直接运行,需要先安装。我们使用管理员身份打开cmd.exe

进入我们的.net安装目录,我这个是安装的4.0,所以进入C:WindowsMicrosoft.NETFramework644.0.30319文件夹,找到该文件夹下面的InstallUtil.exe程序。在cmd里面输入如下,回车

进入.net安装文件夹,输入InstallUtil.exe '生成服务所在的路径',注意这儿最好给路径加上引号,没有引号如果碰到空格可能报错。

安装后如果出现下面的successfully,说明服务安装完成。

然后我们到windows服务里面启动该服务。

启动服务后,在需要引用该服务的类库或者exe程序上添加服务引用,路径为我们App.config里面的 <add baseAddress='http://localhost:8000/ServiceModelSamples/service'/>基地址

点击转到,出现CalculatorService说明我们配置成功。

注:

安装 Windows 服务: installutil binservice.exe 

卸载Windows服务: installutil /u binservice.exe 或者 sc delete 服务名

启动Windows服务: net start WCFWindowsServiceSample

停止Windows服务: type net stop WCFWindowsServiceSample

windows服务的寄宿,参考msdn:https://msdn.microsoft.com/zh-cn/library/ms733069.aspx

本文地址:http://www.cnblogs.com/santian/p/4397235.html

WCF服务寄宿IIS与Windows服务 - C#/.NET的更多相关文章

  1. WCF服务寄宿IIS与Windows服务

      WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的 ...

  2. 第三节:Windows平台部署Asp.Net Core应用(基于IIS和Windows服务两种模式)

    一. 简介 二. 文件系统发布至IIS 三. Web部署发布至IIS 四. FTP发布至IIS 五. Windows服务的形式发布 ! 作       者 : Yaopengfei(姚鹏飞) 博客地址 ...

  3. 如何使用windows云服务器搭建IIs、windows服务

    如何使用windows云服务器搭建IIs.windows服务,以下针对腾讯云服务器进行说明 1.购买云服务器之后,第1步需要设置的是,找到重装系统.重置密码等处. 2.设置安全组,设置完安全组之后才能 ...

  4. MongoDB配置服务--MongoDB安装成为windows服务

    MongoDB安装成为windows服务 1.打开命令提示符(最好以管理员的身份打开),然后输入: mongod --logpath "D:\MongoDB\data\log\logs.tx ...

  5. Windows服务一:新建Windows服务、安装、卸载服务

    Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面 ...

  6. [Solution] Microsoft Windows 服务(1) C#创建Windows服务

    Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而 ...

  7. mpush 服务端配置 for windows 服务自动运行

    mpush 服务端配置 以下安装部分是参照官方的步骤, 一.安装jdk1.8并配置环境变量 示例:  http://www.cnblogs.com/endv/p/6439860.html 二.Wind ...

  8. windows服务搭建(VS2019创建Windows服务不显示安装组件)

    1.创建windows服务应用 2.右键查看代码 3.写个计时器Timer  using System.Timers; 如上图,按tab键快速操作  会自动创建一个委托 改为下边的方式,打印日志来记录 ...

  9. WCF寄宿控制台.WindowsService.WinFrom.WebAPI寄宿控制台和windows服务

    先建立wcf类库.会默认生成一些试用代码.如下: public class Service1 { public string GetData(int value) { return string.Fo ...

随机推荐

  1. orm查询存在价格为空问题

    明明写的没错还是查不到 打印一下sql语句: 解决办法: 把数字变成字符串格式 所以涉及金融计算,涉及小数啊,要求特别精确的,我们用字符串存储.

  2. javascript对文件的读写

    整合了一下网上对于js实现文件读写的代码,但是该功能只能在ie浏览器下执行,另外有些电脑上的ie需要设置. 下面是写入代码: var fso = new ActiveXObject("Scr ...

  3. HDU - 1875 畅通工程再续【最小生成树】

    Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖 ...

  4. bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群 最小点覆盖

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1741 思路 消除所有的小行星 每个点(x,y)只有选择x或者y才能被覆盖 二分图最小点覆盖= ...

  5. 【做题】BZOJ2342 双倍回文——马拉车&并查集

    题意:有一个长度为\(n\)的字符串,求它最长的子串\(s\)满足\(s\)是长度为4的倍数的回文串,且它的前半部分和后半部分都是回文串. \(n \leq 5 \times 10^5\) 首先,显然 ...

  6. FZU 2150 Fire Game(点火游戏)

    FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description - 题目描述 ...

  7. go 一波走起

    $ go run helloworld.go 运行 $ go build helloworld.go 编译 该命令生成一个名为helloworld的可执行的二进制文件,可以随时运行它 $ ./hell ...

  8. WinForm 拖动、移动窗体

    private const int WM_NCLBUTTONDOWN = 0XA1; private const int HTCAPTION = 2; [System.Runtime.InteropS ...

  9. matplotlib.transforms

    来自:龙哥盟飞龙 变换教程 像任何图形包一样,matplotlib建立在变换框架之上,以便在坐标系,用户数据坐标系,轴域者坐标系,图形坐标系和显示坐标系之间轻易变换.在95%的绘图中,你不需要考虑这一 ...

  10. [osg]osg窗口显示和单屏幕显示

    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cow.osg"); osg::ref_ptr&l ...