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,

blank


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.



4 thoughts on “Consume WCF Web Services – Global .NET Tool dotnet-svcutil

  1. Hey Admin,

    great article. I am still a bit lost.

    >I have created a .NET 5 MVC Project.
    >I have been provided with the WSDL and XSD files for an application I am running. The web service is not exposed for security purposes. I have to use svcutil.
    >I ran svcutil *.wsdl *.xsd /language:C# /out:Proxy.cs which created Proxy.cs and output.config.

    How do I then add the files to my .MVC project and make API calls?
    Should I just the the .cs file to controllers ?

    1. Hi Jamal – Thanks. Glad you liked the article. Please add proxy.cs file to your project module. This proxy class will have a Client class which you need to instantiate it in the module and then call the method supported by the class.

  2. Really useful, but I had to double dash the “global” to install dotnet-svcutil:
    dotnet tool install –global dotnet-svcutil

Leave a Reply

Your email address will not be published. Required fields are marked *