Consume WCF Web Services in C# .NET Core – Best Practices

Consume WCF Web Services in C NET Core 60

Today in this article on best practices, we shall learn how to Consume WCF Web Services in C# .NET Core-based applications (like ASP.NET Core API or ASP.NET Core MVC web applications or any other type of project).

Today we shall cover the below techniques for consuming WCF services in .NET or ASP.NET core applications.

Please note that you can use any of the below approaches to address the requirements.

Today in this article, we will cover below aspects,

WCF (Windows Communication Foundation) was a rich programming platform for building service-oriented applications (for the last few decades).

WCF, or Windows Communication Foundation, is a Microsoft technology in the .NET framework used to build service-oriented applications.

It provides a unified platform for creating distributed systems, enabling communication between applications and services running on different platforms.

WCF supports various communication protocols and offers features like security, reliability, and interoperability.

WCF follows a message-based communication model and allows developers to define service contracts and data contracts to facilitate interactions between clients and services.

Today seamless integration of applications in an Enterprise is standard and with the recent popularity of frameworks like RESTful services (WebAPI), It is possible that an organization may need existing WCF or Web legacy services integrated with modern frameworks and technologies.

Although newer technologies like ASP.NET Core and gRPC have gained popularity, WCF remains relevant for Windows-based applications and legacy systems.

The legacy business in value proposition is still ahead of new modern technology stack initiatives and it would take maybe another decade to overcome any legacy or old existing frameworks.

And we don’t know if even today’s green technology could be obsolete in few years down the line.

Connecting WCF service from .NET Core

To use WCF services in .NET Core, you need to create a proxy client of the required service.

Proxy is actually a contract equivalent of actual service and contains complete details of the Interface and method exposed by WCF service.

One can also use the Channel Factory technique to connect to the WCF service easily.

Ways to consume WCF web services in .NET Core application

We shall see below multiple ways to consume WCF services in C# based .NET Core application.

Getting Started

To get started, you certainly need a WCF service.

I already have a sample WCF service with contract details as below.

This service we will be consuming within the .NET Core WebAPI application.

You can use the below-discussed techniques for other types of applications as well like .NET Core Console or Form application (.NET Core 3.1) or ASP.NET Core MVC, etc.

Understand WCF Service Contract

consume wcf service in net 6

This service has a method GetOrderData() which returns order data for a given order ID input.

Create ASP.NET Core API

Let’s start creating an API application (you can create any other type of project as required.)

Let’s use ASP.NET Core 3.1 or any recent .NET 6 + versions.

use webservice in NET 2024

The above templates create skeleton code with a ready-to-use controller with CRUD HTTP methods.

1. Using Connected Services

This is a very simple technique for creating a proxy within the target application.

This option is available in .NET Core or .NET Standards.

Connected services let you create client-side code easily using direct Service URI (If available) or using a WSDL file (If shared by the vendor or third party).

I have discussed both approaches in detail which are based on ‘Connected Services’ using WSDL and URI.

  • Configure WCF web service reference using URI
  • Configure WCF web service reference using the WSDL file

Note: Connected service option is found to be more stable in Visual Studio 2019 and above versions. For Version 2017 and lower you might face a few issues. For the lower version, this option may or may not work. If you come across any issues please try using the 2nd preferred approach- Using ServiceModel MetadataCLI tool which can be used for both VSCode and VS2017 IDE-based applications.

Right-click on “Connected services” as below. This option will be available for all .NET Core or .NET Standards libraries as given below

Consume WCF Web Services in NET Core 2024

Or

Consume WCF Web Services in NET Core

Please select the option “Microsoft WCF Web Service…” as highlighted below,

Configure WCF web service reference using WSDL 2024

Configure WCF web service reference using URI

Let’s look at how to create client code for .NET Core using the service URL.

As shown below please enter the URI below and click on ‘Next’.

Consume WCF web service using WSDL

I got the WCF service URL from the sample service as below.

This URL will be generally in the format of .svc. You shall be entering your actual provider/client URL.

Consume WCF Web Services in NET Core

Once you click ‘finish’ you shall see below windows on the progress of service metadata generation and scaffolding etc.

Consume WCF Web Services in NET Core

Post successful completion of the above steps, the service reference will be added as below in .NET Core WebAPI

