Can you host multiple WCF processes in a single windows service?

谁说我不能喝 提交于 2019-12-18 12:38:56

问题


I have a WCF process hosted in a windows service. I am wondering if I can safely have multiple WCF processes that do different things hosted in the same windows service. Do I have to worry about ports? I am using a mex endpoint


回答1:


Yes, you can. I am doing this exact thing in my project, hosting three separate WCF services inside my Windows service. Just make sure that each WCF endpoint, i.e., the address/binding/contract tuple, is unique.




回答2:


EDIT: SO seems to be trimming my lengthy code/config example so there's a complete explanation here: http://thegrenade.blogspot.com/2009/08/hosting-multiple-wcf-services-under.html

Here's an example that may help get you going:

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 });
    }
}

And some configuration

  • Here, the binding & behaviour configuration is shared across services but you may need different configurations for different types of services.
  • I use different ports for different services, but you don't have to.

    ...




回答3:


Have a look at this Run WCF ServiceHost with multiple contracts its not exactly what you are asking for but maybe of some use.

Using that plus the InstanceContextMode property of the ServiceBehaviour attribute and the ability to configure Service throttling you should be able to get what you want.




回答4:


As with @Matt, I've done it too with help from this link.

http://www.codeproject.com/KB/WCF/generic_wcf_host.aspx



来源:https://stackoverflow.com/questions/1308975/can-you-host-multiple-wcf-processes-in-a-single-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!