Last week I experienced a very annoying unhandled exception in XAML designer of Visual Studio 2008 SP1. It happened after installing of preview and beta tools for Silverlight 3, which was a mistake, because I installed them onto Silverlight 2 tools. When I installed the tools Expression Blend was working properly, but when I wanted to edit the code and use IntelliSense, the only choice was to switch to Visual Studio. Unfortunatelly, the Visual Studio was unable to render any XAML and shown an exception only.
Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(String assemblyName, String typeName)
at System.AppDomain.CreateInstance(String assemblyName, String typeName)
at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
at MS.Internal.Package.VSIsolationProviderService.CreateIsolationProvider(String identity, AssemblyReferenceProvider assemblyReferences, IEnumerable`1 assemblyFolders)
at MS.Internal.Providers.VSDesignerContext.GetIsolationProvider(IServiceProvider provider, IVsHierarchy hierarchy, AssemblyReferenceProvider assemblyReferences, Boolean isSilverlightProject)
at MS.Internal.Providers.VSDesignerContext.GetIsolationProvider(IServiceProvider provider, IVsHierarchy hierarchy, AssemblyReferenceProvider assemblyReferences)
at MS.Internal.Providers.VSDesignerContext.Initialize(IServiceProvider provider, IVsHierarchy hierarchy, UInt32 itemid, Object docDataObj)
at MS.Internal.Providers.VSDesignerContext..ctor(IServiceProvider provider, IVsWindowFrame frame, Object docDataObj)
at MS.Internal.Providers.VSDesignerContext.GetContext(IServiceProvider services, IVsWindowFrame frame, Boolean createIfNotExist)
at MS.Internal.Designer.DesignerPane.InitializeDesigner()

I tried to google some information and found two different scenarios where this exception is thrown.
- When running ASP.NET application
- Trying to open XAML in Visual Studio
This exception was very familiar with Silverlight 2 beta. In most cases, it doesn't hamper to compile any source code, but with this error you'll lose the ability to see rendered XAML and use IntelliSense. You'll be able to work with xaml code itself only. If you get the following error when compiling, you don't have a luck.
Error 1 The "ValidateXaml" task failed unexpectedly.
MS.Internal.Xaml.XamlTypeResolutionException: XAML Namespace http://schemas.microsoft.com/winfx/2006/xaml is not resolved. ---> MS.Internal.Xaml.XamlNamespaceException: Uri http://schemas.microsoft.com/winfx/2006/xaml was not found.
--- End of inner exception stack trace ---
at MS.Internal.Xaml.Schema.ClrNamespace.GetDirective(String name)
at MS.MarkupCompiler.ValidationPass.ValidateXaml(String fileName, Assembly[] assemblies, Assembly callingAssembly, TaskLoggingHelper log, Boolean shouldThrow)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute()
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute()
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.Execute()
at Microsoft.Build.BuildEngine.TaskEngine.ExecuteInstantiatedTask(EngineProxy engineProxy, ItemBucket bucket, TaskExecutionMode howToExecuteTask, ITask task, Boolean& taskResult)
I found many articles and posts about this error, but none worked. Here is a list of steps that you can try to cope with the second scenario.
- Open a second instance of Visual Studio and try to open XAML.
- Close all .xaml and .xml files, close Visual Studio, re-open it and open .xaml file.
- Disable all add-ins in Tools -> Add-in manager
- "Reset all settings" in Visual Studio.
- Install CLR patch (KB963676) that should help with fixing of XAML designer. There is a known feedback from Microsoft which states that this should help. Download here. The *.msu are patches for Windows Vista or Windows Server 2008. The *.exe for Windows XP or Windows Server 2003. Even if you are using 64-bit machines, you still need x86 patch since Visual Studio 2008 is a 32-bit application.
- Uninstall all Silverlight tools, runtime, SDKs, Expression Blend. After this, repair Visual Studio.
- Reinstall Visual Studio and .NET Framework.
If none works, you will have to reinstall the OS, like me.
Here is a list of steps when you are experiencing the first scenario.
- Stop IIS and delete files in "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files"
I hope that some of these steps will help you.
And my advice? Never install any beta or preview versions on your main development system. Always, keep it clean. For testing create a virtual pc disk. With virtual pc disk, you can always take your disk backup and restore it very easily. Personally, I prefer VirtualPC 2007 SP1, because it's free, clean and easy to use.