I've a WCF service with an internal connectionString "Model_DB_Custom" to connect to my database with Entity Framework 6.
DBContext constructor is:
public partial class Model_DB : DbContext
{
/// <summary>
/// Create a dbcontext from default connectionString in app.config to Sql Server
/// </summary>
public Model_DB() : base(
((ConnectionStringsSection)
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
.GetSection("connectionStrings"))
.ConnectionStrings["Model_DB_Custom"]
.ConnectionString)
{
//This constructor works if connectionstring is changed at runtime...
}
...
}
When a windows service application use this WCF service, ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
returns correct WCF server config file (means "C:\Program Files\WCfService\WCfService.exe.config"
)
When a WPF application use this WCF service, , ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
returns its config file (means "C:\Program Files\WPFApp\WPFApp.exe.config"
)
Some ideas I have:
Thanks for your help!
@Thomas, you were right! Thanks for this good idea!
Ok, finally my issue was not related at all to app.config(s).
I didn't call correctly my WCF service.
To avoid proxies generation/use, I use ChannelFactory() in my WCF Service because I manage both servers and clients and that was this SimpleIOC configuration which was bad...
Now this WCF Service is implemented throw MVVM Light/SimpleIOC with following code:
ViewModelLocator.cs:
public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
//Define data access (wcf service) for design mode
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<IService, Design.DesignDataService>();
}
...
}
WcfServer class:
public class WcfServer : IDisposable
{
...
//Wcf connection
private ChannelFactory<IService> _wcfFactory = null;
/// <summary>
/// Gets the DataService proxy from MVVMLight SimpleIOC instance.
/// </summary>
public IService DataService
{
get
{
return SimpleIoc.Default.GetInstance<IService>();
}
}
public WcfServer(string urlServer)
{
UrlServer = urlServer;
}
/// <summary>
/// Connect to wcf service
/// </summary>
public bool Connect()
{
try
{
...
EndpointAddress endpointAddress = new EndpointAddress(new Uri(UrlServer));
if (_wcfFactory == null)
{
_wcfFactory = new ChannelFactory<IService>(WCFSharedConfiguration.ConfigureBindingWithSimplehttps(), endpointAddress);
//Open ChannelFactory
_wcfFactory.Open();
//Define Faulted handler
_wcfFactory.Faulted += FactoryFaulted;
}
else
//Log + return
if (!ViewModelBase.IsInDesignModeStatic)
{
//If we are not in Design (means blend or VS IDE)
SimpleIoc.Default.Register<IService>(() => _wcfFactory.CreateChannel(), true);
}
}
catch (Exception ex)
{
//Log
throw;
}
return true;
}
public bool Disconnect()
{
SimpleIoc.Default.Unregister<IService>();
}
}
May it helps!