我有一个带有内部connectionString“Model_DB_Custom”的WCF服务,用Entity Framework 6连接到我的数据库。
DBContext构造函数是:
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...
}
...
}
当Windows服务应用程序使用此WCF服务时,ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
将返回正确的WCF服务器配置文件(表示"C:\Program Files\WCfService\WCfService.exe.config"
)
当WPF应用程序使用此WCF服务时,ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
将返回其配置文件(表示"C:\Program Files\WPFApp\WPFApp.exe.config"
)
我有一些想法:
感谢您的帮助!
答案 0 :(得分:0)
好的,最后我的问题根本与app.config无关。
我没有正确调用我的WCF服务。
为了避免代理生成/使用,我在WCF服务中使用ChannelFactory(),因为我管理服务器和客户端,这就是这个简单的SimpleIOC配置......
现在使用以下代码实现此WCF服务抛出MVVM Light / SimpleIOC:
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类:
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>();
}
}
愿它有所帮助!