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,

blank

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.



22 thoughts on “Consume WCF Web Services in C# .NET Core – Best Practices

  1. I have implemented the same way as you discussed by using Service Reference. I have instantiated the proxy client in the controller as well. I am getting an error “unable to resolve service for type ‘ServiceReference1.DocumentServiceConverterClient’while attempting to activate API.controllers.conversionController”. It would be really great if you can please provide any resolution for this!!

    1. Hello Riya- If you are not using dependency injection then try to instantiate the DocumentServiceConverterClient correctly,You can use Constructor of controller to initialize instance or use it the way explain the the POC sample in the article.
      If using dependency injection, then try using IOC correctly like example if possible “services.AddScoped()”;

      Hope this helps !

  2. Hi
    Very use full article. Am facing HTTP Error 500.30 – ANCM In-Process Start Failure error post integrating the connected service.

    Without service when i deploy the things work but when deployed with connected service i get the HTTP Error 500.30 – ANCM In-Process Start Failure error.

    Pls can you on the same

    1. Hey Vinay- Thanks for your query. May I know target service Hosting model is Inprocess or out process? Please see switching it has any effect. Please look into the Windows log for more details on the error.

  3. Creating instance of service for every call may cause issue in big application. Do you have any solution for that.

    1. Hey Shekhar- Agree. You could try creating a singleton instance of the proxy class and use it for all other API where it is needed.
      Let me know if that helps!. Thanks.

  4. Hi- Great article. I need to connect to few legacy .asmx services. May i know if above approaches will work for it ? I have not tried yet though.

  5. Thanks for this article.

    I want to consume a WCF service with custom bindings, but all of the above methods fail.This is the error message I get with the 3rd method:

    {System.InvalidOperationException: Contract requires Session, but Binding ‘CustomBinding’ doesn’t support it or isn’t configured properly to support it.

    Any suggestion to get this working is appreciated.

    1. Hi Isha,

      Thanks for your comments. Based on the details you shared, you can try a few options.
      But before you could try anything you should verify WSDL or contract definition for understanding the contract details. Please try using the same configuration as required for vendor Service.
      1. Try to use WsHttpBinding instead of BasicHttpBinding
      2. WsHttpBinding supports session details.
      3. As Andrade mentioned above, you can try to consider using .NET Standard project to use WsHttpBinding along with .Net core client.
      4. WsHttpBinding is not supported in .NET Core but as suggested above .NET Standard project can help.

      Let me know for any queries or questions.

  6. Thanks for this article. It’s rather clarifying, thanks for the insightful explanation. But I still have an issue!

    Here, at my company, we have a couple of awfully ancient ASMX (framework 2) web services that aren’t somehow compatible with svcutil tool. We didn’t find a way to create a client for those using your mentioned tools (we don’t know precisely why).

    We managed to find a workaround using an “extra” .NET Standard Project, but it doesn’t seem right. Any tips about it?

    1. Hi Andrade, Thanks for your feedback.Appreciate that.
      My quick comments,
      1. You mentioned about the error could you please share those if possible.
      2. Please use dotnet-svccutil.exe (and not the svcutil.exe which works with old .NET)
      3. If you own those ASMX services (including contract and definition) then use of ChannelFactory instead of Proxy generation is preferred.

      Please do share more details on the error. Thank you.

  7. I have VS2017 15.4.5 and had issue with creating client code using Connected service. Using svcutil global tool was able to achieve it !! Thanks for very good article.

  8. Thanks for clarifying on Connected services plugin. I am facing similar issues on one of my WCF service and using VS2017(15.4).I shall try 2nd option though anyway. thank you again.

Comments are closed.