常常需要与宿主实例进行交互。这对于使用自托管的方式是不可或缺的。当使用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. 杭电1229 还是A+B

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1229 解题思路,最开始想的是把输入的数据存入数组中,比如输入 1,在数组中就储存为1000(因为数据不 ...

  2. chrome模拟微信

    这里有一个模拟器,既可以设置模拟很多型号的手机设备,也可以伪造你想要的HTTP_USER_AGENT.选择USER_AGENT,选other,微信的HTTP_USER_AGENT是: 在iPhone下 ...

  3. map 解析

    Observable.of(1, 2, 3) .map { $0 * $0 } .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) ...

  4. Go 语言一本通

    什么是GO语言? Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发 ...

  5. js中时间戳与日期时间之间的相互转换

    1.时间戳转换为标准日期时间格式: function timeFormat(dateStr) { var date = new Date(dateStr); Y = date.getFullYear( ...

  6. iOS,android 常用命令

    iOS 常用命令: https://blog.csdn.net/ilikekand17/article/details/81907179 https://www.jianshu.com/p/72c62 ...

  7. 这份接口管理平台 eoLinker 开源版的部署指南教程你一定不想错过

    本文主要内容是讲解如何在本地部署eoLinker开源版. 环境要求 1.PHP 5.5+ / PHP7+(推荐) 2.Mysql 5.5+ / Mariadb 5.5+ 3.Nginx(推荐) / A ...

  8. IOS - 绘制文字 drawInRect: withFont: not working

    在图形绘制中,我们经常会需要绘制文本,但我在给PDF上绘制Text时,始终绘制不上, 使用过: [str drawInRect:cubeRect withAttributes:attrs]; CGCo ...

  9. JavaScript 三要素

    一个完整的JavaScript 实现由3部分组成: ECMACcript ECMAScript 规定了这门语言的下列组成部分: 语法 类型 语句 关键字.保留字 操作符 对象为什么要使用DOM?   ...

  10. javascript 富文本 注意事项

    富文本编辑器 div内嵌iframe iframe body contenteditable属性 true 整个iframe 即为可编辑框,创建时注意事项: 1.编辑 焦点问题 弹出新控件时为控件设置 ...