Consume WCF web service using connected service

This Reference.cs will have a client class as highlighted below which needs to be instantiated and used to connect with the WCF Service,

Consume WCF Web Services in NET Core

Let’s now go to the WebAPI method and use the class OrderServiceClient to call the actual WCF service and get the results.

As a good practice put this proxy generated class in a proper layer of service. You may consider ‘IntegrationLayer’ or ‘InfraLayer’ repository 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.

Using Proxy Client Instance in Application

Below we are using a Proxy client instance to call the WCF service.

WCF Net core

The above logic is just a demonstration. You could create a static shared instance and can use it for all other methods or Depedency injection using Singletone scope instance.

Here is the successful result from a WCF service within .NET Core WebAPI,

You are all set !!

If you only have access to the WSDL definition, then please use the below techniques to create client code.

Configure WCF web service reference using WSDL

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

A typical look of .wsdl will be below,

Consume WCF Services in C NET

Please use the same connected service configuration and browse through the WSDL file as shown below,

Consuming WCF Web Services in NET Corenet core soap client example net core soap middleware aspnetcore WcF c call soap web service wsdl c consume soap web service example c consume wcf service programmatically c soap client wsdl c soap request wsdl c web service client ChannelFactory NET Core ChannelFactory dotnet core consume asmx service in aspnet core consume wcf service c without adding service reference consume wcf service in net core web api consume wcf service in windows application c Consuming WCF Web Services in NET Core Best Practices Crate net core WcF create proxy class for wcf service in c create proxy class from wsdl c command prompt dotnet svcutil example generate proxy class from wsdl file in c how to add soap service as service reference in net core how to call a soap webservice in net c using the wsdl how to consume a wcf soap service in aspnet core how to consume wcf soap service in c how to implement wsdl in c how to use service reference in c invoke wcf in net core microsoft wcf web service reference provider net core 20 add service reference net core 30 add service reference net core 31 add service reference net core 31 soap net core 31 wcf client net core 50 soap net core connect to soap service net core connected service endpoint configuration net core consume soap service net core web service netcore wcf no proxy code net core service reference SOAP services in NET Core SOAP Web Services in NET Core Soapcore soapcore example use wcf net core Visual Studio Code WCF VS Code WCF service WCF net Core wcf client WCF dotnet core WCF in NET 5 WCF in NET Core wcf service application wcf services in c WCF Web Services in NET Core WCF Web Services in NET Core Consuming WCF Web Services in NET Core Best Practices WSDL in NET Core
<br>Using the WSDL Schema for Proxy Generation

Aboe click on Next and choose the option as appropriate.

Finally, you shall see proxy scaffolding generation which is already explained above.

Please follow the same above steps to connect a WCF service from the .NET Core app.


2. Using ServiceModel Metadata (svcutil.exe) Global Tool

The above option of ‘Connected Services‘ was very easy and simple to use.

However, this UI extension will make you fuzzy due to dependency on the right version (Visual Studio 2017, and the below versions are not stable. Works fine in VS2019 and the above version).

SVC-UTIL utility available as NuGet package generates proxy(client-code) using URI and WSDL both approaches.

Please see the below article for more details on this approach,

If you using another IDE like VSCode or looking for global working cross-platform on Linux, macOS, and Windows then you can use this option for creating client-side code.


3. Using Channel Factory in .NET Core

Don’t want to create/maintain a proxy? Yes ..then this could be one of the preferred approaches if you don’t want to maintain a proxy code.

Please see the below article for more details on this approach,

4. Enable existing WCF services as REST API

If you need to enable WCF services for additional endpoints like a REST API endpoint you can do it easily so that it can be consumed across any type of client.

The good news is you can do it without breaking any existing clients.

This technique will give you a breather time if budget or capacity etc. issues are hindering you.

Summary

WCF Web Service integration with modern technologies like .NET Core services is possible.

In this post, we learned three possible approaches,

  • ‘Connected Services’ using Visual Studio
  • ‘ServiceModel Metadata (svcutil.exe)’ a CLI utility and
  • Channel Factory approach.

First, two techniques use proxy scaffolding of service contracts and the last technique makes use of ChannelFactory thereby allowing connecting .NET Core application with WCF Web services.

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

Please sound off your comments below.

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.