Use ChannelFactory to Consume WCF Web Services in .NET Core

Use ChannelFactory to Consume WCF Web Services in NET Core

Today in this article, we shall see Consuming WCF Web Services in .NET Core using ChannelFactory.

In our previous article, we learned a few techniques of consuming WCF services in .NET Core applications using proxy generations and for the same, we looked at a few useful tools like using,

Today we will see yet another simple approach of using a Channelfactory for consuming WCF( Windows Communication Foundation) services in the .NET Core applications.

Let’s first see the pros and cons of this approach before digging into more details.

What is Channel Factory

Channel Factory is a technique for consuming WCF( Windows Communication Foundation) services in the application using proxy/client code. There are multiple benefits of using Channel Factory

Advantages of ChannelFactory

  • No need to create or maintain a proxy/client code
  • No need to add a service reference or connected service reference
  • This is the best option provided the contract definition is known.

Disadvantage

  • Require prior knowledge of service contract definition

Please note that Channel factory options can be leveraged if the service contract definition is known already. Perfect for an internal organization where service contract definition can be shared easily.

Getting Started

I already have a sample WCF service with contract details as below. This service we will be consuming within the .NET Core WebAPI application.

However, you can use the below technique for other types of applications as well like C# .NET Core Console, Form application (.NET Core 3.0), or ASP.NET Core MVC app, etc.

WCF Service Contract

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

Let’s run this WCF service locally. We shall try connecting this service using the Channel factory from the .NET Core app.

Create ASP.NET Core WebAPI

Let’s start creating a WebAPI application (you can create any other type of project as required.

Please add the below code in any of the Controller methods.

Consume Service using Channel Factory

This is an example only, we will add code to the GET method,

        
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            BasicHttpBinding myBinding = new BasicHttpBinding();
            EndpointAddress myEndpoint = new 
            EndpointAddress("http://localhost:60141/Order.svc");

            ChannelFactory<IOrderService> myChannelFactory = 
            new ChannelFactory<IOrderService>(myBinding, myEndpoint);

            // Create a channel
            IOrderService wcfClient1 = myChannelFactory.CreateChannel();
            string result = wcfClient1.GetOrderData(id);
            return result;

        }


As shown above, ChannelFactory lets you define endpoint – ABC i.e Address, Binding, and Contract for service and helps you connect to the target WCF service from the .NET Core framework.

The generic interface ChannelFactory<TChannel> class helps to create a communication channel with a WCF service. Please also check cache-control techniques for channel factories to get better performance.

The next step is to add the contract/Interface definition to your .NET core project.

The Contract/Interface definition looks as below,

blank

That’s all. Simply execute your GET API, you shall see the result as below.

blank

This was very much basic we covered in this article. I hope this helps you get started.

You can also use Channel Factory with Credentials if needed.

Are you dealing with any complex scenarios? Please do let me know or sound off your comments below.

Other references:

Summary:

WCF Web Service integration with modern technologies like .NET Core services is possible. In this post, we learned the simple approach of using ChannelFactory to connect any .NET Core application with WCF Web services. We understood with this approach we need not have to a proxy (client-side) code or maintain service references etc. as ChannelFactory wraps up those things provided Service contract definition is provided.



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 “Consuming WCF Web Services in .NET Core using ChannelFactory

  1. The customer has provided only their WCF Client proxies (referencye.cs class) generated using their WSDL file that our legacy .NET Framework code is consuming. They do not now have the source code for the WCF Service, for some reason. We need to migrate our class libraries to .NET 8 (from .NET 4.8) that uses these proxies to consume the WCF. With only this Reference.cs is there a way to cosume the WCF service in .NET 8, as .NET 8 does not support a few aspects such as System.Web.Services ? Please advice

    1. Hi Ram- You should still be able to use refrences.cs class file and instantiate the class instance using the config associated with it specified.

      You should check for appropriate nuget package for the missing functionality to be needed. FYI – .NET 3.1 onwards most of support was available through SDK 2.0 and nuget package.

  2. This is indeed a simple solution to interface with WCF services. Certainly, when using a limited set of service methods in the Core application.
    One remark though, when working with complex return types (as it is in most of the cases) you also need to define data contracts in your interface description. (Just as is done in the normal reference.cs file generated by the proxy generator). Important, and this is only for complex types, also mention the namespace in de data contract attribute, this is [DataContract(Namespace=””)]. This namespace should correspond with the data contract namespace used on the WCF server. Otherwise, the WCF method will return an empty data type.

Leave a Reply

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