Updated the instructions for console app.

This commit is contained in:
Shin Mao 2017-11-07 18:13:29 -08:00 committed by GitHub
parent 80ff947ddc
commit 65e7766214
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,61 +1,54 @@
# Using Microsoft XmlSerializer Generator on .Net Core
# Using Microsoft Xml Serializer Generator on .Net Core
When a running application creates an instance of XmlSerializer, it generates a temporary assembly containing the implementation of the serializer. This happens once for each type for which an XmlSerializer is needed. To improve performance, we can use Microsoft XmlSerializer Generator to pre-generate code for an XmlSerializer for each specified type in a project and compile it into an assembly. This assembly will then be used by the application when creating an XmlSerializer instead of generating a temporary one.
Like the Xml Serializer Generator (sgen.exe) on desktop, Microsoft.XmlSerializer.Generator NuGet package is the solution for .NET Core and .NET Standard Libraries. It creates an Xml serialization assembly for types contained in an assembly in order to improve the startup performance of Xml serialization when serializing or de-serializing objects of those types using XmlSerializer.
You can start using the tool today following the instructions below.
## Prerequisition
Install the latest 2.0 build released on Oct. on your machine from the following link since the earlier version won't work
* [.NET Core runtime Release/2.0.X build](https://github.com/dotnet/core-setup#daily-builds).
* Install [.Net Core SDK of version 2.0.X](https://www.microsoft.com/net/download/windows).
* Install the latest [.NET Core runtime Release/2.0.X build](https://github.com/dotnet/core-setup#daily-builds).
 
Note: You would need to set the project's `RuntimeFrameworkVersion` to the build version of the runtime, which can be found on the download page. For example, if the build version is `2.0.4-servicing-25831-01`, add the following lines in the project's .csproj,
The version number is just above the link of the installer e.g. version 2.0.4-servicing-25831-01. You need specify the version through RuntimeFrameworkVersion in your .csproj like the following.
```xml
```xml
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RuntimeFrameworkVersion>2.0.4-servicing-25831-01</RuntimeFrameworkVersion>
</PropertyGroup>
```
```
## Instructions
Here are the step by step instructions how to create a dotnet project and install the XmlSerializer Generator for that project.
Here are the step by step instructions on how to Xml Serializer Generator in a .Net Core console application.
1. Create a library project that contains your custom data types. E.g. create a library named MyLibrary using the CLI: `dotnet new library --name MyLibrary`
1. Create a .Net Core console application, e.g. create a app named 'MyApp' using the command: `dotnet new console --name MyApp`.
2. Add dotnet-core MyGet feed to the project.
* Go to the folder of the project you created in step 1, e.g. `cd MyLibrary`
* Add a nuget.config file in the root of the project, using the command `dotnet new nuget`
* Edit the new nuget.config. Remove `<clear />` and add the following line:
`<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />`
- The final file should look like [nuget.config](nuget.config):
2. Add dotnet-core MyGet feed to the project.
* Go to the project folder and create a file named `nuget.config` with the following content. The final file should look like [nuget.config](nuget.config)
```xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
</packageSources>
</configuration>
```
```xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
</packageSources>
</configuration>
```
3. Add a reference to the Microsoft.XmlSerializer.Generator package:
* `dotnet add package Microsoft.XmlSerializer.Generator -v 1.0.0-preview1-25906-03`
* Add the following lines in MyLibrary.csproj.
* Add the following lines in MyApp.csproj.
```xml
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="1.0.0-preview1-25906-03" />
</ItemGroup>
```
```xml
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="1.0.0-preview1-25906-03" />
</ItemGroup>
```
4. Run `dotnet restore` and `dotnet build` to build the library. If everything succeeds, a file named MyLibrary.XmlSerializers.dll will be generated in the output folder. You will see warnings in the build output if the serializer failed to generate.
4. Build the application by running `dotnet restore` and `dotnet build`. If everything succeeds, a file named MyApp.XmlSerializers.dll will be generated in the output folder. You will see warnings in the build output if the tool failed to generate the serialization code.
5. Create a console app and add a project reference to the library. Building the app will generate serialization code for the library and the assembly called MyLibrary.XmlSerializers.dll will be copied to the output folder of the app. You need make sure your console app will use the latest 2.0 build by specifying the RuntimeFrameWorkVersion as mentioned in Prequisition.
Now you can use `XmlSerializer` in the application. And the application will be able to load and use the pre-generated serializers at runtime.
6. Add code to create XmlSerializer with the type defined in the library such as the following. MyLibrary.XmlSerializers.dll will be loaded into the memory after executing the following line.
`XmlSerializer serializer = new XmlSerializer(typeof(MyClass));`