我找到了解決方案。對於那些有興趣的,這裏是:
這感覺有點像黑客,但它是我能想出的唯一解決方案。感謝Sally Xu在forums.silverlight.net上提供的想法。
因爲我的每個用戶都可以在多個服務器上擁有多個數據庫,所以我需要在第一次使用DomainService之前找到創建ConnectionString的方法。每個用戶選擇從UI一個新項目的時候,我把這樣一個cookie:
private void SetCookie(string cookieName, string cookieValue)
{
DateTime expireDate = DateTime.Now + TimeSpan.FromDays(1);
string newCookie = cookieName + "=" + cookieValue + ";expires=" + expireDate.ToString("R");
HtmlPage.Document.SetProperty("cookie", newCookie);
}
的cookieName是SelectedProjectId
和cookieValue是我的UI當前選定的項目。
然後我正常創建一個新的DomainService,但我覆蓋CreateObjectContext()
。這個方法在你第一次引用你的DomainService對象時被調用。我的覆蓋看起來是這樣的:
protected override ProjectEntities CreateObjectContext()
{
long projectId = -1;
StringBuilder connection;
if (System.Web.HttpContext.Current.Request.Cookies["SelectedProjectId"] != null)
{
projectId = Convert.ToInt64(System.Web.HttpContext.Current.Request.Cookies["SelectedProjectId"].Value);
}
else throw new Exception("Selected Project ID Exception"); // temporary
// Verify this user has access to the DB just in case it's spoofed
// Lookup project ID in my database to get the database name and server name
// Load template connection string found in web.config
// Replace the template holders for SERVER_NAME and DATABASE_NAME with above lookup values
return new ProjectEntities(MyDynamicConnectionString);
}
再次,這是一個有點hackish,但它是我能找到的動態地創建我需要一個連接字符串的唯一途徑。我希望這可以幫助別人...
WCF數據服務怎麼樣? – vorrtex 2010-12-04 23:54:37