I am working in an application that uses EF,WCF and Asp.net. The application is working fine with only one problem. The EF has an app.config file where the connection string is present
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="MyEntities" connectionString="metadata=res://*/DataAccessStrategy.Components.XYZ.csdl|res://*/DataAccessStrategy.Components.XYZ.ssdl|res://*/DataAccessStrategy.Components.XYZ.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyDataSource;Initial Catalog=MyDataBase;Persist Security Info=True;User ID=xxx;Password=xxx;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Now the WCF service is the consumer to the EF service and henceforth in this config file I do have an entry of the same connection string as under
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyEntities" connectionString="metadata=res://*/DataAccessStrategy.Components.XYZ.csdl|res://*/DataAccessStrategy.Components.XYZ.ssdl|res://*/DataAccessStrategy.Components.XYZ.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyDataSource;Initial Catalog=MyDataBase;Persist Security Info=True;User ID=xxx;Password=xxx;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
........
............
The asp.net application interacts with WCF as it’s config file is as under
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
.......................
.........................
As can be figured out that, the connection string is present in 2 places.
Now it is not feasible. So I have removed the config file completely from the entity framework(so now it is present in the wcf application) and it worked fine.
But what I want is that I want to make it’s presence in the web.config file of Asp.net application and want to remove from the WCF app.config file. If I do so, I am getting an error message
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Is it at all possible what I am looking for or I am asking something which is beyond scope?
Please help
If I understand your architecture correctly, the ASP.NET application doesn’t interact directly with EF or DB, therefore there is no need to have the connection string in its web.config file. You only need the connection string in WCF service config file which needs database access. This means the connection string is not in two places, the config files actually contain different information:
Hypothetically you could remove the connection string from the WCF service config file and put it in the ASP.NET application config file but in this case you’d have to send it to WCF service with every method call. This is not a good approach and IMO doesn’t have any advantages over your current setup.
You would need to add an aditional string parameter to all your WCF methods to receive the connection string from the ASP.NET application:
You could now move your connection string from the WCF config file to the ASP.NET config file. Before calling the web service method you would read the connection string from the config and pass it to it: