Hosting WCF Services, the Easy Way

March 19th, 2007

Now that I have been through the WCF Master Class I have created a mechanism for loading and hosting multiple WCF services. If you have TortoiseSVN installed you can point it to my public project called SmallSharpTools.Services and get all of the source for the project. It is a provider implementation which defines a provider called ServicesProvider which has a couple of methods to control the hosted services, Start and Stop. I then implemented a Manager Service and a desktop application which interfaces with it to control the hosted services.

Since this is a provider implementation it can be run within a Console Application, Website or a Windows Service. There is no real requirement to run a provider in an ASP.NET Website. The application just has to use the ServicesAdmin class to call the Startup and Shutdown methods when appropriate. There is a sample Console Application and Website along with a GenericService included in this initial release to demonstrate how it is configured. The Startup method checks the autoStart configuration setting for each configured service and only starts it if that value is set to true. It was modeled after Windows Services.

The Manager Service is also just another service. Perhaps the ideal configuration would be to run the Manager Service outside the scope of the ServicesProvider so it can manage the provider services without being included in the list. It just needs access to the ServicesAdmin class. In the included samples I have the Manager Service marked with the isManager attribute in the configuration which prevents the management tool from stopping the Manager Service.

An important detail about WCF is the fact that this goes beyond web services. You can just as easily configure your WCF endpoints for TCP or IPC and make it available to only the hosting application. This is very useful way to create decoupled applications. The assemblies which are a loaded as a part of the configured providers could live in the bin folder or as assemblies held in the GAC. When referenced by Type, assembly name and version you can deploy multiple versions of an assembly to the GAC and have various applications use the ones they require.

A feature I would like to include as a part of this system is a way change an individual service without restarting all of them. I would stop the service, unload the assembly and load the new assembly. It may also re-read the configuration in case the version number or Type for the service changes. The list of providers could also be updated to add or remove existing providers. The hosting scenario gets a little more complicated there. I also plan to make use of the ServiceModelEX project (see WCF section) from IDesign which greatly simplifies the WCF configuration and adds a great deal of programming flexibility.

I can also envision a few commonly used services that I could implement which may be useful within an ASP.NET Website, such as email and password validation services which are used by custom validation controls. I know sites like PeterBlum.com sell entire suites of validation controls. What is useful with WCF services is the fact that you can deploy a part of an application without restarting the entire application. In some scenarios for large websites or applications that could be very useful. And if that service is used by multiple applications a bug in the service could potentially fix several applications without taking them all offline. The possibilities here are unlimited.

4 Responses to “Hosting WCF Services, the Easy Way”

  1. stan Says:

    how can i call a wcf service once it has been "started"? I have the class library, code file (starts, stops, baseaddress) , host and config all built but would really like to see how i might get the host (or even the code file) to call on a method in the service with a passed object.

  2. brennan Says:

    Stan, you will need to use a client. Look in the Solution for the client code for an example. I may be posting more information about self hosting vs external hosting soon to cover this a bit more.

  3. Wes Says:

    Hi,
    Read your post a few days ago, had a look on your code and found it very interesting.
    Did you continue your code (load and unload assembly, start and stop single services ?)

    Rgds

  4. Brennan’s Blog » Blog Archive » What is ESB and do you need it? Says:

    [...] Still, I leave room for the ESB in some scenarios, but the whole discussion reminds me of the Java discussion over Enterprise Java Beans. They were supposed to allow major enterprises to scale, and maybe the jury is still out on whether they were really helpful or just over-engineering, but as best I can tell EBJs were dramatically scaled back in response to criticism by the developer community. An ESB solution looks the classic mistake of applying a big idea to one problem to only end up with two problems. And like with EJB, I am certain that lightweight solutions will emerge, like my WCF hosting solution. [...]