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,

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

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.