2011-09-02 94 views
1

我有一個使用RIA服務的prism應用程序,我的身份驗證服務位於單獨的RIA類庫中。Silverlight:WebContext不可用異常崩潰visual studio

程序在運行時正常工作。身份驗證和所有。

雖然我在設計時遇到了一個令人討厭的錯誤,但它同時使Visual Studio和Blend崩潰。當我打開解決方案時,Blend會立即崩潰。關閉特定視圖(頁面)時,Visual Studio將崩潰。儘管至少混合給了我一個崩潰日誌。這個錯誤與我設置我的viewmodel的一個實例作爲xaml視圖的datacontext,而不是在運行時設置/注入一個。

因此,在運行時設置視圖模型datacontext時,在設計器中加載視圖時會調用viewmodel構造函數。當視圖關閉時,調用析構函數。這是我得到的異常,崩潰VS /混合。因此,這裏是個例外:

System.InvalidOperationException:WebContext 的當前實例不可用。您必須實例化一個WebContext並將其添加到默認應用程序構造函數 中的Application.ApplicationLifetimeObjects中。在 System.ServiceModel.DomainServices.Client.ApplicationServices.WebContextBase.get_Current() 在MyClassLibrary.WebContext.get_Current()在 MyShellProject.ShellViewModel.Finalize()

很奇怪,因爲我在實例化一個WebContext我的App構造函數。在運行時沒有問題。只是因爲我添加了我的viewmodel的一個實例作爲datacontext我在運行時遇到崩潰。

所以在我的App.xaml我:

<Application.ApplicationLifetimeObjects> 
     <MyClassLibrary:WebContext> 
      <MyClassLibrary:WebContext.Authentication> 
       <ApplicationServices:FormsAuthentication> 
        <ApplicationServices:FormsAuthentication.DomainContext> 
         <MyClassLibrary_Web:MyAuthenticationContext /> 
        </ApplicationServices:FormsAuthentication.DomainContext> 
       </ApplicationServices:FormsAuthentication> 
      </MyClassLibrary:WebContext.Authentication> 
     </MyClassLibrary:WebContext> 
    </Application.ApplicationLifetimeObjects> 

我也試着落後於應用程序構造的該等效代碼。兩者都有相同的結果。

本博客文章有關於我的項目是如何建立更多的細節:http://avcode.wordpress.com/2010/08/25/authenticaion-prism-wcf-ria-services/

任何人都有一個線索,爲什麼在設計時,Visual Studio是不知道,我已經實例化的WebContext?

回答

2

比方說,你有以下變量視圖模型:

public bool IsDesignTime 
{ 
    get 
    { 
    return DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual); 
    } 
} 

你可以用一段代碼它試圖使用該變量的值在設計時實例化WebContext並返回一個嘲笑的對象來代替。我認爲WebContext在應用運行和連接時被實例化,但不確定。逾期回覆,但可能對某人有用。

+0

我總是繼續崩潰。但是,是的,檢測設計時間似乎是正確的方向。儘管我用設計時間檢測包裹了我的訪問器屬性,但我仍然遇到了崩潰。我永遠無法弄清楚在設計時如何訪問它,但一定有幕後的東西。無論哪種方式,我的銀光之日已經結束,所以它是無關緊要的。 – Adam

+0

這是正確的答案,儘管你可以簡化事物並使用'DesignerProperties.IsInDesignTool'。將Visual Studio的另一個實例附加到運行Silverlight項目的實例,並繼續重新加載失敗的頁面,直到找到引用WebContext的所有位置(請參閱堆棧跟蹤)。更多詳細信息可以在[此錯誤報告]中找到(http://connect.microsoft.com/VisualStudio/feedback/details/574316/webcontext-not-available)。 – Pakman