How to configure multiple instances of the same WCF service on the same machine
-
Thursday, February 16, 2012 10:07 PM
We have a web application developed using asp.net 4.0 and a WCF service developed using asp.net 4.0.
The web application will be running on the web server. It will communicate with the WCF service running on the App server.
The WCF makes all the database communication using stored procedures.
We are planning to build four applications for four different states. Each state has a separate database.
I was able to setup four web applications, same codebase, the config entries will denote which state.
My questions is can how do I configure the WCF service to run 4 separate instances and each pointing to different database connection strings.
All Replies
-
Friday, February 17, 2012 3:32 AMModerator
Since you have 4 web applications, I assume you have 4 web.config files. If this is the case, you can share the same WCF service code, and in the code it will load the database connection string from the web.config and initialize the db connection with it. This way, each service will point to a different database.
Carlos Figueira
-
Friday, February 17, 2012 6:51 AM
As WCF is capable enough to accept and server multiple request, you do not really need to create 4 instances of same code base just for multi-connection strings unless until you have some performance issue. This would create a lot of maintainability issue.You can host a WCF service with a web.config file which contains 4 connection strings in Config file as below.
<connectionStrings> <add name="ConnectionStringForState1" connectionString="..."/> <add name="ConnectionStringForState2" connectionString="..."/> <add name="ConnectionStringForState3" connectionString="..."/> <add name="ConnectionStringForState4" connectionString="..."/> </connectionStrings>
When your Asp.net Web Application call WCF service, it should send a note of telling request is made for what "state" and based on that, From WCF you can read corresponding config and connect to Database.
conn = ConfigurationManager.ConnectionStrings [ConnectionStringForState1].ConnectionString;
If you really want to Host 4 instance of WCF services than you can host it similar like your 4 asp.net web site hosting (create 4 web site in IIS and Host WCF in each) but your endpointsneed to be different.
For .Net 4.0, you can look for WCF Router, which have a capability of routing a specific Request to a specific service instance based on message header.
or
For WCF Self hosting, this is possible by Overriding Service Host class. This link will guide you to do that.
Lingaraj Mishra
-
Tuesday, February 21, 2012 2:58 AMLingaraj,
The requirement is to host multiple instances( or processes) of the same WCF service as opposed to one process within which there are multiple Service Host instances.
The link explained on how to host multiple service hosts and that does not solve my problem.
I hosted the WCF in IIS with different port numbers and it gave an error that the file executed by the process is already in use.
I am new to WCF , i managed to get the service working , but i am facing deployment issues.
Please help me fix the issue.
Thanks in advance.
-
Wednesday, February 22, 2012 7:13 AM
Can you share some more detailed exception.
When you get this exception and and what exception it throws like is it while accessing the web.config or any other instance.
Lingaraj Mishra
-
Friday, February 24, 2012 9:57 PM
I have some good news. I am no longer getting that exception which says the file is in use.
I was able to successfully host 4 instances of the service. I have one last question,
Do i create them as separate applications under the Default web site or do i create them as different sites binded to different port numbers.
Do I have an advantages of doing one over the other.
Awaiting your advice.
Thanks much.
-
Tuesday, February 28, 2012 5:47 PM
What version of IIS youb are using.nIIS 7 formalizes the concepts of sites, applications, and virtual directories. Virtual directories and applications are now separate objects, and they exist in a hierarchical relationship in the IIS 7 configuration schema. Briefly, a site contains one or more applications, an application contains one or more virtual directories, and a virtual directory maps to a physical directory on a computer.
If all your services are going to use same runtime setting of IIS than you can create tham as separate applications under the Default web site.
If you want to have different isolation level for reliability, or security reasons and each service would have different run time setting than you can create them as different sites binded to different port numbers.
below link should help you to understand more on this.
http://blogs.technet.com/b/chrad/archive/2010/01/24/understanding-iis-bindings-websites-virtual-directories-and-lastly-application-pools.aspx
Lingaraj Mishra

