You are here

Windows Communication Foundation

As a starting point for WCF application take a look at https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-define-a-wc...

We continue working with the example provided by Microsoft. We shall extend the example to show an asynchronous function call and multiple functions call.
We have defined the new interface as shown in the following example.

namespace GettingStartedLib
{
        [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
        [OperationContract]
        int GetDataTotalIndex();
        [OperationContract]
        double GetData(int index);
    }
}

GetDataTotalIndex and GetData have been introduced to demonstrate how we can call seriese of method ayncronuosly. Our service has array of double data type. We retrieve the data by these 2 methods.
The first method gets the total number of indices. Based on the number of indices, we call the asynchronous methods with each index. Since there are 5 items in the array, function call may not return in the calling order.

        private double[] balance = { 0.001, 111.012, 222.22, 33.33, 444.44 };
        public int GetDataTotalIndex()
        {
            return balance.Length;
        }

        public double GetData(int index)
        {
            switch (index)
            {
                case 0:
                    {
                        return balance[index];
                    }
                case 1:
                    {
                        return balance[index];
                    }
                case 2:
                    {
                        return balance[index];
                    }
                case 3:
                    {
                        return balance[index];
                    }
                case 4:
                    {
                        return balance[index];
                    }
                default:
                    return -1.0; // return invalid data
            }
        }

Windows service reference update:

Client-side code:

using GettingStartedClient.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GettingStartedClient
{
    class Program
    {
        public delegate void MyDelegate(string aMessage);
        private static int index = -1;

        static void Main(string[] args)
        {
            CalculatorClient client = new CalculatorClient();
            MyDelegate aDelegate = DelegateUserStateMethod;

            // AddAsync
            double value1 = 100.00D;
            double value2 = 15.99D;
            client.AddCompleted += new EventHandler(AddCallback);
            client.AddAsync(value1, value2, aDelegate);
            Console.WriteLine("nAsync Add({0},{1})", value1, value2);

            // SubtractAsync
            value1 = 145.00D;
            value2 = 76.54D;
            client.SubtractCompleted += new EventHandler(SubtractCallback);
            client.SubtractAsync(value1, value2);
            Console.WriteLine("nAsync Subtract({0},{1})", value1, value2);

            // Call the Add service operation.
            value1 = 100.00D;
            value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

            //Array Manipulation, we continue calling untill the value of index has been updated.
            client.GetDataTotalIndexCompleted += new EventHandler(GetDataTotalIndexCallback);
            while (index (GetDataCallback);
            for (int i = 0; i  to terminate the client.");
            client.Close();
            Console.ReadKey();
        }

        static void GetDataTotalIndexCallback(object sender, GetDataTotalIndexCompletedEventArgs e)
        {
            index = e.Result;
            Console.WriteLine("Total index in services -> " + index);
        }

        static void GetDataCallback(object sender, GetDataCompletedEventArgs e)
        {
            if (e.UserState != null)
            {
                Console.WriteLine("Data Found {0} with index {1}", e.Result, (int)e.UserState);
            }
        }
        static void DelegateUserStateMethod(string message)
        {
            Console.WriteLine("User state message -> " + message);
        }

        // Asynchronous callbacks for displaying results.
        static void AddCallback(object sender, AddCompletedEventArgs e)
        {
            Console.WriteLine("Async Add Result: {0}", e.Result);
            if (e.UserState != null)
            {
                MyDelegate aDelega = (MyDelegate)e.UserState;
                aDelega("User state being used for add call back");
            }
        }

        static void SubtractCallback(object sender, SubtractCompletedEventArgs e)
        {
            Console.WriteLine("Async Subtract Result: {0}", e.Result);
            if (e.UserState != null)
            {
                MyDelegate aDelega = (MyDelegate)e.UserState;
                aDelega("This message should never print with this example, reason no userstate used in substract");
            }
        }
    }
}