开发者

Instantiate a Silverlight control in an HTTP Handler,...Possible?

开发者 https://www.devze.com 2023-01-27 14:20 出处:网络
I have an HTTP Handler in an ASP.Net project and wanted to produce an image based upon a Silverlight User Control rendered out as a bitmap.

I have an HTTP Handler in an ASP.Net project and wanted to produce an image based upon a Silverlight User Control rendered out as a bitmap.

However, in the ProcessRequest开发者_如何学C, when I try to instantiate any Xaml control, I get the following exception....

{"The type initializer for 'MS.Internal.JoltHelper' threw an exception."}

... with the following stacktrace ...

at MS.Internal.JoltHelper.get_ThreadID() at MS.Internal.XcpImports.CheckThread()

at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO)

at System.Windows.Controls.UserControl..ctor() at CaseCard..ctor() in C:!WORKING_FOLDER\Trunk\ProActive\ProActive.UI\CaseCard.xaml.vb:line 6 at ServiceLocator.GetCard() in C:!WORKING_FOLDER\Trunk\ProActive\ProActive.UI\ServiceLocator.vb:line 20 at ProActive.Host.ImageGeneratorHandler.ProcessRequest(HttpContext context) in c:!WORKING_FOLDER\Trunk\ProActive\ProActive.Host\App_Code\ImageGeneratorHandler.cs:line 10 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

... and the inner exception is this ...

{"Could not load file or assembly 'System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.":"System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"}

Now,..I inherited this project a couple of months ago and it's 'in awful condition. I'm battling to get it back on track but there are some real issues somewhere so it's entirely possible that something is environmentally amiss.

That said, I'm starting to wonder if this is possible since Silverlight runs on a totally different framework. Do I really have to go back to GDI+ to get this done?

Any and all help is greatly appreciated.


The reason this doesn't work is because your ASP code is running in the context of the full .NET framework and while trying to instantiate Silverligt control - just like the exception says - the assembly System.Xml with the version 2.0.5.0 is not loadable/found.

Note the version: 2.0.5.0. All of the Silverlight core DLLs have this version instead of 'normal' .NET version like 2.0.0.0.

This is no accident - Silverlight runs inside the context of the small CLR which is a subset of the regular CLR. It's not the same CLR, though most of it is completely identical and while your C# source code is exactly the same, the runtime isn't identical.

At the moment, you can't mix Silverlight code with the full .NET based code like ASP.NET.

There is some work being done inside Microsoft on extracting common core of regular .NET and Silverlight (a couple of libraries actually) for better sharing, but that's not yet publically available.

Also note that if the source code of one of your Silverlight 4.0 libraries does not use Silverlight specific code, you are able to load it into regular .NET process, but the reverse is obviously not possible (Silverlight being the subset, not the other way around).

All this aside, you might achieve your goal by:

  1. Running your Silverlight app OOB on the server and communicating with the ASP.NET pages via some IPC mechanism
  2. Recompiling the code into WPF and load that; when I say recompile, I really mean adapt/refactor since Silverlight UI is not a pure subset of WPF

Neither is particularity easy, though I'd lean towards the second option it's cleaner.

0

精彩评论

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