Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C#

Runtime Generated WCF Service Exposing .NET or COM Types

Rate me:
Please Sign up or sign in to vote.
4.69/5 (38 votes)
24 Apr 2008CPOL8 min read 80K   1.1K   51  
A WCF service wrapper is generated at runtime around a .NET or COM type to expose its interface.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleClient.Class1;
using ConsoleClient.Class1_PerCall;
using ConsoleClient.Class2;
using ConsoleClient.Class2_PerCall;

namespace ConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\nACTION REQUIRED: Press any key to proceed after Services started...");
            Console.ReadKey();

            // Class1 per session
            Console.WriteLine("\n**** Class1 per session ****");
                       
            Class1Client c1 = new Class1Client();
            c1.set_Count(139);
            int n = 61;
            Console.WriteLine("{0} + {1} = {2}", c1.get_Count(), n, c1.Add(n));
            Console.WriteLine("{0} - {1} = {2}", c1.get_Count(), n, c1.Subtract(n));

            Console.WriteLine(c1.Concat("XXX "));
            Console.WriteLine(c1.Concat("YYY "));
            
            // Class2 per session
            Console.WriteLine("\n**** Class2 per session ****");

            Class2Client c2 = new Class2Client();
            string param1 = "AAA ";
            int param2 = 15;
            Console.WriteLine("{0}", c2.Method1(ref param1, ref param2));

            param1 = "BBB ";
            param2 = 5;
            Console.WriteLine("{0}", c2.Method1(ref param1, ref param2));

            param1 = "CCC ";
            param2 = 10;
            Console.WriteLine("{0}", c2.Method1(ref param1, ref param2));

            // Class1 per call
            Console.WriteLine("\n**** Class1 per call ****");
                        
            Class1_PerCallClient c1pc = new Class1_PerCallClient();
            c1pc.set_Count(139);
            n = 61;
            Console.WriteLine("{0} + {1} = {2}", c1pc.get_Count(), n, c1pc.Add(n));
            Console.WriteLine("{0} - {1} = {2}", c1pc.get_Count(), n, c1pc.Subtract(n));

            Console.WriteLine(c1pc.Concat("XXX "));
            Console.WriteLine(c1pc.Concat("YYY "));

            // Class2 per call
            Console.WriteLine("\n**** Class2 per call ****");
                       
            Class2_PerCallClient c2pc = new Class2_PerCallClient();
            param1 = "AAA ";
            param2 = 15;
            Console.WriteLine("{0}", c2pc.Method1(ref param1, ref param2));

            param1 = "BBB ";
            param2 = 5;
            Console.WriteLine("{0}", c2pc.Method1(ref param1, ref param2));

            param1 = "CCC ";
            param2 = 10;
            Console.WriteLine("{0}", c2pc.Method1(ref param1, ref param2));

            // Sleep to test ReceiveTimeout feature.
            int timeoutInSec = 30;
            Console.WriteLine("\n*** Sleeping for {0} sec to test \"receiveTimeout\" and then will proceed... ***\n", timeoutInSec);
            System.Threading.Thread.Sleep(timeoutInSec * 1000);
            
            // Class1 per session
            Console.WriteLine("\n**** Class1 per session ****");

            n = 61;
            try
            {
                Console.WriteLine("{0} + {1} = {2}", c1.get_Count(), n, c1.Add(n));
                Console.WriteLine("{0} - {1} = {2}", c1.get_Count(), n, c1.Subtract(n));

                Console.WriteLine(c1.Concat("XXX "));
                Console.WriteLine(c1.Concat("YYY "));
            }
            catch (Exception e)
            {
                Console.WriteLine("\nERROR: {0}.\nInnerException Message: {1}\n", e.Message, e.InnerException.Message);
            }

            Console.WriteLine("\nPress any key to exit...");
            Console.ReadKey();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel


  • Nov 2010: Code Project Contests - Windows Azure Apps - Winner
  • Feb 2011: Code Project Contests - Windows Azure Apps - Grand Prize Winner



Comments and Discussions