WCF dotnet-svcutil tool for .NET Core

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

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:

  1. Create a directory named HelloSvcutil for your project and make it your current directory, as in the following example:

    mkdir HelloSvcutil
    cd HelloSvcutil
    
  2. Create a new C# web project in that directory using the dotnet new command as follows:

    dotnet new web
    
  3. Install the dotnet-svcutil NuGet package as a CLI tool:

    dotnet tool install --global dotnet-svcutil
    
  4. 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.

Using the Service Reference

  1. Restore the WCF packages using the dotnet restore command as follows:

    dotnet restore
    
  2. 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.

  3. Open the Startup.cs file in your editor, and add a using directive for the service reference namespace at the top:

    using ServiceReference;
    
  4. 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);
        });
    }
    
    
  5. Run the application using the dotnet run command as follows:

    dotnet run
    
  6. 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

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.

Information