繁体   English   中英

Silverlight WCF RIA服务动态更改连接字符串

[英]Silverlight WCF RIA Services Dynamically Change Connection String

我们有一个Silverlight应用程序,它使用WCF RIA服务和Entity Framework 4.1连接到数据库。

目前,已提供连接字符串,这是web.config中的标准配置,并且都可以成功运行。

但是,我现在希望能够在运行时基于用户登录等动态地更改连接字符串。

我发现了其他一些暗示可以做到这一点的帖子 ,但是它们使用的是ObjectContext,而我们在System.Data.Entity命名空间以及DbDomainService类中使用的是DbContext。

为了弥补这一点,我在DbDomainService实现中重写了CreateDbContext()方法,如下所示:

protected override CoreContext CreateDbContext()
{
    dbServer = null, dbName = null;

    httpCookie = System.Web.HttpContext.Current.Request.Cookies["DBServer"];
    if (httpCookie != null)
    {
        dbServer = httpCookie.Value;
    }

    var cookie = System.Web.HttpContext.Current.Request.Cookies["DBName"];
    if (cookie != null)
    {
        dbName = cookie.Value;
    }

    if (dbServer == null && dbName == null)
    {
        return new CoreContext();
    }

    string connStr = "Data Source=" + dbServer + ";Initial Catalog=" + dbName + ";Integrated Security=true";
    return new CoreContext(connStr);
}

首次加载Silverlight应用程序时,此操作成功完成,但是,在所有后续加载中,尽管更改了替换为连接字符串的值,但仍使用与最初建立的连接相同的连接。

更改连接的唯一方法似乎是回收IIS中的应用程序池并再次加载该应用程序。

难道我做错了什么? 还是无法让DbDomainService动态更改其连接?

我在考虑您的domainservice类的实例化模型。 您是否尝试过自定义IDomainServiceFctory 它使您可以决定何时创建它们的新实例,并且实现起来非常简单。
也看看这个帖子由Fredrik诺门。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM