Host multiple wcf services in windows service




















The installer class, which inherits from Installer , allows the program to be installed as a Windows service by the Installutil. Implement the service contract in a class called CalculatorService as shown in the following code.

Add a local variable called serviceHost to reference the ServiceHost instance. Define the Main method that calls ServiceBase. Run new CalculatorWindowsService. Override the OnStart String[] method by creating and opening a new ServiceHost instance as shown in the following code.

Override the OnStop method closing the ServiceHost as shown in the following code. Create a new class called ProjectInstaller that inherits from Installer and that is marked with the RunInstallerAttribute set to true.

This allows the Windows service to be installed by the Installutil. Add an application configuration file to the project. The source code for this blog entry can be found on the CodePlex project using the link above , click on the Source Code tab and download changeset Initial commit of source.

Lots of new functionality has been added with some refactoring so I'll be updating the article shortly to reflect these changes]. Ok, hands up who love IIS? Nope, me neither. Why not host all your services within a single Windows service on your server.

Config so you can dynamically load them at runtime. This means we could retrieve this information from a central configuration service or from a database, etc. The main entry point of the program, on startup of Windows the service this class just creates and runs a ServiceContainer.

When the ServiceContainer starts, it makes a call to the ConfigService to get the WCF service information assembly, service and contract names along with the endpoint Uri address e.

From this information, the WcfAppServer can create the A. B, C for the WCF configuration. The address is provided, the binding can be inferred from the start of the address and the contract and service types are read using reflection on the assembly. This allows reflection to retrieve all the required type information. You can replace the ConfigService code for a call to your own config service or database.

For this demonstration, the ConfigService just returns hard coded information see further down for code. Once the ServiceContainer has its list of WcfServiceConfigs , it loops through each item first creating, then opening an IsolatedServiceHost for each service.

This class isolates each service host by creating a ServiceHostWrapper object within a new AppDomain. This ensures one service faulting will not affect any of the other services.

This class simply serves as a wrapper around the generic. Name it as ICustomerService. Add ServiceContract attribute to ICustomer.

Your ICustomerService should look like. Add two new XML files name it as Customers. These xml files will be used as datastore for OrderService and CustomerService respectively. Add a new class to Services folder.

Sometimes in service implementation exception occurs. Service exception needs to be handle by using FaultContract. You need to follow same steps for Customer Service and Order Service. Create a folder Hosts in your service library application and place the CustomerServiceHost. Add client application as suggested in the previous step. Ideally, we should have add two applications to use those services. In all most? It's not true. It can be the feeling after reading about MEF and checking some examples.

The truth is that all you need is to implement an interface that can't be simpler empty! Sign in Email. Forgot your password? Search within: Articles Quick Answers Messages. Tagged as All-Topics. Stats



0コメント

  • 1000 / 1000