2

私は、数十を超える WCF サービスを単一の Windows サービスに移行する任務を負っています。Windows サービス テンプレートを使用して Windows サービスを作成し、次のコードを ServiceHostController に追加しました。

public partial class ServiceHostController : ServiceBase
{
    private List<ServiceHost> serviceHosts;

    public ServiceHostController()
    {
        InitializeComponent();
        this.ServiceName = "WCFServices";
        this.CanStop = true;
        this.AutoLog = true;
    }

    protected override void OnStart(string[] args)
    {
        if (serviceHosts != null)
        {
            foreach (var service in serviceHosts)
            {
                service.Close();
            }
        }

        InitializeServices();
        foreach (var service in serviceHosts)
        {
            service.Open();
        }
    }

    protected override void OnStop()
    {
        if (serviceHosts != null)
        {
            foreach (var service in serviceHosts)
            {
                service.Close();
            }
            serviceHosts.Close(); = null;
        }

        foreach (var service in serviceHosts)
        {
            service.Close();
        }
    }

    private void InitializeServices()
    {
        serviceHosts = new List<ServiceHost>()
        {
            new ServiceHost(typeof(WCFService1)),
            new ServiceHost(typeof(WCFService2)),
            // add dozens of services here
        };
    }
}

ここで自分自身を繰り返さないルールに従わないことに加えて (実際のコードは異なります)、これは Windows サービス コードでこれらの WCF サービスをホストする方法ですか?

4

1 に答える 1

3

ハンス、あなたはすべて正しいです。しかし、私はあなたの InitializeServices(); を置き換えます。次のコードで。これは疑似コードなので、ビットを置き換える必要があります:)

1) app.config でエンドポイントを構成する

2) サービス プロジェクト\アセンブリからサービス タイプを取得します。

  Dictionary<Type, Type> mappings = new Dictionary<Type,Type>();

   foreach (Type t in MyServiceAssembly.GetTypes())
  {
    if (t.GetInterfaces().Length > 0)
    {
      foreach (Type ti in t.GetInterfaces())

        {
          if (mapping.ContainsKey(ti))
            System.Diagnostics.Debug.WriteLine("Class {0} implements more than one interface {1}", t.FullName, ti.FullName);
          else
            mapping.Add(ti, t);

          // System.Diagnostics.Debug.WriteLine("Class {0} implements {1}", t.FullName, ti.FullName);
        }
    }
  }

4)app.configからエンドポイントを制御したい場合は、エンドポイントを繰り返し処理し、対応するサービスの実装を取得してから、ホストを作成します

//サービスの起動時にエンドポイントを読み取ります

 List<ServiceHost> serviceHosts = new List<ServiceHost>();

     ServicesSection servicesSection = (ServicesSection)WebConfigurationManager.GetSection("system.serviceModel/services");  

    for(int i = 0;i<servicesSection.Services.Count;i++)
    {
    ServiceEndpointElement endpoint = servicesSection.Services[i].Endpoints[0];  
    string url = string.Format("net.tcp://{0}:{1}/YouNameSpace_service_Name_From_EndPoint/{2}.svc","YourHost","YourPort");
      ServiceHost serviceHost = new ServiceHost(mappings[endpoint.Contract] , new Uri(url));

              serviceHost.Open();

              mServiceHosts.Add(serviceHost);
    }

5) app.config からエンド ポイントを制御したくない場合は 、マッピング リストを反復処理します。

//サービスの起動時にこれを行います

 List<ServiceHost> serviceHosts = new List<ServiceHost>();

     foreach(type t in mappings.Keys)
    {
            string url = string.Format("net.tcp://{0}:{1}/YouNameSpace_{2}.svc","YourHost","YourPort",t.name);
      ServiceHost serviceHost = new ServiceHost(mappings[t] , new Uri(url));

              serviceHost.Open();

              mServiceHosts.Add(serviceHost);
    }
于 2011-11-09T22:37:39.710 に答える