Consuming WCF Web Services in .NET Core - Best Practices (2024)

Consuming WCF Web Services in .NET Core - Best Practices (1)

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,

  • Connecting WCF service from .NET Core
  • Ways to consume WCF web services in .NET Core application
  • Getting Started
  • Understand WCF Service Contract
  • Create ASP.NET Core API
  • Using Connected Services
  • Configure WCF web service reference using URI
  • Using Proxy Client Instance in Application
  • Configure WCF web service reference using WSDL
  • Using ServiceModel Metadata (svcutil.exe) Global Tool
  • Using Channel Factory in .NET Core
  • Enable existing WCF services as REST API
  • Summary

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.

  • Connected Service in Visual Studio( VS2017 and above)
  • Using the ServiceModel Metadata Global CLI tool
  • Using Channel Factory

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

Consuming WCF Web Services in .NET Core - Best Practices (2)

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.

Consuming WCF Web Services in .NET Core - Best Practices (3)

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

Consuming WCF Web Services in .NET Core - Best Practices (4)

Or

Consuming WCF Web Services in .NET Core - Best Practices (5)

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

Consuming WCF Web Services in .NET Core - Best Practices (6)

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’.

Consuming WCF Web Services in .NET Core - Best Practices (7)

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.

Consuming WCF Web Services in .NET Core - Best Practices (8)

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

Consuming WCF Web Services in .NET Core - Best Practices (9)

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

Consuming WCF Web Services in .NET Core - Best Practices (10)

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

Consuming WCF Web Services in .NET Core - Best Practices (11)

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 throughConfiguration in .NET Coreor Environment variables etc.

Using Proxy Client Instance in Application

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

Consuming WCF Web Services in .NET Core - Best Practices (12)

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,

Consuming WCF Web Services in .NET Core - Best Practices (13)

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,

Consuming WCF Web Services in .NET Core - Best Practices (14)

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

Consuming WCF Web Services in .NET Core - Best Practices (15)

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.


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,

  • Consuming WCF Web Services in .NET Core using Global Tool

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,

  • Consuming WCF services without creating a proxy or adding web references

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.

  • Enable WCF services as REST API

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.


Share on FacebookPost on X

Consuming WCF Web Services in .NET Core - Best Practices (18)Follow us

Save
Consuming WCF Web Services in .NET Core - Best Practices (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6222

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.