how to selfhost wcf without iis

Reading up on WCF we have self hosting option available , one limitation here is we have to manage the host process lifecycle ourselves. What I am exploring here is to run the service without IIS and do a self hosting.

Few things come to mind - How will request management work here. In case of IIS it manages the request and give control to dotnet on a particular thread. In absence of IIS do we need to write code ourselves to manage incoming requests ( say on a tcp port ) or WCF provides some classes to manage request and spawn threads to process each thread.

  • I am aware that in case of self hosting this needs to be a windows service. In case of self hosting how can me tap on the number of simultaneous requests on the sever , it can be managed by limiting the thread pool ? or we can configure this via wcf ?

Thanks dc


Self-hosting does not require a Windows service. You can self-host inside a console application if you so desire. It's just that Windows services are a good solution for self-hosting if you require 24/7 access but do not want to, for whatever reason, use IIS.

Managing the lifecycle of the host process is not a big deal. I use a Windows service to host a WCF service. I simply start my WCF service in the OnStart() callback of my Windows service, like so:

private ServiceHost _host;
protected override void OnStart(string[] args)
    _host = new ServiceHost(typeof(CalculatorService));

Likewise, I close the WCF service in the OnStop() callback of my Windows service:

protected override void OnStop()
    if (_host != null) _host.Close();

This effectively ties the lifecycle of the WCF service to the lifetime of the Windows service. You could do something similar in any kind of application - console, Windows Forms app, etc. For example, in the OnLoad() callback of your Windows Forms app, start the ServiceHost for your WCF service and close it when exiting the app. Simple enough.

WCF gives you a lot of flexibility on how to handle incoming requests. For example, you could make your WCF service a singleton, which means that you'll have one and only one instance of your WCF service. In this case, all incoming requests are handled by this one instance. But you can also have your WCF service handle each incoming request with a new instance of your WCF service. This allows your service to scale better, but will likely require you to synchronize any access to your backend data storage, e.g., database. You can control this behavior using the InstanceContextMode property of the ServiceBehaviorAttribute on your WCF service.

As I read your question again, it sounds like you're just learning WCF, so I hope none of this has overwhelmed you. Check out my answer to this SO question for some links that you may find helpful.

To answer your specific question, the WCF hosting infrastructure will spin up a HTTP listener that works with HTTP.SYS (the same thing IIS uses) which will listen for traffic on the specific port/address you configure.

For any other questions, I'm sure this section in MSDN will answer them.

Need Your Help

Thread-safe increment/decrement with StackExchange.Redis

c# multithreading redis stackexchange.redis

I'm using the StackExchange.Redis package as a shared store for a multi-threaded application that will in turn be running on multiple servers multi-multi-threaded ;)