WCF 自托管应用程序可以使用 app.config 自动创建 ServiceHosts 吗? [英] Can WCF self hosted applications create ServiceHosts automatically using the app.config?

查看:19
本文介绍了WCF 自托管应用程序可以使用 app.config 自动创建 ServiceHosts 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个自托管的 wcf 应用程序时,我会为我想要公开的每个服务创建 ServiceHost 对象.然后在 app.config 中查找(匹配服务器名称),然后提取关联的端点地址和合同.

When I create a self hosted wcf application, I create ServiceHost objects for each service I want to expose. It then looks in the app.config (matching up the server name), and then pulls the associated endpoint address and contract.

有没有办法为 app.config 中列出的每个服务自动创建 ServiceHost.我想向 app.config 添加新服务并自动加载它们,而无需重新编译我的程序并使用我手动编码的过程来创建 ServiceHost 对象.

Is there a way to automatically create ServiceHosts for every service that is listed in the app.config. I would like to add new services to the app.config and have them loaded automatically without recompilng my program and using my manually coded process to create ServiceHost objects.

有人可以链接我的工厂或教程来告诉我如何做到这一点吗?谢谢

Is there a factory or a tutorial someone could link me that shows me how to do this? Thanks

推荐答案

我不确定从配置中提取关联地址和合同是什么意思 - 这是自动完成的.配置文件中的服务部分会自动与 ServiceHost 中托管的服务类型配对:

I'm not sure what do you mean by pulling associated addresses and contracts from config - this is done automatically. Service section in configuration file is automatically paired with type of service hosted in ServiceHost:

服务托管:

using (var host = new ServiceHost(typeof(MyNamespace.Service))
{
  // no endpoint setting needed if configuration is correctly paired by the type name
  host.Open() 
}

服务配置:

<services>
  <service name="MyNamespace.Service">
    ...
  </service>
</service>

现在您唯一需要的是自动处理 ServiceHost 创建.这是我的示例代码:

Now the only thing you need is to handle ServiceHost creation automatically. Here is my sample code to do it:

   class Program
   {
       static void Main(string[] args)
       {
           List<ServiceHost> hosts = new List<ServiceHost>();

           try
           {
               var section = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;
               if (section != null)
               {
                   foreach (ServiceElement element in section.Services)
                   {
                       var serviceType = Type.GetType(element.Name);
                       var host = new ServiceHost(serviceType);
                       hosts.Add(host);
                       host.Open();
                   }
               }

               Console.ReadLine();
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
               Console.ReadLine();
           }
           finally
           {
               foreach (ServiceHost host in hosts)
               {
                   if (host.State == CommunicationState.Opened)
                   {
                       host.Close();
                   }
                   else
                   {
                       host.Abort();
                   }
               }
           }
       }
   } 

这篇关于WCF 自托管应用程序可以使用 app.config 自动创建 ServiceHosts 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