none
WCF 如何动态生成服务?版主和各位大神麻烦进来指点下撒 RRS feed

  • 问题

  • //例如要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
    2012年8月8日 7:38

答案

全部回复