Using ChannelFactory with Credentials

Today in this article we shall see how to use Channel Factory to call service with Authentication enabled mainly using Network credentials using Basic Authentication i.e by providing UserName and Password credentials techniques etc.

Getting Started

We already covered basics in our last article. Please visit below article for more details.

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 an order data for a given order ID input.

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

Create ASP.NET Core API

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

Please add below code in the location of your choice usually repository or Infra layer of your module.

Adding ClientCredentials to the Channelfactory

Adding ClientCredentials instance and assign it to the Channelfactory asb below,

            //Add credentials 
            ClientCredentials loginCredentials = new ClientCredentials();
            loginCredentials.UserName.UserName = "thecodebuzz";
            loginCredentials.UserName.Password = "******";
            myChannelFactory.Endpoint.Behaviors.Add(loginCredentials);


This is an example only, complete code within ASP.NET Core GET method,

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

            //Create Channel Factory Instance
            ChannelFactory<IOrderService> myChannelFactory =
            new ChannelFactory<IOrderService>(myBinding, myEndpoint);

            //Add credentials 
            ClientCredentials loginCredentials = new ClientCredentials();
            loginCredentials.UserName.UserName = "thecodebuzz";
            loginCredentials.UserName.Password = "******";
            myChannelFactory.Endpoint.Behaviors.Add(loginCredentials);

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

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

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

Other references:

Summary

Today in this article we shall learn how to use Channel Factory to call service with Authentication enabled mainly using Network credentials (ex.using Basic authentication) techniques etc.



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.



Leave a Reply

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