积极答复者
WCF 如何动态生成服务?版主和各位大神麻烦进来指点下撒

问题
-
//例如要host的服务如下: namespace My.Test { interface ITest1 { } interface ITest2 { } interface ITest3 { } partial class TestService : ITest1, ITest2, ITest3//更多的接口... ... { } }
而类TestService后面实现的接口会不断增加,原本通过配置定义终结点的方式就变得麻烦起来了,比如,用配置的方式如下:
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="false"/> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="My.Test.TestService"> <endpoint address="" binding="wsHttpBinding" contract="My.Test.ITest1" name="ITest1" bindingConfiguration="wsHttpBindingConfiguration"/> <endpoint address="" binding="netTcpBinding" contract="My.Test.ITest2" name="ITest2" bindingConfiguration="tcpBindingCfg"/> <!--更多... ...--> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> <host> <baseAddresses><!--基址暂定http和tcp的协议--> <add baseAddress="http://192.168.1.10:9998/TestService"/> <add baseAddress="net.tcp://192.168.1.10:9999/TestService"/> </baseAddresses> </host> </service> </services> <bindings> <wsHttpBinding> <binding name="wsHttpBindingConfiguration" maxReceivedMessageSize="20971510"> <readerQuotas maxStringContentLength="20971520" maxArrayLength="20971520"/> <security mode="None"> <transport clientCredentialType="Windows"/> <message clientCredentialType="Windows"/> </security> </binding> </wsHttpBinding> <netTcpBinding> <binding name="tcpBindingCfg"> <security mode="None" > <transport clientCredentialType="Windows" /> <message clientCredentialType="Windows" /> </security> </binding> </netTcpBinding> </bindings> </system.serviceModel>
当每增加一个Contract就要手工去改动配置,很麻烦,因为我的契约库是单独出来的,想在新增Contract时,要需要重启服务即可得自动host上去。故我想到用反射的方式,得到 TestService实现的所有Contract,我想通过编码的方式增加终结点,如下:
var t = typeof(TestService); using (var host = new ServiceHost(typeof(TestService))) { var contracts = t.GetInterfaces(); foreach (var item in contracts) { host.AddServiceEndpoint(item.GetType(), new NetTcpBinding(), ""); host.AddServiceEndpoint(item.GetType(), new WSHttpBinding(), ""); } host.Open(); }
然后配置中就Services变成这样:
<services> <service behaviorConfiguration="metadataBehavior" name="My.Test.TestService"> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> <host> <baseAddresses><!--基址暂定http和tcp的协议--> <add baseAddress="http://192.168.1.10:9998/TestService"/> <add baseAddress="net.tcp://192.168.1.10:9999/TestService"/> </baseAddresses> </host> </service> </services>
相当于终结点那部份就通过编码实现。但上面的方式行不通?有人能指教 下吗?不胜感激。
自省,谦虚
- 已编辑 CowVirus 2012年8月8日 9:47
答案
-
版主和各路大神捏?都不在线的呃?
自省,谦虚
你要动态生成服务。
目前来看必须要更新程序集吧。这个过程重新加载服务库程序集也要重新启动托管程序。
自动加载,完全可以实现代码方式。代码要有方法去获取类或者契约名称,反射是一直方式,不过你要命名有个约定。有一定的规律可循。
不然操作太复杂
Frank Xu Lei--谦卑若愚,好学若饥
[老徐的网站]:http://www.frankxulei.com/[老徐的博客]:http://54peixun.com/Author/frankxulei
微软WCF中文技术论坛
微软WCF英文技术论坛
Windows Azure中文技术论坛- 已标记为答案 Haixia_XieModerator 2012年8月14日 11:19
全部回复
-
版主和各路大神捏?都不在线的呃?
自省,谦虚
你要动态生成服务。
目前来看必须要更新程序集吧。这个过程重新加载服务库程序集也要重新启动托管程序。
自动加载,完全可以实现代码方式。代码要有方法去获取类或者契约名称,反射是一直方式,不过你要命名有个约定。有一定的规律可循。
不然操作太复杂
Frank Xu Lei--谦卑若愚,好学若饥
[老徐的网站]:http://www.frankxulei.com/[老徐的博客]:http://54peixun.com/Author/frankxulei
微软WCF中文技术论坛
微软WCF英文技术论坛
Windows Azure中文技术论坛- 已标记为答案 Haixia_XieModerator 2012年8月14日 11:19