Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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 HelloSvcutil
Create a new C# web project in that directory using the dotnet new
command as follows:
dotnet new web
Install the dotnet-svcutil
NuGet package as a CLI tool:
dotnet tool install --global dotnet-svcutil
Run 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.
Restore the WCF packages using the dotnet restore
command as follows:
dotnet restore
Find the name of the client class and operation you want to use. Reference.cs
will contain a class that inherits from System.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.SayHelloClient
is the name of the client class, and has a method called HelloAsync
that can be used to call the operation.
Open the Startup.cs
file in your editor, and add a using
directive for the service reference namespace at the top:
using ServiceReference;
Edit the Configure
method to invoke the web service. You do this by creating an instance of the class that inherits from ClientBase
and 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 run
command as follows:
dotnet run
Navigate 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
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.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Build your first microservice with .NET - Training
Microservice applications are composed of small, independently versioned, and scalable customer-focused services that communicate with each other by using standard protocols and well-defined interfaces. Each microservice typically encapsulates simple business logic, which you can scale out or in. You test, deploy, and manage the microservice independently. Smaller teams develop a microservice based on a customer scenario, and choose the technologies that they use. This module teaches you how to build your f
Documentation
Add WCF Web Service Reference - .NET
An overview of the Microsoft WCF Web Service Reference Provider Tool that adds functionality for .NET Core and ASP.NET Core projects, similar to Add Service Reference for .NET Framework projects.
ServiceModel Metadata Utility Tool (Svcutil.exe) - WCF
Learn about the ServiceModel Metadata Utility, which generates WFC service model code from metadata documents and metadata documents from service model code.
Using dotnet-svcutil.xmlserializer - .NET
Learn how you can use the `dotnet-svcutil.xmlserializer` NuGet package to pre-generate a serialization assembly for .NET Core projects.