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服务。
图6:图7:
  • 添加终结点:终结点(endpoint)分别有:TCP、HTTP、命名管道、MSMQ、对等、元数据
图8:图9:
图10:图11:
图12:图13
图14:图15: 
图16:图17:
  • 依次添加其他的通信模式的终结点(上面图示为:http通信模式基本Web服务操作性)模式选择见下图
图18:图19:
  • 下面依次演示添加其他通信模式的终结点

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:

最终页面:
到目前为止我们已经将Window服务宿主配置完毕,现在保存,关闭WCF配置工具,并打开App.Config
<?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>
最终的App.config
<?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:

图46:

在VS2008命令窗口中输入:wcftestclient http://localhost:8081/Service 出现下面的界面

47:

Demo下载

最新文章

  1. include包含头文件的语句中,双引号和尖括号的区别是什么?
  2. 修改Apache配置文件开启gzip压缩传输
  3. SNMP-配置文件详解
  4. ASP.NET MVC3 Dynamically added form fields model binding
  5. DataTable 中Distinct操作
  6. Java Collections Source Code Series 2 ---接口
  7. Autolayout环境设置任意个数相等间距排列的按钮的方法
  8. 在oj平台上练习的一些总结【转】
  9. 初识phaser框架——开源的HTML5 2D游戏开发框架
  10. SGU 135.Drawing Lines
  11. 各个Maven仓库镜像(包括国内)
  12. Git 常用命令汇总
  13. 最强PostMan使用教程(1)
  14. windows端运行.sh脚本
  15. vim配置文件.vimrc
  16. PHP保留两位小数并且四舍五入及不四舍五入的方法
  17. 加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用
  18. 批量解帧视频文件cpp
  19. 最近比赛中遇到的几道dp题
  20. jmeter 多线程组间变量共享

热门文章

  1. 悬线法DP总结
  2. CODE FESTIVAL 2016 qual C题解
  3. 《挑战30天C++入门极限》C++运算符重载转换运算符
  4. BFC、IFC、FFC、GFC
  5. &lt;ImageFieldFile:XXXX&gt; is not JSON serializable
  6. H2数据库函数及数据类型概述
  7. 干货满满!10分钟看懂Docker和K8S(转)
  8. Java回调机制在RPC框架中的应用示例
  9. SqStack进制计算
  10. 全面系统Python3入门+进阶-1-7 课程内容与特点