Generate WCF client in .NET Core using svcutil CLI tool

If you are developing WCF services in other IDE, like VSCode, then you should be looking into another option, the dotnet-svcutil.

Category WCF | Tags: CLI

Published: 08 May 2021

Sometimes, the UI extension will make you fuzzy due to dependency on the right version of Visual Studio 2017. If I am aware it is unstable for most VS2017 lower versions.

If you are developing WCF (Windows Communication Foundation) services in other IDE like VSCode then UI extension option will also not be useful. Today we will be looking into another option i.e dotnet-svcutil.

This is a .NET Core CLI tool and is available cross-platform on Linux, macOS, and Windows.

Advantage of dotnet-svcutil :

  • Its CLI utility and easy to use as a NuGet package.
  • It doesn’t depend on Visual Studio version strictly. It will work even if you have a lower version of Visual Studio like the VS2017 (15.5 or less) version.
  • If using VSCode as IDE for services development.
  • It is available cross-platform on Linux, macOS, and Windows.
  • Compatible with .NET Core and .NET Standard projects.

Install ‘dotnet-svcutil’ from Nuget package manager, or the best option would be to install it globally through the command line so that it can be used across the system.

Please run below command from your command prompt from any location,

dotnet tool install --global dotnet-svcutil --version 2.0.0


Configure WCF web service reference using URI

Let’s create a proxy (client-side) code using this utility and passing URI as input.

dotnet-svcutil http://localhost:60141/Order.svc

This command will creates Reference.cs file.

This Reference.cs will have a client class as highlighted below which needs to be instantiated and used to connect with the WCF Service.
As a good practice put this proxy generated class in a proper layer of service. You may consider adding it to ‘IntegrationLayer’ or ‘InfraLayer’ repository or other custom modules as needed etc.

The file can also be modified for endpoint configuration to support multiple URLs or proxy and the same can be provided through configration or Environment variables etc.

Let’s invoke the WCF method again using OrderServiceClient within WebAPI

using System;

[HttpGet("{id}")]
public ActionResult<stringGet(int id)
{
    OrderServiceClient client = new OrderServiceClient();

    var result = client.GetOrderDataAsync(id);
    return DayOfWeek(result);
}

 

Configure WCF web service reference using WSDL

If you have access to .WSDL file (Web Services Description Language) then the same above technique can also be used to create client code for .NET Core.

dotnet-svcutil [wsdl file path]


So one can generate the client code using WSDL file as well. Use the generated file as explained in details above already.