Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The Windows Communication Foundation (WCF) dotnet-svcutil tool is a .NET tool that retrieves metadata from a web service on a network location or from a WSDL file, and generates a WCF class containing client proxy methods that access the web service operations.
Similar to the Service Model Metadata - svcutil tool for .NET Framework projects, the dotnet-svcutil is a command-line tool for generating a web service reference compatible with .NET Core and .NET Standard projects.
The dotnet-svcutil tool is an alternative option to the WCF Web Service Reference Visual Studio connected service provider that first shipped with Visual Studio 2017 version 15.5. The dotnet-svcutil tool as a .NET tool, is available cross-platform on Linux, macOS, and Windows.
Important
You should only reference services from a trusted source. Adding references from an untrusted source may compromise security.
Prerequisites
- .NET Core 2.1 SDK or later versions
- Your favorite code editor
Getting started
The following example walks you through the steps required to add a web service reference to a .NET Core web project and invoke the service. You'll create a .NET Core web application named HelloSvcutil and add a reference to a web service that implements the following contract:
[ServiceContract]
public interface ISayHello
{
[OperationContract]
string Hello(string name);
}
For this example, let's assume the web service will be hosted at the following address: http://contoso.com/SayHello.svc
From a Windows, macOS, or Linux command window perform the following steps:
Create a directory named HelloSvcutil for your project and make it your current directory, as in the following example:
mkdir HelloSvcutil cd HelloSvcutilCreate a new C# web project in that directory using the
dotnet newcommand as follows:dotnet new webInstall the
dotnet-svcutilNuGet package as a CLI tool:dotnet tool install --global dotnet-svcutilRun the dotnet-svcutil command to generate the web service reference file as follows:
dotnet-svcutil http://contoso.com/SayHello.svc
The generated file is saved as HelloSvcutil/ServiceReference/Reference.cs. The dotnet-svcutil tool also adds to the project the appropriate WCF packages required by the proxy code as package references.
Using the Service Reference
Restore the WCF packages using the
dotnet restorecommand as follows:dotnet restoreFind the name of the client class and operation you want to use.
Reference.cswill contain a class that inherits fromSystem.ServiceModel.ClientBase, with methods that can be used to call operations on the service. In this example, you want to call the SayHello service's Hello operation.ServiceReference.SayHelloClientis the name of the client class, and has a method calledHelloAsyncthat can be used to call the operation.Open the
Startup.csfile in your editor, and add ausingdirective for the service reference namespace at the top:using ServiceReference;Edit the
Configuremethod to invoke the web service. You do this by creating an instance of the class that inherits fromClientBaseand calling the method on the client object:public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { var client = new SayHelloClient(); var response = await client.HelloAsync(); await context.Response.WriteAsync(response); }); }Run the application using the
dotnet runcommand as follows:dotnet runNavigate to the URL listed in the console (for example,
http://localhost:5000) in your web browser.
You should see the following output: "Hello dotnet-svcutil!"
For a detailed description of the dotnet-svcutil tool parameters, invoke the tool passing the help parameter as follows:
dotnet-svcutil --help
Feedback & questions
If you have any questions or feedback, open an issue on GitHub. You can also review any existing questions or issues at the WCF repo on GitHub.
Release notes
- Refer to the Release notes for updated release information, including known issues.