WCF学习之旅—WCF服务的批量寄宿(十三)
WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)
九、 无svc文件服务激活的原理:
在WCF4.0里,通过提供一种虚拟的服务类型映射机制来实现WCF服务的激活。我们可以在配置文件里指定服务类型和相对地址之间的映射关系。这就使得我们可以在不是要.svc文件的情况下,在WAS/IIS里托管WCF服务程序。
1) 关于服务激活,这里一个重要的配置元素就是serviceActivation。我们可以定义服务类型和相对地址之间的映射关系。在配置文件里serviceActivations节点属于serviceHostingEnvironment>。一个简单的服务类型和相对地址之间的映射如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment> <serviceActivations>
<add relativeAddress="BookService.svc" service="WcfServiceLib.BookService"/>
</serviceActivations>
</serviceHostingEnvironment> </system.serviceModel>
<system.web>
<compilation defaultLanguage="c#" />
</system.web>
</configuration>
使用这个配置,我们就可以在WCF4.0里,使用http的方式,无svc文件激活BookService。注意<serviceHostingEnvironment>属于一个应用程序级别的配置。我们必须把它放置在<system.serviceModel>
节点下。此外,serviceHostingEnvironment继承自machinetoApplication。如果我们在machine注册单个服务,程序里的每个服务必须继承该服务。
这种通过配置设置的激活映射,支持http和非http协议。我们需要在相对地址relatativeAddress 里扩展文件名,例如.svc、.xoml 或.xamlx。我们也可以定义自己的处理扩展组件,然后在这里配置,那么WCF也会做类似的映射。为了避免冲突,我们在配置文件里定义的<serviceActivations>会代替svc的内容。也就是配置文件的设置优先级会比较高。
2)
IIS部署:部署方式同本文WCF服务部署到IIS7.5。
- 指定网站的ASP.NET的版本,这里注意版本为4.0,默认的版本是2.0。
网站ASP.NET的版本配置如图所示:

3) 这里直接启动浏览器,可以在浏览器里查看到服务的信息。如果启用服务元数据页面,可以查看到服务的WSDL信息。页面如下:

十、批量寄宿
(1) 在解决方案下新建控制台输出项目 BatHosting。

(2)添加 System.ServiceModel.dll 的引用。
(3)添加 WCF 服务类库(WcfServiceLib)的项目引用。
(4) 创建配置文件,在配置文件中添加两个配置项
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="RestWebBinding">
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8888/BookService/metadata" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
<behavior name="RestServiceBehavior">
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RestWebBehavior">
<!--这里必须设置--> <webHttp />
</behavior> </endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metadataBehavior" name=" WcfServiceLib.BookService">
<endpoint address="http://127.0.0.1:8888/BookService" binding="wsHttpBinding" contract=" WcfServiceLib.IBookService" />
</service>
<service name=" WcfServiceLib.BookRestService" behaviorConfiguration="RestServiceBehavior">
<endpoint address="http://127.0.0.1:8888/" behaviorConfiguration="RestWebBehavior" binding="webHttpBinding" bindingConfiguration="RestWebBinding" contract=" WcfServiceLib.IBookRestService"> </endpoint>
</service>
</services>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings> <add name="Entities" connectionString="metadata=res://*/BookModel.csdl|res://*/BookModel.ssdl|res://*/BookModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=Test;integrated security=SSPI;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
</configuration>
(5)创建宿主程序,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using WcfServiceLib; namespace BatHosting
{
class Program
{
/// <summary>
/// 批量寄宿
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{ try
{
Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location); ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
foreach (ServiceElement el in svcmod.Services.Services)
{ Type svcType = Type.GetType(el.Name + "," + "SCF.WcfService");
if (svcType == null)
Console.WriteLine("WCF Service Type " + el.Name + " 在配置文件中的名称.");
ServiceHost host = new ServiceHost(svcType);
host.Opened += delegate
{
Console.WriteLine(string.Format("{0},使用配置文件,批量寄宿!",svcType.ToString())); }; host.Open();
Console.ForegroundColor = ConsoleColor.Yellow; foreach (ServiceEndpoint se in host.Description.Endpoints)
{
Console.WriteLine("[终结点]: {0}\r\n\t[A-地址]: {1} \r\n\t [B-绑定]: {2} \r\n\t [C-协定]: {3}",
se.Name, se.Address, se.Binding.Name, se.Contract.Name);
}
}
Console.Read();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); }
}
}
}
(5)运行宿主程序,在运行客户端进行调用之前,需要先运行宿主程序。如下图所示,则说明宿主建立成功。

