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:Likewise, I close the WCF service in the
OnStop()callback of my Windows service: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 theServiceHostfor 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
InstanceContextModeproperty of theServiceBehaviorAttributeon 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.