Thursday, September 24, 2020

Calling WCF services from .Net Core

Originally Microsoft was not planning to support WCF Services in .Net Core because "WCF is not platform-independent". But Microsoft has pushed WCF really hard for the last ten years so a large portion of their user base was not thrilled with the idea of replacing all their WCF communication layers with something else. I recently discovered that Microsoft now supports WCF from .Net Core.

Here's a walkthrough on consuming WCF services from .Net Core. I'm using Visual Studio 2019 version 16.7.4 Enterprise. You have to use Visual Studio 2017 version 15.5 or later to make this work.

Start a new C#, .Net Core, console project called WCFFromCore. Add a C# WCF Service Application project called WCFServices. Your solution explorer window looks like this.


Rename IService1 to ITimestampService and Service1 to TimestampService. Now we can write the service. Change the contents of ITimestampService.cs to this.

using System;
using System.ServiceModel;
 
namespace WCFServices
{
    [ServiceContract]
    public interface ITimestampService
    {
        [OperationContract]
        DateTime GetTimestamp();
    }
}

Change the contents of TimestampService.svc.cs to this.

using System;
 
namespace WCFServices
{
    public class Service1 : ITimestampService
    {
        public DateTime GetTimestamp()
        {
            return DateTime.Now;
        }
    }
}

Now rename Service1 to TimestampService. Make sure it gets changed in TimeStampService.svc too.

As you can see, this is no different from what you were doing before. This is a very good thing.

Build the WCFServices project (this step is important).

Our next task is to create the proxies in the console application. This looks a little different from what you may be used to. Right-click on Dependencies and select "Add Connected Service". If you don't see that option you need to make sure your Visual Studio version is 15.5 or later. 


This displays the Connected Services page. You need to click on "Add a WCF web service reference to your project".


You will see the Configure WCF Web Service Reference page. Click [Discover] and change the namespace to TimeStampServiceReference.


Now click on Client Options (in the left side menu) and select Generate Synchronous Operations. Click [Finish] to generate the proxies in your console application.


Your solution explorer now contains the WCF proxies you just generated.


The big difference here is that asynchronous operations are now the default and you have to do extra work to generate synchronous operations. Looking at conversations on their websites it seems Microsoft would really appreciate it if we used the asynchronous methods.

Now we have generated the proxies, here's how we consume them. Nothing much has changed here. Change Program.cs to look like this. We call the WCF Service in synchronous, synchronous over asynchronous, and true asynchronous modes.

using System;
using System.ServiceModel;
using System.Threading.Tasks;
using TimestampServiceReference;
 
namespace WCFFromCore
{
    class Program
    {
        static void Main(string[] args)
        {
            TimestampServiceClient client = new TimestampServiceClient();
            DateTime Timestamp;
 
            try
            {
                Timestamp = client.GetTimestamp();
                Console.WriteLine(string.Format("The synchronous time on the server is {0}", Timestamp));
 
                Timestamp = client.GetTimestampAsync().GetAwaiter().GetResult();
                Console.WriteLine(string.Format("The asynchronous time on the server is {0}", Timestamp));
 
                Task<DateTime> t = client.GetTimestampAsync();
                t.ContinueWith(x => Console.WriteLine(string.Format("The true asynchronous time on the server is {0}", x.Result)));
                Console.WriteLine("Made async request");
                t.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("ERROR:{0}", ex.Message));
            }
        }
    }
}

 


It took a lot of whining from the user community but Microsoft eventually came through for us. This will certainly make our transition to .Net Core a lot easier.


No comments:

Post a Comment