【问题标题】:WCF connectionstring from configuration file with EF6使用 EF6 的配置文件中的 WCF 连接字符串
【发布时间】:2017-03-21 10:47:13
【问题描述】:

我有一个带有内部连接字符串“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"

我的一些想法:

  • 权限问题? Windows 服务正在系统帐户下运行,并且 管理员下的 WPF 应用程序。
  • 这 2 个客户端的 app.config 文件错误?

感谢您的帮助!

【问题讨论】:

  • 只使用:base("name=Model_DB_Custom")
  • 您好 Eric,如果在 WCF 服务运行时修改此连接字符串,此解决方案似乎不起作用...
  • 其实 base("name=Model_DB_Custom") 是无法识别的。我尝试但我可以看到数据库连接是:“数据源=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true”。我想这是一个默认的连接字符串......我从来没有实现过。
  • 根据您的 cmets,看起来您实际上并不是在远程调用 WCF 服务,而是直接引用服务实现并在进程内运行它。我们在这里遗漏了什么吗?
  • @thomasr,感谢您的有趣评论!我在每个客户端中都添加了 wcf 服务库的引用。我不使用代理,因为它适用于我的 Windows 服务。您是说在使用 wcf 服务之前使用“svcutil”吗?

标签: entity-framework wcf configuration-files


【解决方案1】:

@Thomas,你是对的!感谢这个好主意!

好的,最后我的问题与 app.config(s) 完全无关。

我没有正确调用我的 WCF 服务。

为了避免代理生成/使用,我在我的 WCF 服务中使用 ChannelFactory(),因为我同时管理服务器和客户端,而这种 SimpleIOC 配置很糟糕......

现在这个 WCF 服务已经实现了 throw 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>();
        }
   }

愿它有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 2014-06-17
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多