常常需要与宿主实例进行交互。这对于使用自托管的方式是不可或缺的。当使用IIS或WAS时,不能直接访问宿主。为了克服这个障碍,WCF提供了一个宿主工厂。在.svc文件中使用Factory标签,使用此工厂,而不使用默认工厂:

<%@ ServiceHost
Language = "C#"
Debug = "true"
CodeBehind = "~/App_Code/MyService.cs"
Service = "MyService"
Factory = "MyServiceFactory"
%>

当不显示使用.svc文件时,也可以在配置文件中指定宿主工厂:

“<serviceActivations>
<add relativeAddress = "MyService.svc"
service = "MyService"
factory = "MyServiceFactory"
/>
</serviceActivations>” 摘录来自: Juval Löwy and Michael Montgomery. “Programming WCF Services”。 iBooks.

自定义宿主工厂类必须继承自ServiceHostFactory类并重写虚方法CreateServiceHost():

“public class ServiceHostFactory : ...
{
protected virtual ServiceHost CreateServiceHost(Type serviceType,
Uri[] baseAddresses);
//More members
}” 摘录来自: Juval Löwy and Michael Montgomery. “Programming WCF Services”。 iBooks.

例如:

“class MyServiceFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType,
Uri[] baseAddresses)
{
ServiceHost host = new ServiceHost(serviceType,baseAddresses); //Custom steps here return host;
}
}” 摘录来自: Juval Löwy and Michael Montgomery. “Programming WCF Services”。 iBooks.

CreateServiceHost()方法其实就相当于IIS或WAS托管服务的Main()方法,你可以以这样的目的来使用它。

尽管对于从 CreateServiceHost 返回的 ServiceHost 可以执行什么操作没有技术限制,但建议您尽可能使工厂实现简单化。如果有大量的自定义逻辑,最好将这些逻辑放入宿主内而不是工厂内,以便可以重用它们。

完整示例:CustomServiceHost.zip

参考:

Juval Löwy and Michael Montgomery. “Programming WCF Services”

使用 ServiceHostFactory 扩展宿主(https://msdn.microsoft.com/zh-cn/library/aa702697.aspx)

Custom Hosting in IIS/WAS的更多相关文章

  1. ASP.NET Core In Process Hosting on IIS with ASP.NET Core 2.2(转载)

    ASP.NET Core 2.2 has been out for a while now and with it come some significant improvements to the ...

  2. Using Custom Domains With IIS Express In Asp.Net Core

    IIS Express是一个Mini版的IIS,能够支持所有的Web开发任务,但是这种设计有一些缺陷,例如只能通过localhost:<port>的方式来访问我们的应用程序,看起来就有点不 ...

  3. Getting Started with OWIN and Katana(Console 代替iis 制作 web服务的简单方案)

    Open Web Interface for .NET (OWIN) defines an abstraction between .NET web servers and web applicati ...

  4. [转]ASP.NET Core 1 Deploy to IIS

    本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...

  5. ASP.NET Core 2.2 和之前版本区别: 可以在IIS上进行ASP.NET核心进程托管 (翻译)

    原文链接: https://weblog.west-wind.com/posts/2019/Mar/16/ASPNET-Core-Hosting-on-IIS-with-ASPNET-Core-22 ...

  6. 000.Introduction to ASP.NET Core--【Asp.net core 介绍】

    Introduction to ASP.NET Core Asp.net core 介绍 270 of 282 people found this helpful By Daniel Roth, Ri ...

  7. .NET Best Practices

    Before starting with best practices tobe followed, it is good to have clear understanding of how mem ...

  8. Linux上运行NET

    今天尝试了下Ubuntu上运行NET程序,按照 https://github.com/aspnet/Home 的指引,一步一步来: 1.安装DNVM(原名KVM) Linux控制台下输入 curl - ...

  9. WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

    http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...

随机推荐

  1. 深入理解 Swift 派发机制

    原文: Method Dispatch in Swift作者: Brain King译者: kemchenj 译者注: 之前看了很多关于 Swift 派发机制的内容, 但感觉没有一篇能够彻底讲清楚这件 ...

  2. 洛谷P1654 OSU!_概率与期望

    Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 1000000 ...

  3. OpenStack 发行版本

    2010年7月,Rackspace和美国宇航局联合其他25家公司启动了OpenStack项目 OpenStack的发行版本时间表和核心项目 OpenStack发行版本 名称 日期           ...

  4. jsp js action之间传值

    1.struts2 action如何向JSP的JS函数传值 action中定义变量 public class TestAction extends ActionSupport implements S ...

  5. jQuery选择器补充

    ---------------------------------------------------------------------------------------------------- ...

  6. DOM中元素节点,属性节点,文本节点的理解

    节点信息 每个节点都拥有包含着关于节点某些信息的属性.这些属性是: nodeName(节点名称) nodeValue(节点值) nodeType(节点类型) nodeType nodeType 属性可 ...

  7. 《Javascript权威指南》学习笔记之十五:BOM之源---window对象

    BOM是Browser Object Model的缩写,即浏览器对象模型,提供了独立于网页内容和浏览器窗体之间进行交互的APi.API由若干对象组成,因为浏览器是Javascript的宿主,因此,这些 ...

  8. 用友ERP T6技术解析(六) 库龄分析

    2.4 库存管理   2.4.1 库龄分析 介绍:库存账龄是在某时间节点,某种或某类存货的库存时间的加权平均值,跟库存周转率关系明显,库存周转率越高,库存账龄越低,可是二者又不是反比关系.不能简单把库 ...

  9. 关于心理的二十五种倾向(查理&#183;芒格)-2

    5)避免不一致倾向避免不一致倾向实际上就是人天生就害怕改变.相同是由于人类大脑的生理机制决定的.由于这样的倾向能够带来节省运算空间和能量的优点.这样的抗改变模式的形成,可能的原因例如以下:A) 迅速作 ...

  10. 1003. 我要通过!(20) (ZJUPAT 模拟)

    题目链接:http://pat.zju.edu.cn/contests/pat-b-practise/1003 "答案正确"是自己主动判题系统给出的最令人欢喜的回复.本题属于PAT ...