我有一个使用RIA服务的prism应用程序,我的身份验证服务位于一个单独的RIA类库中。
该程序在运行时工作正常。身份验证和所有。
但是,我在设计时遇到了一个令人讨厌的错误,它会使视觉工作室和混合都崩溃。打开解决方案时,Blend会立即崩溃。关闭特定视图(页面)时,Visual Studio将崩溃。至少混合物给了我一个崩溃日志。该错误与我设置我的viewmodel实例作为xaml中视图的datacontext而不是在运行时设置/注入一个实例。
因此,在运行时设置viewmodel datacontext时,在设计器中加载视图时会调用viewmodel构造函数。当视图关闭时,析构函数被调用。这就是我遇到崩溃vs / blend的异常。所以这是例外:
System.InvalidOperationException:WebContext的当前实例 不可用。您必须实例化WebContext并将其添加到 Application.ApplicationLifetimeObjects在默认App中 构造函数。在 System.ServiceModel.DomainServices.Client.ApplicationServices.WebContextBase.get_Current() 在MyClassLibrary.WebContext.get_Current()中 MyShellProject.ShellViewModel.Finalize()
这很奇怪,因为我在我的App构造函数中实例化一个WebContext。并且在运行时没有问题。只是因为我将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>
我也在App构造函数中尝试了相当于此代码的代码。两者都有相同的结果。
此博客文章详细介绍了我的项目设置方式:http://avcode.wordpress.com/2010/08/25/authenticaion-prism-wcf-ria-services/
任何人都有一个线索,为什么在设计时,visual studio不知道我已经实例化了一个WebContext?
答案 0 :(得分:2)
假设您在ViewModel中有以下变量:
public bool IsDesignTime
{
get
{
return DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual);
}
}
您可以使用此变量的值包装尝试在设计时实例化WebContext的代码片段,然后返回Mocked对象。我认为WebContext是在App运行和连接时实例化的,但不确定。迟到的回复但可能对某人有用。