十一、总结
通过上面的几个例子,我们实现了控制台宿主、Form宿主、IIS寄宿、WAS宿主(基于TCP协议)、批量寄宿等的实现。在实际的开发过程中,我们大部份都使用IIS做宿主,方便、快捷;有时候我们还会用到基于Windows服务的宿主。
WCF学习之旅—WCF服务的批量寄宿(十三)的更多相关文章
- WCF学习之旅—WCF服务部署到IIS7.5(九)
上接 WCF学习之旅—WCF寄宿前的准备(八) 四.WCF服务部署到IIS7.5 我们把WCF寄宿在IIS之上,在IIS中宿主一个服务的主要优点是在发生客户端请求时宿主进程会被自动启动,并且你可以 ...
- WCF学习之旅—WCF服务部署到应用程序(十)
上接 WCF学习之旅—WCF寄宿前的准备(八) WCF学习之旅—WCF服务部署到IIS7.5(九) 五.控制台应用程序宿主 (1) 在解决方案下新建控制台输出项目 ConsoleHosting.如下 ...
- WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...
- WCF学习之旅—WCF服务的WAS寄宿(十二)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) 八.WAS宿主 IIS ...
- WCF学习之旅—WCF第二个示例(五)
二.WCF服务端应用程序 第一步,创建WCF服务应用程序项目 打开Visual Studio 2015,在菜单上点击文件—>新建—>项目—>WCF服务应用程序.在弹出界面的“名称”对 ...
- WCF学习之旅—WCF第二个示例(七)
三.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据. 在第一个步骤中,你将 Windows 窗体项目添加到解 ...
- WCF学习之旅—WCF第二个示例(六)
第五步,创建数据服务 在“解决方案资源管理器”中,使用鼠标左键选中“SCF.WcfService”项目,然后在菜单栏上,依次选择“项目”.“添加新项”. 在“添加新项”对话框中,选择“Web”节点,然 ...
- WCF学习之旅—WCF概述(四)
一.WCF概述 1) 什么是WCF? Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终 ...
- WCF学习之旅—WCF服务配置(十四)
一.概述 我们在前面章节中讲了寄宿,在前面的实例中也用到了配置文件,这一篇主要讲讲如何在应用配置文件,提高WCF程序的灵活性.在编写WCF服务应用程序时,编写配置项也是其中一项主要工作,在前面的几个示 ...
随机推荐
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- Connect() 2016 大会的主题 ---微软大法好
文章首发于微信公众号"dotnet跨平台",欢迎关注,可以扫页面左面的二维码. 今年 Connect 大会的主题是 Big possibilities. Bold technolo ...
- CorelDRAW X8 如何破解激活(附国际版安装包+激活工具) 2016-12-15
之前有位搞平面的好友“小瘦”说CDR X8无法破解,只能用X7.呃……呃……呃……好像是的 其实CDR8难激活主要在于一个点“没有离线激活了,只可以在线激活”,逆天不是专供逆向的,当然没能力去破解,这 ...
- 网站定位之---根据IP获得区域
记得以前做一个培训机构网站时候需要定位,那时候用的搜狐的api,不是很精准. demo:https://github.com/dunitian/LoTCodeBase/tree/master/NetC ...
- 有趣的 CSS 像素艺术
原文地址:https://css-tricks.com/fun-times-css-pixel-art/#article-header-id-4 译者:nzbin 友情提示:由于国内网络的原因,Cod ...
- 香蕉云APP,2016下半年开发日记
2016-6-17 数据库设计不应该过多依赖范式,适度的冗余可以加快搜索速度,在服务器的配置还可以的情况下,可以采用冗余来解决查找慢的问题.还一个是要选择好数据库引擎,例如 InnoDB 和 myi ...
- 【NLP】揭秘马尔可夫模型神秘面纱系列文章(一)
初识马尔可夫和马尔可夫链 作者:白宁超 2016年7月10日20:34:20 摘要:最早接触马尔可夫模型的定义源于吴军先生<数学之美>一书,起初觉得深奥难懂且无什么用场.直到学习自然语言处 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- git 命令总结
1 删除分支 git push origin :branch name(Task_******) //删除远程分支 git branch -D branch name(Task_******) ...
- 检查sql执行效率
SELECT SUBSTRING(ST.text, ( QS.statement_start_offset / 2 ) + 1, ( ( CASE statem ...