Port Asp. Net Core and corefx to. Net 4.0 and corefx

Source: Internet
Author: User
Tags mscorlib

Port Asp. Net Core and corefx to. Net 4.0 and corefx
Introduction

XP compatibility is required because of work content, and XP supports. Net Framework 4.0 at most. Therefore, it cannot be enjoyed. net Core brings a bunch of very useful libraries, but fortunately no matter corefx or Asp. net Core is open-source, and we can modify and compile the support by ourselves.. Net 4.0.

Technical feasibility

Net 4.0 compared with 4.5 and netstandard 1.0, the main differences are:

  • System. Threading. Tasks. Task Type .. Net 4.0 tasks do not have a GetAwaiter member, and the compiler cannot generate code that uses async await. Fortunately, the compiler looks for tasks. getAwaiter does not directly search for the member method. If it is an extension method, you can compile the GetAwaiter Extension Method on the Task. net 4.0 uses async await.
  • Reflection class API .. Net 4.5 reconstructed the reflection API and separated TypeInfo for runtime reflection. In. Net 4.0, We can compile a TypeInfo class, but in. Net 4.5, TypeInfo inherits from Type. According to my observation, the object returned by GetType is actually a TypeInfo object, which cannot be achieved through TypeInfo written by myself. Fortunately, apart from mscorlib, other libraries do not use this inheritance relationship, therefore, in order not to cause problems, the TypeInfo implemented by me does not inherit from the Type.
  • WeakReference <T> .. The WeakReference <T> in Net 4.5 does not inherit from WeakReference. Its Terminator method is an InternalCall, which is implemented in clr. However, we cannot modify the clr implementation. Therefore, the implemented WeakReference <T> inherits from WeakReference and reused its terminator.
  • Other types of APIs are missing. It mainly includes GC and Cryptography. For non-existing types, we can add implementations, but there is no way to modify existing types. Although the clr has TypeForwardTo, the assembly redirecting technology can replace the type implementation. mscorlib is referenced by the Assembly compiled by Net 4.0 by default, and mscorlib cannot be redirect. Therefore, there is a lack of APIs of the type in mscorlib-this cannot be solved for the time being.
Transplanted project corefx
  • System. Runtime: added the ExceptionDispatchInfo, IReadOnlyCollection <T> and other read-only set interfaces, AsyncStateMachineAttribute required for generating asynchronous methods, and CallerMemberNameAttribute, which is commonly used in MVVM.
  • System. AppContext: added the AppContext class.
  • System. Runtime. CompilerServices. Unsafe: added the Unsafe class (ref and pointer conversion, direct read/write memory, etc)
  • System. Threading: added the Volatile class.
  • System. Threading. Tasks: add classes that support async await
  • System. Security. Cryptography. Algorithms: added the implementation of IncrementalHash.
Asp. Net Core
  • Microsoft. Extensions. DependencyInjection
  • Microsoft. Extensions. Options
  • Microsoft. Extensions. Configuration

Net Core should be familiar with Asp and they can also be used in common. Net desktop programs.

Some open-source projects
  • Autofac: A Powerful IoC implementation
  • AutoMapper: ing between objects
  • MaterialDesignThemes: WPF's MaterialDesign
Example
  • Create a. Net 4.0 Project
  • Add https://www.myget.org/f/dotnet40/api/v3/index.jsonto the Nuget package source and set the priority level to the top
  • Install-package System. Threading. Tasks-Version 4.3.0-net40 (Be sure to add Version)
  • Add assembly redirecting to app. config
      <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>
  • Then you can enjoy async await.

 

The following example uses

Caliburn. Micro

Microsoft. Extensions. DependencyInjection

Microsoft. Extensions. Configuration

Autofac

Autofac. Extensions. DependencyInjection

AutoMapper

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);        }}

It seems similar to Asp. Net Core.

Summary

Although we can only use. Net 4.0 in the working environment, as the saying goes, there are no conditions, and the conditions must be met. Porting them to. Net 4.0 is also an effort to keep up with. Net Core and open source.

The packages and their associated versions can be viewed in the https://www.myget.org/feed/Packages/dotnet40

About the program for porting to. Net 4.0 I created a github organization that contains all the project https://github.com/dotnet40/ for porting

Finally, thank you for your time!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.