来自 EF6 配置文件的 WCF 连接字符串
WCF connectionstring from configuration file with EF6
我有一个带有内部 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)
returns 正确的 WCF 服务器配置文件(表示 "C:\Program Files\WCfService\WCfService.exe.config"
)
当 WPF 应用程序使用此 WCF 服务时,ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
returns 其配置文件(表示 "C:\Program Files\WPFApp\WPFApp.exe.config"
)
我的一些想法:
- 权限问题? Windows 服务在系统帐户下 运行 和
管理员下的 WPF 应用程序。
- 这 2 个客户端的 app.config 文件错误?
感谢您的帮助!
@Thomas,你是对的!感谢这个好主意!
好的,最后我的问题与 app.config(s) 完全无关。
我没有正确调用我的 WCF 服务。
为了避免代理 generation/use,我在我的 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 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>();
}
}
希望对您有帮助!
我有一个带有内部 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)
returns 正确的 WCF 服务器配置文件(表示 "C:\Program Files\WCfService\WCfService.exe.config"
)
当 WPF 应用程序使用此 WCF 服务时,ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
returns 其配置文件(表示 "C:\Program Files\WPFApp\WPFApp.exe.config"
)
我的一些想法:
- 权限问题? Windows 服务在系统帐户下 运行 和 管理员下的 WPF 应用程序。
- 这 2 个客户端的 app.config 文件错误?
感谢您的帮助!
@Thomas,你是对的!感谢这个好主意!
好的,最后我的问题与 app.config(s) 完全无关。
我没有正确调用我的 WCF 服务。
为了避免代理 generation/use,我在我的 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 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>();
}
}
希望对您有帮助!