November 2, 2020

.Net Core - Call WCF Service with Https link

In last post , we have seen how to add WCF reference in .Net Core Application. In this example we will see how we can call different WCF operations or methods both with Http link and Https link.

If the target WCF link is Http link then you can create the WCF client instance using following code snippet:

BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
EndpointAddress endpointAddress = 
              new EndpointAddress("https://localhost:8081/OrderManagement/OrderManagement.svc");
OrderManagementClient wcfClient = 
              new OrderManagementClient(basicHttpBinding, endpointAddress);
var result = wcfClient.GetOrderByID(1);

However if the WCF link is bound over Https, then you have to make small changes in order to consume service with Https. You have to by-pass SSL Certificate Authentication. You can use following code snippet to consume WCF Service with Https link:

BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
EndpointAddress endpointAddress = 
                new EndpointAddress("https://localhost:8082/OrderManagement/OrderManagement.svc");
OrderManagementClient wcfClient = 
                new OrderManagementClient(basicHttpBinding, endpointAddress);

//wcfClient.ClientCredentials.ServiceCertificate.SslCertificateAuthentication
//is need to be set for Https certificate authentication
wcfClient.ClientCredentials.ServiceCertificate.SslCertificateAuthentication =
	new X509ServiceCertificateAuthentication()
	{
		CertificateValidationMode = X509CertificateValidationMode.None,
		RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck
	};

var result = wcfClient.GetOrderByID(1);

Related Post:

November 1, 2020

.Net Core - Add WCF Reference

In this post, I will explain how you can add reference for a WCF Service in .Net Core Application, this will be a little bit different then how we consume WCF Service in regular .Net Framework.

In this example I have a WCF service already created, and I want to consume in .Net Core Console Application.

In Solution Explorer, right click on Connected Services node, and select Add Connected Service.

In Connected Services window, click on Microsoft WCF Web Service Reference Provider.

In the Service Endpoint tab, Enter the URL of WCF Service in the URL textbox and click Go button.

Type the desired namespace in Namespace textbox and click Next.

In the next tab Data Type Options, it will allow to change default options to generate client proxy. For example you can change Collection Type, the type from System.Array to System.Collections.Generic.List. Once you done with the changes click Next.

In Client Options tab, It will ask you to change the access level for generated classes. Default access level is public.

Click Finish button, and it will generate the required proxy classes to call WCF.

When the process completed, you are done with adding WCF reference.

Once you are done with adding reference, you can create an instance of the generated WCF client type and invoke the service operations as follows:

	BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
	EndpointAddress endpointAddress = 
          		new EndpointAddress("http://localhost:8081/OrderManagement/OrderManagement.svc");
	OrderManagementClient wcfClient =
    			new OrderManagementClient(basicHttpBinding, endpointAddress);
	var result = wcfClient.GetOrderByID(1);

References:

Related Post(s):