Consuming WCF Web Services in .NET Core using svcutil CLI tool

dotnet svcutilConsume WCF Web Services Global NET Tool dotnet svcutil

Today in this article, we will see how to Consume WCF Web Services – Global .NET Tool Dotnet-svcutil.

In our last post of Consuming WCF Web service using Connected Services, we looked at ‘Connected services‘ VS IDE UI extension for creating client-side code in .NET Core.

Today in this article, we will cover below aspects,

If you are developing WCF(Windows Communication Foundation) services in other IDE like VSCode then the 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 as below,

svcutil

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 the below command from your command prompt from any location,

Command:

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

Example:

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 pass URI as input.

Command :

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

Example

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

This command will create Reference.cs file.

Using Refrences.cs file in the applicaiton

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 the autogenerated file 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 Configuration in .NET Core or Environment variables etc.

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

Consume WCF Web Services Global NET Tool dotnet svcutil

Here are the results from a WCF service within .NET Core WebAPI,

using WSDL file to generate client code

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.

Command :

dotnet-svcutil [wsdl file path]

Example:

client side code using URI or WSDL

So one can generate the client code using the WSDL file as well. Now please follow the steps mentioned above for this generated file.

Other references:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Happy coding !!.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.