我有一个使用RIA服务的棱镜应用程序,我的身份验证服务位于一个单独的RIA类库中。
该程序在运行时运行良好。身份验证等等。
不过,在设计时,我遇到了一个严重的错误,它使visual和and都崩溃了。当我打开解决方案时,混合物会立即崩溃。当我关闭特定的视图(页面)时,Visual将崩溃。不过,至少混合之后给了我一个撞车日志。该错误与我将视图模型的一个实例设置为xaml视图的数据文本有关,而不是在运行时设置/注入一个实例。
因此,当在运行时设置viewmodel数据文本时,将在设计器中加载视图时调用viewmodel构造函数。当视图关闭时,将调用析构函数。这就是我得到的异常,崩溃的vs/混合。因此,这里有一个例外:
System.InvalidOperationException:当前WebContext实例不可用。您必须实例化一个WebContext并将其添加到默认应用程序构造函数中的Application.ApplicationLifetimeObjects中。在System.ServiceModel.DomainServices.Client.ApplicationServices.WebContextBase.get_Current() at MyClassLibrary.WebContext.get_Current() at MyShellProject.ShellViewModel.Finalize()
这很奇怪,因为我在应用程序构造函数中实例化一个WebContext。运行时也没有问题。只有当我将视图模型的一个实例添加为数据文本时,我才会在运行时得到崩溃。
所以在我的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不知道我实例化了一个WebContext?
发布于 2012-01-20 23:00:39
假设在ViewModel中有以下变量:
public bool IsDesignTime
{
get
{
return DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual);
}
}您可以包装一段代码,该代码试图在设计时使用该变量的值实例化WebContext,然后返回一个模拟对象。不过,我认为WebContext在应用程序运行和连接时是实例化的。迟回复,但可能对某人有用。
https://stackoverflow.com/questions/7288244
复制相似问题