WCF之Windows宿主(可安装成服务自动并启动)
WCF之Windows宿主(可安装成服务自动并启动)
- 创建解决方案WCFServiceDemo
- 创建WCF服务库(类库或WCF服务库)WCFService ,添加引用System.ServiceModel、System.Runtime.Serialization
图1:图2:
- 创建实体模型Book
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks; namespace WCFService.Models
{
[DataContract]
[Serializable]
public class Book
{
[DataMember]
public string Name { get; set; }
[DataMember]
public double Price { get; set; }
}
}
Book
- 创建实现类BookService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCFService.Models; namespace WCFService
{
public class BookService : IBookService
{
List<Book> list = new List<Book>();
public bool Add(string name, double price)
{
list.Add(new Book() { Name = name, Price = price });
return true;
} public List<Book> GetList()
{
return list;
}
}
}
BookService
- 创建接口IBookService(接口必须加上ServiceContract特性,方法必须加上OperationContract特性)
using System;
using System.ServiceModel;
namespace WCFService
{
[ServiceContract]
public interface IBookService
{
[OperationContract]
bool Add(string name, double price); [OperationContract]
System.Collections.Generic.List<WCFService.Models.Book> GetList();
}
}
IBookService
- 如图:
- 创建Windows服务宿主WindowsServiceHost ,添加引用System.ServiceModel、System.Runtime.Serialization
图3:图4:
- 修改Service1的属性
在Service1的设计界面中右击,选择“属性”,把其中的(Name)和ServiceName都改为BookServiceHost
编写代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using WCFService; namespace WindowsServiceHost
{
public partial class BookServiceHost : ServiceBase
{
ServiceHost _Host = new ServiceHost(typeof(BookService));
public BookServiceHost()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
_Host.Open();
} protected override void OnStop()
{
_Host.Close();
}
}
}
BookServiceHost
- 编辑WCF配置(WCF工具配置)
图5:
- 新增服务,弹出界面,由于该App.Config文件是我们新添加的一个配置文件,所以左边的服务项中是空的。点击右边的“新建服务...”弹出“新建服务元素向导”窗口,单击“浏览”按钮,选择Bin/Debug目录下Services.dll程序集中的Services.BookService服务。


- 添加终结点:终结点(endpoint)分别有:TCP、HTTP、命名管道、MSMQ、对等、元数据。










- 依次添加其他的通信模式的终结点(上面图示为:http通信模式基本Web服务操作性)模式选择见下图


