Anuya
Anuya

Reputation: 8350

Using single windows service to host 4 different WCF projects. How?

Have above 5 WCF projects. In which 4 has to be hosted as windows service and 1 in IIS. All in the same machine.

For each of the 4 WCF projects, I require 4 Windows service projects to host separately. To minimize the number of projects to be maintained, I am thinking of one single windows service to install all the 4 WCF projects for easy maintainability. Anyway apart from OnStart and OnStop I call the wcf and no other logics are there.

The challenges I see are, each Windows service requires the same app config file as used in WCF projects. If I would do this dynamically by getting the service name from app settings, How will I load the app.config file of different wcf projects to host as windows service during run time.

Is this feasible ? If so how can i achieve this ?

Upvotes: 1

Views: 56

Answers (1)

Sergey Vaulin
Sergey Vaulin

Reputation: 732

Yes, it's possible, you just need to configure each one endpoint in your config file. How to do that:

  1. Windows Service WCF hoster is ServiceHost class, so you need to create 4 hosts for every contract.

    var serviceHost = new ServiceHost(typeof(CommunicationManagement)); serviceHost.Open()

  2. Now you can configure every your service endpoint in services section:

<system.serviceModel>
<services>
	<service name="Communication.Service.CommunicationManagement">
		<endpoint
				binding="netTcpBinding" 
					bindingConfiguration="myBinding" 
					contract="Communication.Service.ICommunicationManagement" 
					name="CommunicationManagement">
			<identity>
				<dns value="localhost" />
			</identity>
		</endpoint>
		<endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange" />
		<host>
			<baseAddresses>
				<add baseAddress="http://localhost:8000/Communication/Service/CommunicationManagement" />
			</baseAddresses>
		</host>
	</service>
	<service bname="Communication.Service.Managers.PhoneAdatpersManager">
		<endpoint 
				binding="netTcpBinding"
					bindingConfiguration="myBinding" 
					contract="Communication.IPhoneAdministration"
					name="PhoneAdministration">
			<identity>
				<dns value="localhost" />
			</identity>
		</endpoint>
		<endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange" />
		<host>
			<baseAddresses>
				<add baseAddress="http://localhost:8000/Communication/Service/PhoneAdministration" />
			</baseAddresses>
		</host>
	</service>
</services>
<system.serviceModel>

  1. Above you can see PhoneAdatpersManager and CommunicationManagement services, they hosting in a single Windows Service and works together on 8000 port (but you can use different ports).

Upvotes: 1

Related Questions