I. Introduction
The webmaster has been in contact with the AOT for 3 months, and has previously written about it in theGood News: NET 9 X86 AOT Breakthrough - Support for Older Win7 & XP EnvironmentsIt was mentioned in the article "AOT". During this time, the project developed by the webmaster using Avalonia also successfully completed the AOT release test. However, the process was not a smooth one. The webmaster started AOT testing only after most of the project's functionality was completed, during which time he encountered a lot of problems and "stepped on a lot of potholes", so to speak. In order to facilitate future review, but also in order to provide reference to the majority of readers, here will summarize this experience.
NET AOT is a technology that compiles .NET code ahead of time into native code. The advantages are numerous, faster startup, reduced runtime resource usage, and also improved security.NET runtime and other dependencies do not need to be installed after the release of AOT. NET 8, 9 AOT release can be run under XP, Win7 non-SP1 operating systems. This makes application deployment more convenient, can adapt to more old system environments, for developers to expand the application scenarios, in the performance improvement at the same time, but also increased system compatibility, so that the development and deployment of .NET applications more flexible and broader, to bring better user experience.
II. Lessons learned
(i) Importance of testing strategy
From the beginning of the creation of the project, you should develop a good habit, that is, as long as the addition of new features or the use of newer syntax, it is timely AOT release test. Otherwise, the problem accumulates to the later stage, the solution will be exceptionally difficult, the webmaster ignored this point because of the early stage, paid a terrible price. The helpless solution is to re-create the project, and then restore the features one by one and conduct AOT testing. After a week of overtime AOT testing, each AOT release process is roughly as follows:
- It takes 2 or 3 minutes for an intranet AOT to be published, during which time you can only look at the requirements document, technical articles, requirements document, technical articles.
- Published, run no effect, reflected in the double-click did not appear interface, the process list does not have it, indicating that the program crashed, check the system application event log, the log will usually contain abnormal warning information.
- Check the code against the log messages and modify the relevant APIs.
- Repeat steps 1 - 3 above for another AOT release.
After a week of hard work, the project was finally tested properly after AOT, and the project was wrapped up.
(ii) Points to note and solutions for AOT
1. Add
Create an XML file in the main project, for exampleThe content is roughly as follows:
<linker>
<assembly fullname="" preserve="All" />
</linker>
Projects that require AOT support, add to that XML aassembly
Nodes.fullname
is the name of the program set.is the name of the main project of the webmaster's gadget.strike (on the keyboard)View source.
In the main project addItemGroup
The node associates this XML file:
<ItemGroup>
<TrimmerRootDescriptor Include="" />
</ItemGroup>
2. Prism support
The webmaster uses the Prism framework and DryIOC containers, to support AOT, the following NuGet packages need to be added:
<PackageReference Include="" Version="8.1.97.11073" />
<PackageReference Include="" Version="8.1.97.11073" />
Need to add
<assembly fullname="Prism" preserve="All" />
<assembly fullname="DryIoc" preserve="All" />
<assembly fullname="" preserve="All" />
<assembly fullname="" preserve="All" />
3. Reading and writing
NET Core using thepackage operation file.
The following needs to be added:
<assembly fullname="" preserve="All" />
utilization().location
Failed, currently using()
The application program configuration of the acquisition, the way to specify the path is examined later.
4. HttpClient use
Add the following:
<assembly fullname="" preserve="All" />
5. Dapper support
Dapper's AOT support requires the installation ofPackage.
Add the following:
<assembly fullname="Dapper" preserve="All" />
<assembly fullname="" preserve="All" />
The methods for database operations need to be addedDapperAOT
characteristics, examples of which are given below:
[DapperAot]
public static bool EnsureTableIsCreated()
{
try
{
using var connection = new SqliteConnection();
();
const string sql = $@"
CREATE TABLE IF NOT EXISTS {nameof(JsonPrettifyEntity)}(
{nameof()} Bool,
{nameof()} INTEGER
)";
using var command = new SqliteCommand(sql, connection);
return () > 0;
}
catch (Exception ex)
{
return false;
}
}
6.
consultation
serialize
public static bool ToJson<T>(this T obj, out string? json, out string? errorMsg)
{
if (obj == null)
{
json = default;
errorMsg = "Please provide object";
return false;
}
var options = new JsonSerializerOptions()
{
WriteIndented = true,
Encoder = ,
TypeInfoResolver = new DefaultJsonTypeInfoResolver()
};
try
{
json = (obj, options);
errorMsg = default;
return true;
}
catch (Exception ex)
{
json = default;
errorMsg = ;
return false;
}
}
deserialization
public static bool FromJson<T>(this string? json, out T? obj, out string? errorMsg)
{
if ((json))
{
obj = default;
errorMsg = "Please provide json string";
return false;
}
try
{
var options = new JsonSerializerOptions()
{
Encoder = ,
TypeInfoResolver = new DefaultJsonTypeInfoResolver()
};
obj = <T>(json!, options);
errorMsg = default;
return true;
}
catch (Exception ex)
{
obj = default;
errorMsg = ;
return false;
}
}
7. Reflections
Reference projects
- Creates the specified type of
List<T>
maybeDictionary<T>
Example:
public static object CreateInstance(Type type)
{
var itemTypes = ();
if (typeof(IList).IsAssignableFrom(type))
{
var lstType = typeof(List<>);
var genericType = (());
return (genericType)!;
}
else
{
var dictType = typeof(Dictionary<,>);
var genericType = ((), itemTypes[1]);
return (genericType)!;
}
}
- reflect a call
List<T>
cap (a poem)Dictionary<T>
(used form a nominal expression)Add
method fails to add an element, here is the pseudo-code:
// List<T>
var addMethod = ("Add");
(obj, new[]{ child })
// Dictionary<Key, Value>
var addMethod = ("Add");
(obj, new[]{ key, value })
Solution, convert to an implemented interface call:
// List<T>
(obj as IList).Add(child);
// Dictionary<Key, Value>
(obj as IDictionary)[key] = value;
- Get the array,
List<T>
、Dictionary<key, value>
number of elements
Same as the above Add method reflection to get the Length or Count attribute are returned to 0.("Length", 0)
, the encapsulated Property non-AOT runs correctly:
public static T Property<T>(this object obj, string propertyName, T defaultValue = default)
{
if (obj == null) throw new ArgumentNullException(nameof(obj));
if ((propertyName)) throw new ArgumentNullException(nameof(propertyName));
var propertyInfo = ().GetProperty(propertyName);
if (propertyInfo == null)
{
return defaultValue;
}
var value = (obj);
try
{
return (T)(value, typeof(T));
}
catch (InvalidCastException)
{
return defaultValue;
}
}
AOT success: just call the property directly by converting to a base type or an implemented interface:
// arrays
var length = ((Array)value).Length;
// List<T>
if (value is IList list)
{
var count = ;
}
// Dictionary<key, value>
if (value is IDictionary dictionary)
{
var count = ;
}
8. Windows 7 support
In the event that the AOT is not available after theWindows 7
to run, please addYY-Thunks
Package:
<PackageReference Include="YY-Thunks" Version="1.1.4-Beta3" />
and specify the target frame asnet9.0-windows
。
9. Winform\compatible with XP
If it doesn't run after #8, please refer to the previous article,NET 9 AOT breakthrough - support for older Win7 and XP environments - CodeWorks ()Add the VC-LTL package, which is not described here.
10. Other
There are many other things that need to be taken care of, and I will gradually improve this article as I think about it subsequently.
III. Summary
AOT release test although the process may encounter many problems, but through timely testing and correct configuration adjustments, and ultimately be able to achieve a smooth release of the project. I hope that the above summarized experience can help you in the process of using AOT, so that we can make less detours in the development process, improve the efficiency and quality of project development. At the same time, we also look forward to the practice of continuous exploration and summarization, and jointly promote the progress and development of technology.
AOT may refer to the project:
- : /dotnet9/
- :/dotnet9/
- :/dotnet9/