Consume WCF Web Services in C# .NET Core – Best Practices
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.
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
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.
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
Or
Please select the option “Microsoft WCF Web Service…” as highlighted below,
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’.
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.
Once you click ‘finish’ you shall see below windows on the progress of service metadata generation and scaffolding etc.
Post successful completion of the above steps, the service reference will be added as below in .NET Core WebAPI
This Reference.cs will have a client class as highlighted below which needs to be instantiated and used to connect with the WCF Service,
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.
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,
Please use the same connected service configuration and browse through the WSDL file as shown below,
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.
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!!
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 !
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
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.
Creating instance of service for every call may cause issue in big application. Do you have any solution for that.
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.
Awesome post .. thanks..made me go easy for my requirement ..
Thanks Max for your feedback!
Thank you .. brilliant !
Thanks Aiden! Appreciate your feedback.
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.
Hey Jazza, Thanks for your time and encouraging comments. Yes, you can use any of the above approaches as discussed. Please refer Consuming ASMX Web Services in ASP.NET Core for more details.
Thanks for very good insight on consuming services in .NET Core
Thank you Abragam. Appreciate it!
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.
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.
Hi.
I have similar problem like Isha, but i’m wondering if .Net core support WsHttpBinding
https://github.com/dotnet/wcf/blob/main/release-notes/SupportedFeatures-v2.1.0.md
Was something changed in 3.1 version?. I’m using 3.1 version.
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?
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.
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.
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.
Thank you for reading and Thanks for the note.