Using dotnet-svcutil.xmlserializer on .NET Core
The dotnet-svcutil.xmlserializer
NuGet package can pre-generate a serialization assembly for .NET Core projects. It pre-generates C# serialization code for the types in the client application that are used by the WCF Service Contract and that can be serialized by the XmlSerializer. This improves the startup performance of XML serialization when serializing or deserializing objects of those types.
Prerequisites
- .NET Core 2.1 SDK or later
- Your favorite code editor
You can use the command dotnet --info
to check which versions of .NET SDK and runtime you already have installed.
Getting started
To use dotnet-svcutil.xmlserializer
in a .NET Core console application:
Create a WCF Service named 'MyWCFService' using the default template 'WCF Service Application' in .NET Framework. Add
[XmlSerializerFormat]
attribute on the service method like the following:[ServiceContract] public interface IService1 { [XmlSerializerFormat] [OperationContract(Action = "http://tempuri.org/IService1/GetData", ReplyAction = "http://tempuri.org/IService1/GetDataResponse")] string GetData(int value); }
Create a .NET Core console application as WCF client application that targets at .NET Core 2.1 or later versions. For example, create an app named 'MyWCFClient' with the following command:
dotnet new console --name MyWCFClient
To ensure your project is targeting .NET Core 2.1 or later, inspect the
TargetFramework
XML element in your project file:<TargetFramework>netcoreapp2.1</TargetFramework>
Add a package reference to
System.ServiceModel.Http
by running the following command:dotnet add package System.ServiceModel.Http
Add the WCF Client code:
using System.ServiceModel; class Program { static void Main(string[] args) { var myBinding = new BasicHttpBinding(); var myEndpoint = new EndpointAddress("http://localhost:2561/Service1.svc"); //Fill your service url here var myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint); IService1 client = myChannelFactory.CreateChannel(); string s = client.GetData(1); ((ICommunicationObject)client).Close(); } } [ServiceContract] public interface IService1 { [XmlSerializerFormat] [OperationContract(Action = "http://tempuri.org/IService1/GetData", ReplyAction = "http://tempuri.org/IService1/GetDataResponse")] string GetData(int value); }
Add a reference to the
dotnet-svcutil.xmlserializer
package by running the following command:dotnet add package dotnet-svcutil.xmlserializer
Running the command should add an entry to your project file similar to this:
<ItemGroup> <DotNetCliToolReference Include="dotnet-svcutil.xmlserializer" Version="1.0.0" /> </ItemGroup>
Build the application by running
dotnet build
. If everything succeeds, an assembly named MyWCFClient.XmlSerializers.dll is generated in the output folder. If the tool failed to generate the assembly, you'll see warnings in the build output.Start the WCF service by, for example, running
http://localhost:2561/Service1.svc
in the browser. Then start the client application, and it will automatically load and use the pre-generated serializers at run time.