WCF之Windows宿主(可安装成服务自动并启动)

WCF之Windows宿主(可安装成服务自动并启动)

  • 创建解决方案WCFServiceDemo
  • 创建WCF服务库(类库或WCF服务库)WCFService  ,添加引用System.ServiceModel、System.Runtime.Serialization

图1:图2: 

  • 创建实体模型Book
 Book
  • 创建实现类BookService
 BookService
  • 创建接口IBookService(接口必须加上ServiceContract特性,方法必须加上OperationContract特性)
 IBookService
  • 如图:

  • 创建Windows服务宿主WindowsServiceHost ,添加引用System.ServiceModel、System.Runtime.Serialization            

图3:图4:

  • 修改Service1的属性

    在Service1的设计界面中右击,选择“属性”,把其中的(Name)和ServiceName都改为BookServiceHost

  编写代码:

 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="2147483647"/>
<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>

然后把下面代码删掉:

<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="2147483647"/>
<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>

以上所写不仅适用与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下载

WCF之Windows宿主的更多相关文章

  1. WCF之Windows宿主(可安装成服务自动并启动)

    WCF之Windows宿主(可安装成服务自动并启动) 创建解决方案WCFServiceDemo 创建WCF服务库(类库或WCF服务库)WCFService  ,添加引用System.ServiceMo ...

  2. Windows Communication Foundation (WCF)和Windows CardSpace的示例程序

    微软公司昨天发布了一个Windows Communication Foundation (WCF)和Windows CardSpace的示例程序包,内容极为丰富,从最简单的Hello World到复杂 ...

  3. [Docker] Windows 宿主环境下,共享或上传文件到容器的方法

    需求如题. 解决方案1 - 挂载目录(适用于创建新的容器) 格式-v 容器目录 或 -v 本地目录:容器目录 范例Linux宿主环境下:使用镜像 nginx:latest,以后台模式启动一个容器,将容 ...

  4. WCF之Host宿主

    Self_hosting自托管宿主. 过程:手动创建Host实例,把服务端点添加到Host实例上,把服务接口与Host关联. 一个Host只能指定一个服务类型,但是可以添加多个服务端点,也可以打开多个 ...

  5. wcf类库及宿主

    说起wcf,一直以来总是直接创建wpf的应用程序,这样默认的宿主是IIS.如果想更换宿主,那么我们首先得创建wcf类库. 这个类库会自动创建一个app.config文件.到最后部署的时候,把它移到宿主 ...

  6. 使用Winform程序作为WCF服务的宿主

    如果我们自己新建一个WCF服务库,生成了dll文件.那我们需要创建一个宿主程序,在本例中我们新建一个Winform程序作为WCF的宿主程序. 在网上很多教程里对创建过程写的很模糊,错误也很多.本文是作 ...

  7. 在Linux客户机与Windows宿主机之间建立共享(VitrualBox)

    VirtualBox中,如果客户机和宿主机都是Windows的话,共享相对是比较方便的.一般是通过\\vboxsvr\shared 这样的路径访问即可. 但是如果客户机是Linux的话,就略微麻烦一点 ...

  8. 在Windows宿主机中连接虚拟机中的Docker容器

    1. 简单拓扑图

  9. WCF 与 Windows Store Client App

    首先复习下WCF: WCF实际上是构建了一个框架,这个框架实现了在互联系统中各个Application之间如何通信.使得Developers和Architect在构建分布式系统中,无需在考虑如何去实现 ...

随机推荐

  1. python——使用xlwing库进行Excel操作

    Excel是现在比不可少的数据处理软件,python有很多支持Excel操作的库,xlwing就是其中之一. xlwings的安装 xlwings库使用pip安装: 在控制台输入 pip instal ...

  2. Linux——安装并配置Kafka

    前言 Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 这种动 ...

  3. Windows环境下设置Tomcat8以服务的形式运行,不再打开Tomcat窗口

    内容简介 在Windows操作系统下,设置Tomcat8以服务的形式运行,按照以下3步来操作即可.前提条件:已安装好Java环境,并配置好java的环境变量:已下载好Tomcat8并解压到某目录. s ...

  4. C++ socket bind() 函数绑定错误

    VS2015编译错误: errorCxxxx: 'initializing' : cannot convert from 'std::_Bind<false,void,SOCKET&,s ...

  5. H5利用formData来上传文件(包括图片,doc,pdf等各种格式)方法小结!

    H5页面中我们常需要进行文件上传,那么怎么来实现这个功能呢??? 我主要谈如下两种方法. (一).传统的form表单方法 <form action="/Home/SaveFile1&q ...

  6. NET PDB文件到底包含多少秘密?

    虽然我希望.NET PDB文件与本地PDB文件处理方式相同,但我们在这件事上没有任何选择,因为事情就是这样.我相信微软的调试器团队多年来听到过很多类似帕特里克的评论.也许我们会在未来的Visual S ...

  7. I Count Two Three(打表+排序+二分查找)

    I Count Two Three 二分查找用lower_bound 这道题用cin,cout会超时... AC代码: /* */ # include <iostream> # inclu ...

  8. git dev分支合并线上master

    1.本地dev新建/切换本地master 新建 git checkout -b master 切换 git checkout  master 2.本地dev与本地master合并 git merge ...

  9. 修改elementUI源码新增组件/修改组件

    前言 经常我们会遇到elementUI组件库期间有5%达不到我们想要的需求,第一我们重新写组件,第二我们改源码 安装element https://github.com/ElemeFE/element ...

  10. mysql pi() 获取pi

    mysql> select pi(); +----------+ | pi() | +----------+ | 3.141593 | +----------+ row in set (0.00 ...