成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

Host多個WCF服務(Self-host)

2010-08-28 10:51:35來源:西部e網作者:

     如果用self-host的方式來加載服務的話,我們一般是用這樣的代碼:ServiceHost host1 = new ServiceHost(typeof(UserService)); 問題是,如果當你的服務很多的時候這樣做是不勝其煩的,今天在看Ingo Rammer(大名鼎鼎的《Advanced .Net Remoting》作者)的Blog的時候,看到了他解決這一問題的方法:

Important Update: The originally presented code only works if the service is defined in the same assembly which hosts the service (because the name="" attribute in <service> may not contain the assembly name of the service). See at the end of the article for a slightly different version which works in all cases --- but which involves adding a second config file.

As WCF has reached RC1 stage, I find myself cleaning up a few bits of older WCF code. While playing around with it, I always found myself having to start more and more ServiceHosts for different configurations. The following snippet iterates over all <service> entries in configuration/system.serviceModel and opens a ServiceHost for each of them.

Update: Added code to close all services.

一. 服務配置在app.config或web.config中:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel;

public class ServiceHostGroup
{
    static List<ServiceHost> _hosts = new List<ServiceHost>();

    private static void OpenHost(Type t)
    {
        ServiceHost hst = new ServiceHost(t);
        hst.Open();
        _hosts.Add(hst);
    }

    public static void StartAllConfiguredServices()
    {
        Configuration conf =
          ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);

        ServiceModelSectionGroup svcmod =
          (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
        foreach (ServiceElement el in svcmod.Services.Services)
        {
            Type svcType = Type.GetType(el.Name);
            if (svcType == null)
              throw new Exception("Invalid Service Type " + el.Name + " in configuration file.");
            OpenHost(svcType);
        }

    }


    public static void CloseAllServices()
    {
        foreach (ServiceHost hst in _hosts)
        {
            hst.Close();
        }
    }
}

二.服務配置在外部配置文件中:

As mentioned above, I have in the meantime learned (after long and hard fighting against the "this service has no non-metadata endpoints"-exception), that the name="" attribute in <service> must not contain an assembly name. I guess I have been too used to Remoting configuration files after all ;-). To still get dynamic service configuration without code changes, I've added a second configuration file called services.xml and changed the code for ServiceHostGroup to the following:

Services.XML:

<configuredServices>
  <service type="ServiceImplementation.ArticleService, ServiceImplementation" />
</configuredServices>


ServiceHostBase.cs:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

public class ServiceHostGroup
{
  static List<ServiceHost> _hosts = new List<ServiceHost>();

  private static void OpenHost(Type t)
  {
    ServiceHost hst = new ServiceHost(t);
    Type ty = hst.Description.ServiceType;
    hst.Open();
    _hosts.Add(hst);
  }

  public static void StartAllConfiguredServices()
  {
    ConfiguredServices services = ConfiguredServices.LoadFromFile("services.xml");

    foreach (ConfiguredService svc in services.Services)
    {
      Type svcType = Type.GetType(svc.Type);
      if (svcType == null) throw new Exception("Invalid Service Type " + svc.Type + " in configuration file.");
      OpenHost(svcType);
    }
  }

  public static void CloseAllServices()
  {
    foreach (ServiceHost hst in _hosts)
    {
      hst.Close();
    }
  }
}

[XmlRoot("configuredServices")]
public class ConfiguredServices
{
  public static ConfiguredServices LoadFromFile(string filename)
  {
    if (!File.Exists(filename)) return new ConfiguredServices();

    XmlSerializer ser = new XmlSerializer(typeof(ConfiguredServices));
    using (FileStream fs = File.OpenRead(filename))
    {
      return (ConfiguredServices) ser.Deserialize(fs);
    }
  }

  [XmlElement("service", typeof(ConfiguredService))]
  public List<ConfiguredService> Services = new List<ConfiguredService>();
}

public class ConfiguredService
{
  [XmlAttribute("type")]
  public string Type;
}

原文:http://blogs.thinktecture.com/ingo/archive/2006/09/05/414686.aspx

關鍵詞:VS.NET

贊助商鏈接:

主站蜘蛛池模板: 秦皇岛市| 晴隆县| 微博| 珠海市| 页游| 胶南市| 盐边县| 济南市| 嘉禾县| 禹城市| 措勤县| 涟源市| 安陆市| 上高县| 琼结县| 达孜县| 柳江县| 比如县| 北宁市| 平顶山市| 迁安市| 满城县| 米林县| 百色市| 望城县| 革吉县| 麻阳| 荃湾区| 墨江| 镇康县| 灵台县| 镇康县| 平乡县| 和田县| 分宜县| 石阡县| 策勒县| 揭东县| 图木舒克市| 牟定县| 栾城县|