- 下面依次演示添加其他通信模式的终结点
Http(高级Web服务互操作性)
图20: 图21:
图22:
到目前为止我们配置好了两个http通道下的两个终结点,但这两个终结点的地址我们都使用的是相对地址,它们是相对于当前ServiceHost地址,所以我们还需要配置当前ServiceHost的地址.
- 配置ServiceHost地址:
图23:
这样我们两个终结点算是配置完成了。
“自运行WCF服务”与“在IIS布运行WCF服务”不一样的是,“自运行WCF服务"除了可以使用Http方式发布WCF服务,可以使用TCP、命名管道和微软消息队列进行信息传输。
下面我们再配置两个终结点,一个是使用TCP通信模式,另一个使用命名管道通信模式。
TCP:
图24:图25:
命名管道:
图26:图27:
到此为至,我们已经为该WCF服务建立了四个数据传输的终结点
下面我们为该ServiceHost程序配置“元数据终结点”,以向客户端发送服务元数据信息
- 添加服务行为。在左侧配置中选择“高级”-“服务行为”,再点击右侧的“新建服务行为分配”,点击“添加”弹出“添加行为元素扩展部份”窗口,选择“serviceMetaData”
图28:图29:
- 为服务配置刚刚新建的行为。
图30:
- 配置元数据终结点
图31:图32:
图34:图33:
图35:图36:
图37:图38:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph=""/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior" name="WCFService.BookService">
<clear />
<endpoint address="basic" binding="basicHttpBinding" contract="WCFService.IBookService"
listenUriMode="Explicit">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="ws" binding="ws2007HttpBinding" contract="WCFService.IBookService"
listenUriMode="Explicit">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="net.tcp://localhost:8082/BookService" binding="netTcpBinding"
contract="WCFService.IBookService" listenUriMode="Explicit">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="net.pipe://localhost/BookService" binding="netNamedPipeBinding"
contract="WCFService.IBookService" listenUriMode="Explicit">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="mex" binding="basicHttpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/service" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
App.Config
然后把下面代码删掉:
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph=""/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior" name="WCFService.BookService">
<clear />
<endpoint address="basic" binding="basicHttpBinding" contract="WCFService.IBookService"
listenUriMode="Explicit">
</endpoint>
<endpoint address="ws" binding="ws2007HttpBinding" contract="WCFService.IBookService"
listenUriMode="Explicit">
</endpoint>
<endpoint address="net.tcp://localhost:8082/BookService" binding="netTcpBinding"
contract="WCFService.IBookService" listenUriMode="Explicit">
</endpoint>
<endpoint address="net.pipe://localhost/BookService" binding="netNamedPipeBinding"
contract="WCFService.IBookService" listenUriMode="Explicit">
</endpoint>
<endpoint address="mex" binding="basicHttpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/service" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
App.Config
以上所写不仅适用与Windows宿主,同时适用IIS、控制台,因此后面关于IIS以及控制台宿主的发布不再重复以上配置
- 为服务添加安装程序
在Service1设计界面中右击,选择“添加安装程序”
图40:图41:
图42:图43(此图网络引用):
- 开始安装
进入vs2012 开发命令提示,进入项目对应的盘符并进入exe所在文件夹,执行命令 :installutil WindowsServiceHost.exe
图44:
- 启动BookServiceHost服务
图45:
- 测试服务:打开IE,在地址栏中输入:http://localhost:8081/service出现下面的界面
图46:
图
在VS2008命令窗口中输入:wcftestclient http://localhost:8081/Service 出现下面的界面
47::
WCF之Windows宿主(可安装成服务自动并启动)的更多相关文章
- WCF之Windows宿主
WCF之Windows宿主(可安装成服务自动并启动) WCF之Windows宿主(可安装成服务自动并启动) 创建解决方案WCFServiceDemo 创建WCF服务库(类库或WCF服务库)WCFSer ...
- Windows 程序注册成服务的方法
Windows 程序注册成服务的方法 将windows 程序注册成服务这个是很多后台程序需要实现的功能,注册成服务后,你的程序就可以像windows 服务一样随系统启动,并且隐藏你的控制台界面.下面介 ...
- Spring Boot项目生成jar包,并在windows服务器中注册成服务,开机启动
背景: 使用Spring Boot开发的Web项目,打包生成了一个jar包,希望能部署在Windows服务器中 尝试: 1.Spring Boot生成的jar包,可以直接用java -jar运行,但是 ...
- windows bat发布成服务
删除服务 删除名为"WINDOWS MANAGEMONT INSTALLER"的系统服务 sc delete "windows managemont Installer& ...
- 批处理启动vm虚拟机服务 vm12启动无界面启动vm虚拟机系统 windows上如何操作服务 sc net启动关闭服务
windows(win10)批处理脚本 打开vm虚拟机的服务,并且开启无界面虚拟机 @echo off net start "vds" net start "VMAuth ...
- linux 下将tomcat注册成服务并开机启动
一.将startup.sh和shutdown.sh新建软连接到/usr/bin ln -s /usr/local/apache-tomcat-8.5.38/bin/startup.sh /usr/bi ...
- windows IIS安装php服务及配置
windows IIS安装php服务及配置 启动IIS服务 打开 "控制面板" => "程序" => "启用或关闭Window功能&quo ...
- Windows安装redis并将redis设置成服务
Redis 作为一种缓存工具,主要用于解决高并发的问题,在分布式系统中有着极其广泛的应用,Redis 本身是应用于 Linux/Unix 平台的(部署在服务器上边),官方并没有提供 Windows 平 ...
- Windows批处理以服务的方式启动解决思路(ShadowsockR注册成Windows Service)
我以ShadowsockR的server启动来解释: 由于这东西是python,如果要启动,可以写一个批处理(python.exe server.py)来启动,但是我观察发现启动的时候是附带pytho ...
随机推荐
- shell脚本之字符串运算的使用
字符串运算符 下表列出了常用的字符串运算符,假定变量 a 为 "abc",变量 b 为 "efg": 运算符 说明 举例 = 检测两个字符串是否相等,相等返回 ...
- loj 3102
题目大意: 给定 \(m\) 棵无向树\(\left\{T_{1}=\left(V_{1}, E_{1}\right), T_{2}=\left(V_{2}, E_{2}\right), \cdots ...
- Comet OJ - Contest #9 & X Round 3题解
传送门 \(A\) 咕咕 typedef long long ll; int a1,a2,n,d;ll res; int main(){ scanf("%d%d%d",&a ...
- 通过 frp 实现群晖的 drive 远端同步
通过 frp 实现 drive 同步 其实其他的也类似, 只是指定 tcp 的端口不一致就可以. frp 实现的外网端口映射"肥肠"方便. 也推广下 frp 的地址 frp-git ...
- 《挑战30天C++入门极限》理解C++面向对象程序设计中的抽象理论
理解C++面向对象程序设计中的抽象理论 很多书在一开始就开始学习josephus问题,为了让大家前面学起来较为容易我把前面涉及到此问题的地方都故意去掉了,现在我们已经学习过了结构体和类,所以放 ...
- Hadoop hadoop 之hdfs数据块修复方法
hadoop 之hdfs数据块修复方法: .手动修复 hdfs fsck / #检查集群的健康状态 hdfs debug recoverLease -path 文件位置 -retries 重试次数 # ...
- 蚂蚁金服财富技术部,诚招Java研发工程师。校招内推!!!
蚂蚁金服财富技术部,诚招Java研发工程师. 团队是蚂蚁金服财富技术部核心团队,支持亿级互联网交易清算,在这里不仅能学习到先进的互联网技术,也能了解许多终身受益的金融知识. 内推对象 2020届毕业生 ...
- 以前进行的程序安装创建了挂起的文件操作(SqlServer2000或SqlServer 2000 SP4补丁安装)
在安装SqlServer 2000或者SqlServer 2000 SP4补丁时常常会出现这样的提示,从而不能进行安装,即使重新启动了计算机,也还是会有同样的提示.在网上查了一下资料,原来是注册表里记 ...
- nodejs五子棋online游戏开发视频教程,客户端cocos creator js
开发的游戏是五子棋online,网络版的,服务端部分和客户端部分都在这个教程里面,可以看一下目录! 服务器nodejs游戏开发教程 使用Nodejs开发网络服务器 游戏服务端 ,cocos creat ...
- 2019年ArcGIS规划专业专项培训(四天)
2019年ArcGIS规划专业专项培训(四天) 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 第一天:GIS入门 第一章 GIS概述及其应 ...