2009-08-20 34 views
4

WindowsサービスでホストされているWCFプロセスがあります。 同じWindowsサービスでホストされているさまざまなことを行う複数のWCFプロセスを安全に使用できるかどうかは疑問です。 ポートについて心配する必要はありますか? 私はmexエンドポイントを使用しています複数のWCFプロセスを単一のWindowsサービスでホストできますか?

+4

あなたは、単一のWindowsサービスは、複数のない_processes_でホストされている複数の_WCFのservices_を持っています。 –

答えて

3

はい、できます。私はWindowsサービスの中で3つの別々のWCFサービスをホストしている私のプロジェクトでこの正確なことをやっています。各WCFエンドポイント、すなわちアドレス/バインディング/契約タプルが一意であることを確認するだけです。

1

このRun WCF ServiceHost with multiple contractsをご覧ください。あなたが求めているのではなく、多分使用しているかもしれません。

これにServiceBehaviour属性のInstanceContextModeプロパティとService throttlingを設定する機能を使用すると、必要なものを取得できるはずです。

8

EDIT:SOここに完全な説明がありますので、私の長いコード/設定例をトリミングしているようだ:http://thegrenade.blogspot.com/2009/08/hosting-multiple-wcf-services-under.html

ここであなたが軌道に乗る助けるかもしれ例です:

class Program { 
    static void Main() { 
     if (Environment.UserInteractive) { 
      ServiceManager serviceManager = new ServiceManager(); 
      serviceManager.OpenAll(); 
      Console.ReadKey(); 
      serviceManager.CloseAll(); 
     } 
     else 
      ServiceBase.Run(new WindowsService()); 
    } 
} 

public class WindowsService : ServiceBase 
{ 
    public static string WindowsServiceName = "Windows Service Name"; 
    public static string WindowsServiceDescription = "Windows Service Description"; 
    public static string WindowsServiceUsername = @".\username"; 
    public static string WindowsServicePassword = "password"; 

    private readonly ServiceManager serviceManager = new ServiceManager(); 

    private readonly IContainer components = new Container(); 

    protected override void Dispose(bool disposing) { 
     if (serviceManager != null) serviceManager.CloseAll(); 
     if (disposing && (components != null)) components.Dispose(); 
     base.Dispose(disposing); 
    } 

    public WindowsService() { 
     ServiceName = WindowsServiceName; 
     CanStop = true; 
    } 

    protected override void OnStart(string[] args) { 
     base.OnStart(args); 
     serviceManager.OpenAll(); 
    } 

    protected override void OnStop() { 
     serviceManager.CloseAll(); 
     base.OnStop(); 
    } 
} 

public class ServiceManager { 
    readonly List<ServiceHost> serviceHosts = new List<ServiceHost>(); 

    public void OpenAll() { 
     OpenHost<Service1>(); 
     OpenHost<Service2>(); 
     ... 
    } 

    public void CloseAll() { 
     foreach (ServiceHost serviceHost in serviceHosts) 
      serviceHost.Close(); 
    } 

    private void OpenHost<T>() { 
     Type type = typeof(T); 
     ServiceHost serviceHost = new ServiceHost(type); 
     serviceHost.Open(); 
     serviceHosts.Add(serviceHost); 
    } 
} 

/// <remarks> 
/// Enables application to be installed as a Windows Service by running InstallUtil 
/// </remarks> 
[RunInstaller(true)] 
public class WcfServiceHostInstaller : Installer { 
    public WcfServiceHostInstaller() { 
     Installers.Add(new ServiceInstaller 
          { 
           StartType = ServiceStartMode.Automatic, 
           ServiceName = WindowsService.WindowsServiceName, 
           Description = WindowsService.WindowsServiceDescription 
          }); 
     Installers.Add(new ServiceProcessInstaller { Account = ServiceAccount.User, Username = WindowsService.WindowsServiceUsername, Password = WindowsService.WindowsServicePassword }); 
    } 
} 

そしていくつかの構成

  • ここで、th eバインディング&ビヘイビア設定はサービス間で共有されますが、異なるタイプのサービスに対して異なる設定が必要な場合があります。
  • サービスごとに異なるポートを使用しますが、そうする必要はありません。

    ...

関連する問題