. Net Standard extension supports instance sharing,. netstandard

Source: Internet
Author: User

. Net Standard extension supports instance sharing,. netstandard

Part 1 (. net infrastructure and cross-framework development. net in the Current ecosystem, I also shared the simple implementation process. This article explains my OSS. common Project extension. net Standard supports the following processes: solution selection, porting detection, porting process, solutions to common problems, and nuget packaging and deployment.

Before getting started, I will briefly introduce the content involved in the OSS. Common Project to help you understand the problems encountered later. This class library is a relatively simple basic class library that provides the following content: 1. definition of basic user system device information entity, 2. mainstream encryption and decryption solutions (md5, aes, sha1, hmacsha1), 3. regular object DTO conversion, static Extension Method (time, String, etc.), 4. basic log, cache, asynchronous auxiliary static class and default solution implementation (using the simple Provider mode, users can register their own implementation solution), 5. global result, defined by the paging object. Its main function is to complete the collection and normalization of common broken methods, so as to reduce unnecessary consumption in the business logic. After understanding this, we will enter the specific expansion process.

  I. Solution Selection

This is actually introduced in the previous article, currently. net core ,. net Framework, mono for Xarmain, and so on all have their own runtime. Although C # syntax is used, the class libraries cannot directly call each other without porting or standard libraries. With. net standard 2.0 is coming soon ,. net core (asp.net core) application scenarios will become more and more common, the compatibility requirements for old projects will become more and more strong, oss. this problem is also encountered in common, so I will perform it.. net standard.

Because the current project is currently in several. the. net framework project is still in use. To prevent loss of support for the net45 version in the old project, I will keep two solutions.. net Standard.. net framework is used. Two class library projects share the same set of code files and are controlled by Conditional compilation for Framework-specific functions. The directory structure on github has been updated. Please check it out.

  2. Transplantation Detection

Before porting, we need to make a rough assessment of porting, understand the coverage area of code changes, and determine the portability of code. Here we recommend that you use the official port detection tool (ApiPort) provided by Microsoft ), or use its VS extension. Here I am using the vs plug-in. After installing the plug-in, open the solution. Right-click the plug-in and choose the following two options:

First, click the second option to configure the port comparison version to be checked, such:

After completing the corresponding detection version, click OK. Click the first option to execute the analysis process and generate two types of reports: html and xsl. The html report interface is as follows:

The report will show the interface coverage of the corresponding version and related suggestions, which can be said to be more detailed.

  Iii. porting process

After the above detection, we can see that the oss. the common project is in. under net standard1.4, more than 20% of the Code cannot be directly supported. I have looked at it, mainly in Code related to configuration, cache, reflection, and other special attributes, this is still expected, but it is still a headache to see a pile of Red Cross forks. No way, your own class library will be complete when you cry. Next we will introduce the porting steps.

1. Add a project file

To make the project intuitive and easy to manage, I changed the original OSS. change the Common class library name to OSS. common. NET45: Create an OSS instance. the standard library project of Common. The two project files are placed in the same directory. For details, if you want to create a standard library project in vs2015, you must first create a portable class library and modify it on the class Library Attribute page, if you are not clear, please refer to the previous article.

In this case, if you directly generate OSS. common. for a NET45 project, an error occurs, even if you have not performed any actual code operations, mainly because the project is required to add a portable class library. json files for dependency management. When they are in the same directory, nuget will. the dependency in json is restored by default, although you are currently generating OSS. common. NET45 project, no way, It's so silly. If you encounter this error, create another project corresponding to the current project file in the current directory. the json file is ready. Here I have added OSS. common. net45.project. json file. Add the following code to the file:

{
"Frameworks ":{
"Net45 ":{}
},
"Runtimes ":{
"Win ":{}
}
}

 

2. Code Integration

After the corresponding solution is created, the code file is attached to the newly created standard library. Many errors will be generated directly at this time. At this time, we need to sacrifice the big trick of Conditional compilation, since the standard library will be maintained in the future, I have created a Conditional compilation symbol for NETFW on the old NET45 project, and the rest is a complete set of errors.

In the process of processing compatibility, the main problems are: 1. the standard library does not support both. the call methods of the standard library and Framework are different. 3. indirectly implementing the Standard Library

Here I will give an example of my situation for your reference:

1. the standard library is not supported at all. This is the most typical cache module. net standard, System. runtime. the Caching class library is completely removed. No way, you can only use it. # if NETFW completely blocks the default Cache in the Module, the default implementation can only be used under the Framework (originally intended to implement a cache class, but it may cause unpredictable bugs and be voided ).

2. The call methods of the standard library and Framework are different. For example, the IsEnum attribute of the Type is required under the net standard. gettypeinfo (). IsEnum can be used. Example code:

# If NETFW
If (! EnType. IsEnum)
# Else
If (! EnType. GetTypeInfo (). IsEnum)
# Endif

3. the implementation of the standard library can be indirectly completed. This common method is the list ConvertAll method, which is implemented by default in the Framework, but not in the standard library, here, I have defined a ConvertExtention class:

# If! NET40
Public static List <TResult> ConvertAll <TPara, TResult> (this List <TPara> list, Func <TPara, TResult> func)
{
If (list = null)
Return null;
Var resultList = new List <TResult> (list. Count );
List. ForEach (e => resultList. Add (func (e )));
Return resultList;
}
# Endif

Of course there will be other problems, but fortunately, they have basically been solved. If you have any questions, you can download the oss. common Code and view it on your own.

 

  Iv. nuget packaging and deployment

This is relatively simple. Generate the corresponding dll in the two solutions, and add the corresponding dll to the net45 and netstandard1.4 folders respectively in the lib folder.

Note that it is best to add a dependency. For example, the Hmacsha1 encryption algorithm of the standard library is in "System. security. cryptography. in the Algorithms dll assembly, if this dll is not referenced in the call project, no error will be reported. However, when the Code executes the call, an error not found in the Assembly will pop up, if you find this problem, you can install it using the nuget online installation command (install-package.

Let's take a look at the configuration of my nuget file:

 

If you have other questions, follow the Public Account (osscoder ):

 

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.