WCF 4.0 如何编程修改wcf配置,不使用web.config静态配置
How to programmatically modify WCF without web.config setting
WCF 4.0 如何编程修改wcf配置,不使用web.config静态配置
接上一篇
WCF4.0安装 NET.TCP启用及常见问题
的例子,继续
在IIS中要实现定义的ServiceHost,需要以下步骤:
1)引入Dll: System.ServiceModel.Activation
2) 修改 svc文件的头部:
<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceOfMyTest.Service_Test_NetTcp"
Factory="CustomServiceHost.SelfDescribingServiceHostFactory"
CodeBehind="Service1.svc.cs" %>
加入 Factory,每一个svc文件的头部都要做修改,都需要添加。
3)实现自己的ServiceHost:
我参考了微软的例子,融入了自己修改:
代码:
SelfDescribingServiceHostFactory.cs
using System;
using System.ServiceModel;
using System.ServiceModel.Activation; namespace CustomServiceHost
{
public class SelfDescribingServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
//All the custom factory does is return a new instance
//of our custom host class. The bulk of the custom logic should
//live in the custom host (as opposed to the factory) for maximum
//reuse value.
var host = new SelfDescribingServiceHost(serviceType, baseAddresses);
return host; } }
}
通常,如果我们使用自定义的ServiceHost,最后一句话基本上都是 host.Open(),而在IIS中,我们直接继承 ServiceHostFactory,实现CreateServiceHost函数,并Return host,而不是 直接Open。
SelfDescribingServiceHost.cs
using System;
using System.Net.Security;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Xml;
using WcfServiceOfMyTest; namespace CustomServiceHost
{
//This class is a custom derivative of ServiceHost
//that can automatically enabled metadata generation
//for any service it hosts.
class SelfDescribingServiceHost : ServiceHost
{
public SelfDescribingServiceHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
} //Overriding ApplyConfiguration() allows us to
//alter the ServiceDescription prior to opening
//the service host.
protected override void ApplyConfiguration()
{
//First, we call base.ApplyConfiguration()
//to read any configuration that was provided for
//the service we're hosting. After this call,
//this.ServiceDescription describes the service
//as it was configured.
base.ApplyConfiguration(); //Now that we've populated the ServiceDescription, we can reach into it
//and do interesting things (in this case, we'll add an instance of
//ServiceMetadataBehavior if it's not already there.
ServiceMetadataBehavior smb = this.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb == null)
{
smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = false;
this.Description.Behaviors.Add(smb);
}
else
{
////Metadata behavior has already been configured,
////so we don't have any work to do.
//return;
} ServiceDebugBehavior sdb = this.Description.Behaviors.Find<ServiceDebugBehavior>();
if (sdb == null)
{
sdb = new ServiceDebugBehavior();
this.Description.Behaviors.Add(sdb);
}
sdb.IncludeExceptionDetailInFaults = true; ServiceBehaviorAttribute sba = this.Description.Behaviors.Find<ServiceBehaviorAttribute>();
if (sba == null)
{
sba = new ServiceBehaviorAttribute(); this.Description.Behaviors.Add(sba);
}
sba.IncludeExceptionDetailInFaults = true;
sba.MaxItemsInObjectGraph = int.MaxValue; ServiceThrottlingBehavior stb = this.Description.Behaviors.Find<ServiceThrottlingBehavior>();
if (stb == null)
{
stb = new ServiceThrottlingBehavior();
stb.MaxConcurrentCalls = int.MaxValue;
//stb.MaxConcurrentSessions = int.MaxValue;
this.Description.Behaviors.Add(stb);
} var items = this.Description.Endpoints; //这个东西无法在代码中修改,只能在web.config 中修改
//ServiceHostingEnvironment.MultipleSiteBindingsEnabled = true; //Add a metadata endpoint at each base address
//using the "/mex" addressing convention
foreach (Uri baseAddress in this.BaseAddresses)
{
if (baseAddress.Scheme == Uri.UriSchemeHttp)
{
/*
svcutil.exe http: //localhost/WcfServiceOfMyTest/Service1.svc?wsdl
*/
smb.HttpGetEnabled = true;
this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
"mex");
}
else if (baseAddress.Scheme == Uri.UriSchemeHttps)
{
smb.HttpsGetEnabled = true;
this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpsBinding(),
"mex");
}
else if (baseAddress.Scheme == Uri.UriSchemeNetPipe)
{
this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexNamedPipeBinding(),
"mex");
}
else if (baseAddress.Scheme == Uri.UriSchemeNetTcp)
{
//这里只暴露了NET.TCP的协议
var tcpBinding = new NetTcpBinding()
{
Name = "CacheSrvNetTcpBindConfig",
ReaderQuotas = XmlDictionaryReaderQuotas.Max,
ListenBacklog = int.MaxValue, MaxBufferPoolSize = long.MaxValue,
MaxConnections = int.MaxValue,
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
PortSharingEnabled = true,
CloseTimeout = TimeSpan.FromMinutes(),
OpenTimeout = TimeSpan.FromMinutes(),
ReceiveTimeout = TimeSpan.FromMinutes(),
SendTimeout = TimeSpan.FromMinutes(),
TransactionFlow = false,
TransferMode = TransferMode.Buffered,
TransactionProtocol = TransactionProtocol.OleTransactions,
HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
ReliableSession = new OptionalReliableSession
{
Enabled = false,
Ordered = true,
InactivityTimeout = TimeSpan.FromMinutes()
},
Security = new NetTcpSecurity
{
Mode = SecurityMode.None,
Message = new MessageSecurityOverTcp
{
ClientCredentialType = MessageCredentialType.None
},
Transport = new TcpTransportSecurity
{
ClientCredentialType = TcpClientCredentialType.None,
ProtectionLevel = ProtectionLevel.None
}
}
}; /*
svcutil.exe net.tcp://dst52382.cn1.global.ctrip.com/WcfServiceOfMyTest/Service1.svc/mex
*/
this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
tcpBinding,//MetadataExchangeBindings.CreateMexTcpBinding(),
"mex"); this.AddServiceEndpoint(typeof(IJustAnInterface), tcpBinding, "");
}
} }
} }
Web.config
<?xml version="1.0"?>
<configuration> <system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel> <system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer> </configuration>
WCF 4.0 如何编程修改wcf配置,不使用web.config静态配置的更多相关文章
- 使用Web.Config Transformation配置灵活的配置文件
发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常常有发布的需求,就需要常常修改web.config文件,这往往是一件非常麻 ...
- Web.Config Transformation配置灵活的配置文件
使用Web.Config Transformation配置灵活的配置文件 发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常 ...
- web.config中配置数据库(多数据)连接的两种方式
这是我的第一篇文章,既然是第一篇了,那就从最基础的只是说起--web.config中配置数据库连接. 网上有很多这方面的资料,但发现并没有一篇从头到位很清楚明了说完的,今天就把我的整理写在这里吧. 在 ...
- membership 在web.config中配置信息
<?xml version="1.0" encoding="utf-8"?><configuration> <configSect ...
- c# MVC在WEB.Config中配置MIME
在IIS中,默认没有添加.json格式的MIME,所有无法读取服务器中的.json格式的文件,返回结果404 方式一:在IIS中手动添加MIME 1.点击MIME进入MIME列表 2.添加MIME 3 ...
- HttpModule在Web.config的配置和动态配置
学习笔记 ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHandler,HttpHandler处理完之后, ...
- asp.net 多个域名重定向,在web.Config中配置
一个网站有多个域名,但是需要在访问其中某个域名之后跳转到另一域名. Web.config 中配置 </system.webServer> <!--重定向 域名 开始--> &l ...
- 在Web.config中配置handler
在Web.config中配置handler节点时发现用vs2010和用vs2015竟然不一样,经过多次测试发现了一些倪端: <configuration> <!--vs2010中需要 ...
- c#与vb.net在App_Code里面编译要通过,需要以下web.config的配置
web.config的配置: <system.web> <codeSubDirectories> <add directoryName="VB"/&g ...
随机推荐
- 【OCP认证12c题库】CUUG 071题库考试原题及答案(26)
26.choose two Examine the structure of the PRODUCTS table. Which two statements are true? A) EXPIRY_ ...
- css3半圆
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- “全栈2019”Java多线程第七章:等待线程死亡join()方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- LOJ#6044. 「雅礼集训 2017 Day8」共(Prufer序列)
题面 传送门 题解 答案就是\(S(n-k,k)\times {n-1\choose k-1}\) 其中\(S(n,m)\)表示左边\(n\)个点,右边\(m\)个点的完全二分图的生成树个数,它的值为 ...
- 面向对象之-@classmethod、@staticmethod和@classonlymethod的区别
实例方法.静态方法与类方法的含义 实例方法(普通方法)的含义就是需要类对象实例之后才能调用的方法,该方法的基本格式为: def test(self,*args,**kwargs): # 第一个参数必须 ...
- 第一个PSP0级
1.计划: 需求描述: 按照图片要求设计添加新课程界面.(0.5分) 在后台数据库中建立相应的表结构存储课程信息.(0.5分) 实现新课程添加的功能. 要求判断任课教师为王建民.刘立嘉.刘丹.王辉.杨 ...
- leetcode-39-组合总和(有趣的递归)
题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...
- L03-Linux RHEL6.5系统中配置本地yum源
1.将iso镜像文件上传到linux系统.注意要将文件放在合适的目录下,因为后面机器重启时还要自动挂载,所以此次挂载成功之后该文件也不要删除. 2.将iso光盘挂载到/mnt/iso目录下. (1)先 ...
- linux下线程的分离和结合属性
在任何一个时间点上,线程是可结合的(joinable),或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死:在被其他线程回收之前,它的存储器资源(如栈)是不释放的.相反, ...
- BZOJ3168. [HEOI2013]钙铁锌硒维生素(线性代数+二分图匹配)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=3168 题解 首先,我们需要求出对于任意的 \(i, j(1 \leq i, j \leq ...