将Asp.Net Core和corefx移植到.Net 4.0

引言

由于工做内容的缘由须要兼容 XP,而 XP 最多支持到.Net Framework 4.0。所以没法享受到 .Net Core 带来的一堆很好用的库,好在不管 corefx 仍是 Asp.Net Core 都是开源的,咱们能够自行修改编译出支持 .Net 4.0 的版本。git

技术可行性

Net 4.0 相比 4.5 和 netstandard 1.0,主要的差异有:github

  • System.Threading.Tasks.Task 类型。.Net 4.0 的 Task 没有 GetAwaiter 成员,编译器没法生成使用 async await 的代码。好在编译器查找 Task.GetAwaiter 并非直接查找成员方法,若是是扩展方法也能够,那么咱们经过对 Task 编写 GetAwaiter 扩展方法就能够在 .Net 4.0 中使用 async await 了。
  • Reflection 类别的 API。.Net 4.5 对反射 API 进行了重构,分离出了 TypeInfo 用于运行时反射。在 .Net 4.0 中,咱们能够自行编写一个 TypeInfo 类,可是 .Net 4.5 中 TypeInfo 继承自 Type。据我观察实现中 GetType 返回的对象实际上就是一个 TypeInfo 对象,这一点咱们没法经过本身编写的 TypeInfo 作到,不过好在除了 mscorlib,其余库并无用到这层继承关系,所以为了避免产生问题我实现的 TypeInfo 没有继承自 Type。
  • WeakReference<T>。.Net 4.5 中的 WeakReference<T> 并无继承自 WeakReference,它的终结器方法是一个 InternalCall,也就是在 clr 中实现的。而咱们没法修改 clr 的实现,所以我实现的 WeakReference<T> 继承自 WeakReference,重用了它的终结器。
  • 其它一些类型中 API 的缺失。主要包括 GC、Cryptography。对于没有的类型,咱们能够添加实现,但对于已存在的类型是没有办法进行修改的。虽然 clr 中有 TypeForwardTo,配合 assembly redirecting 技术能够替换类型的实现,但 .Net 4.0 编译的程序集默认引用了 mscorlib,而 mscorlib 并不能被 redirect,因此对于 mscorlib 中已存在类型的 API 缺失——这一点暂时没有办法解决。

已经移植的项目

corefx

  • System.Runtime:添加了 ExceptionDispatchInfo、IReadOnlyCollection<T>等一些只读集合的接口、生成异步方法所须要的 AsyncStateMachineAttribute、以及使用 MVVM 中很经常使用的 CallerMemberNameAttribute 等
  • System.AppContext:添加了 AppContext 类
  • System.Runtime.CompilerServices.Unsafe:添加了 Unsafe 类(ref 和 指针转换、直接读写内存等)
  • System.Threading:添加了 Volatile 类
  • System.Threading.Tasks:添加支持 async await 相关的类
  • System.Security.Cryptography.Algorithms:添加了 IncrementalHash 的实现

Asp.Net Core

  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Options
  • Microsoft.Extensions.Configuration

这些写过 Asp,Net Core 的应该很熟悉,他们也能够用在普通的 .Net 桌面程序中json

一些开源项目

  • Autofac:一个功能很强大的 IoC 实现
  • AutoMapper:对象间的映射
  • MaterialDesignThemes:WPF 的 MaterialDesign

示例

  • 新建一个 .Net 4.0 项目
  • 在 Nuget 程序包源里加上 https://www.myget.org/F/dotnet40/api/v3/index.json,并将优先级调到最上面
  • install-package System.Threading.Tasks -Version 4.3.0-net40(注意必定要加上 Version)
  • 在 app.config 加上 assembly redirecting
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.12.0" newVersion="4.0.12.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.12.0" newVersion="4.0.12.0" />
      </dependentAssembly>
  •  而后你就能够愉快的 async await 了

 

下面的示例是使用了api

Caliburn.Microapp

Microsoft.Extensions.DependencyInjection异步

Microsoft.Extensions.Configurationasync

Autofacide

Autofac.Extensions.DependencyInjectionui

AutoMapperspa

AutoMapper.Extensions.Microsoft.DependencyInjection

    public class AppBootstrapper : BootstrapperBase
    {
        public IConfiguration Configuration { get; }
        public IServiceProvider ServiceProvider { get; private set; }
        private IContainer _container;

        public AppBootstrapper()
        {
            Configuration = LoadConfiguration();
            Initialize();
        }

        private IConfiguration LoadConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("config.json", false, false);
            return builder.Build();
        }

        protected override void Configure()
        {
            var serviceCollection = new ServiceCollection();
            ServiceProvider = ConfigureServices(serviceCollection);
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            services.AddAutoMapper(AssemblySource.Instance.ToArray());
            services.AddSingleton<IWindowManager>(new WindowManager());
            services.AddSingleton<IEventAggregator>(new EventAggregator());
            
            services.AddSingleton(p => _container);

            var builder = new ContainerBuilder();
            builder.Populate(services);
            builder.RegisterAssemblyModules(AssemblySource.Instance.ToArray());

            _container = builder.Build();
            return new AutofacServiceProvider(_container);
        }
}

 看起来和在 Asp.Net Core 中没什么差异。

总结

虽然工做环境限制咱们只能使用 .Net 4.0,但俗话说没有条件,创造条件也要上。将它们移植到 .Net 4.0 也是跟上 .Net Core 和开源的步伐的一种努力吧。

关于这些包和相关的版本号能够在 https://www.myget.org/feed/Packages/dotnet40 查看

关于移植到 .Net 4.0 的计划我建立了一个 github 组织,里面包含移植的全部项目 https://github.com/dotnet40/

最后,感谢你们花时间阅读!

相关文章
相关标签/搜索