开发者

Silverlight: WebContext is not available exception crashes visual studio

I have a prism app using RIA services, and my authentication service is in a separate RIA class library.

The program works fine at runtime. Authentication and all.

I am getting a nasty error at design time, though, which crashes both visual studio and blend. Blend will crash immediately when I open the solution. Visual studio will crash when I close the particular view (page). At least blend gave me a crash log after though. The error has to do with me setting an instance of my viewmodel as the datacontext of the view in xaml rather than setting/injecting one at runtime.

So when setting a viewmodel datacontext at runtime, the viewmodel constructor is called when the view is loaded in the designer. And when the view is closed the destructor is called. That is where I am getting the exception that crashes vs/blend. So here is the exception:

System.InvalidOperationException: The current instance of WebCo开发者_如何学Cntext is not available. You must instantiate a WebContext and add it to Application.ApplicationLifetimeObjects within the default App constructor. at System.ServiceModel.DomainServices.Client.ApplicationServices.WebContextBase.get_Current() at MyClassLibrary.WebContext.get_Current() at MyShellProject.ShellViewModel.Finalize()

It's odd because I am instantiating a WebContext in my App constructor. And there are no problems at runtime. Only since I added an instance of my viewmodel as the datacontext I get the crash at runtime.

So in my App.xaml I have:

<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>

I did also try the code behind equivalent of this in the App constructor. Both have the same result.

This blog post has a little more detail about how my project is set up: http://avcode.wordpress.com/2010/08/25/authenticaion-prism-wcf-ria-services/

Anyone have a clue as to why at design time, visual studio is not aware that I have instantiated a WebContext?


Let's say you have the following variable in ViewModel:

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

You can wrap the piece of code which tries to instantiate the WebContext at design time using this variable's value and return a Mocked object instead. I think WebContext is instantiated when the App is running and connected, not sure though. Late reply but may be useful to someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