ASP.NET 中的 UWP API [英] UWP API in ASP.NET

查看:17
本文介绍了ASP.NET 中的 UWP API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ASP.NET 代码隐藏 DLL 中使用一段特定于 Windows 10 的 UWP API(特别是 Windows.Graphics.Printing3D 内容).有什么办法吗?

I'd like to use a piece of Windows 10 specific UWP API (specifically, the Windows.Graphics.Printing3D stuff) in an ASP.NET code-behind DLL. Is there any way to do so?

推荐答案

以 XML 格式打开项目文件,并将以下行粘贴到第一个 下:

Open the project file as XML, and paste the following line under the first <PropertyGroup>:

<TargetPlatformVersion>10.0</TargetPlatformVersion>

执行此操作后,添加引用"对话框将包含 UWP 库,浏览..."对话框中的文件类型选项将包含 .winmd.

Once you do that, the Add reference dialog will include UWP libraries, and the file type options in the "Browse..." dialog there will include .winmd.

加载项目,执行 Add reference/Browse,找到 C:Program Files (x86)Windows Kits10UnionMetadataWindows.winmd,添加.

Load the project, do Add reference/Browse, locate C:Program Files (x86)Windows Kits10UnionMetadataWindows.winmd, add that.

托管程序集System.Runtime.WindowsRuntime中有一些有用的扩展方法(例如IBuffer.AsStream()),但由于某种原因,它没有列在组件.要引用它,您需要直接编辑项目文件,并在第一个 下添加以下内容:

There are some helpful extension methods in the managed assembly System.Runtime.WindowsRuntime (e. g. IBuffer.AsStream()), but for some reason, it's not listed under Assemblies. To reference it, you'd need to edit the project file directly, and under the first <ItemGroup>, add the following:

<Reference Include="System.Runtime.WindowsRuntime" />

与指南所述不同,您不需要将编译目标更改为 x86 或 x64;离开 AnyCPU.

Unlike the guide states, you don't need to change the compilation target to x86 or x64; leave AnyCPU be.

对于桌面 .NET 应用程序,这已经足够了.然而,对于 ASP.NET,有一个问题.ASP.NET 运行时设置其 AppDomain 的方式与 UWP 不兼容.这可能是一个深层次的错误,但我已经报告了它,微软代表说整个事情一开始就不受支持.

For desktop .NET applications, this is sufficient. For ASP.NET, however, there's a catch. The way the ASP.NET runtime sets up its AppDomains not compatible with UWP. It's probably a bug deep down, but I've reported it, and a Microsoft rep said the whole thing was not a supported scenario to begin with.

无论如何,您必须将AppDomainLoaderOptimization 策略更改为SingleDomain.最快的方法是通过滥用 AppDomain 的私有方法:

Anyway, you have to change the LoaderOptimization policy of the AppDomain to SingleDomain. The quickest way to do so is via abusing a private method of AppDomain:

AppDomain ad = AppDomain.CurrentDomain;
MethodInfo mi = ad.GetType().GetMethod("SetupLoaderOptimization", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(ad, new object[] { LoaderOptimization.SingleDomain });

在应用启动代码中可以做到这一点.

A good place to do that would be in the app startup code.

另一种危险性稍低的方法是创建一个新的 AppDomain,它将继承当前设置的所有设置属性,但 LoaderOptimization 将设置为 SingleDomain,并在该域中运行 UWP 相关代码.像这样:

A slightly less dangerous approach would involve creating a new AppDomain, which would inherit all setup properties from the current one but LoaderOptimization, which will be set to SingleDomain, and running the UWP dependent code in that domain. Like this:

AppDomain CreateUnsharingDomain()
{
    AppDomain cad = AppDomain.CurrentDomain;
    AppDomainSetup cads = cad.SetupInformation;
    return AppDomain.CreateDomain("Dummy", cad.Evidence,
        new AppDomainSetup
        {
            ApplicationName = cads.ApplicationName,
            ApplicationBase = cads.ApplicationBase,
            DynamicBase = cads.DynamicBase,
            CachePath = cads.CachePath,
            PrivateBinPath = cads.PrivateBinPath,
            ShadowCopyDirectories = cads.ShadowCopyDirectories,
            ShadowCopyFiles = cads.ShadowCopyFiles,
            ApplicationTrust = cads.ApplicationTrust,
            LoaderOptimization = LoaderOptimization.SingleDomain
        });
        //Not sure which other properties to copy...
}

CreateUnsharingDomain().DoCallBack(MyUWPDependentMethod);

同样,创建一次域并在应用程序生命周期内缓存它是有意义的.

Again, it would make sense to create the domain once and cache it for the app lifetime.

这种方法可能比使用猴子补丁 AppDomain 的方法更快.多域优化的存在是有原因的;如果您将大部分 Web 代码保留在 MultiDomain 世界中,优化将按预期工作.

This approach might be faster than the one with the monkey-patched AppDomain. The MultiDomain optimization exists for a reason; if you leave most of the Web code in a MultiDomain world, the optimization will do its work as intended.

灵感:David Moore 的演练:使用 Windows 桌面应用程序中的 WinRT 库".

这篇关于ASP.NET 中的 UWP API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