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

查看:74
本文介绍了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格式打开项目文件,并将以下行粘贴到第一个< PropertyGroup>

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.

加载项目,执行添加引用/浏览,找到 C:\Program文件(x86)\Windows套件\10\UnionMetadata\Windows.winmd ,然后添加。

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

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

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;

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.

无论如何,您都有将 AppDomain LoaderOptimization 策略更改为 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.

灵感:>演练:从Windows桌面应用程序使用WinRT库戴维·摩尔(David Moore)

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

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