Click here to Skip to main content
15,892,537 members
Articles / Operating Systems / Windows

Debugging WCF Apps

Rate me:
Please Sign up or sign in to vote.
4.59/5 (22 votes)
20 Jan 2007CPOL13 min read 93.3K   587   54  
Exploring diagnostic utilities for WCF
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Diagnostics;
using System.Configuration;

namespace CheckPointe
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class WCFService : IWCFService
    {
        /// <summary>
        /// Returns the result of the requested operation
        /// </summary>
        /// <param name="op"></param>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <returns></returns>
        public OperationResult DoOperation(OperationType op, int value1, int value2)
        {
            OperationResult or = new OperationResult();
            try
            {
                switch (op)
                {
                    case OperationType.Add:
                        or.Value = value1 + value2;
                        break;
                    case OperationType.Sub:
                        or.Value = value1 - value2;
                        break;
                    case OperationType.Mul:
                        or.Value = value1 * value2;
                        break;
                    case OperationType.Div: //exception will catch div by zero
                        or.Value = value1 / value2;
                        break;
                }
                or.Result = ResultCode.Success;
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message);
                or.Result = ResultCode.Fail;
            }
            return or;
        }
        static void Main(string[] args)
        {
            NetTcpBinding tcpBinding = new NetTcpBinding();
            NetNamedPipeBinding pipeBinding = new NetNamedPipeBinding();

            Uri tcpBaseAddress;
            Uri pipeBaseAddress;
            ServiceHost host = null;

            try
            {
                //We'll support two transports, one local and one remote
                tcpBaseAddress = new Uri(ConfigurationManager.AppSettings["baseTcpWCFService"]);
                pipeBaseAddress = new Uri(ConfigurationManager.AppSettings["basePipeWCFService"]);
                host = new ServiceHost(typeof(CheckPointe.WCFService), tcpBaseAddress, pipeBaseAddress);

                host.AddServiceEndpoint(typeof(CheckPointe.IWCFService), tcpBinding, tcpBaseAddress);
                host.AddServiceEndpoint(typeof(CheckPointe.IWCFService), pipeBinding, pipeBaseAddress);

                host.Open();

                // The service can now be accessed.
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            if (host != null)
                host.Close();   //Release host

        }
    }
}

namespace WCFService
{
    class Program
    {
    }
}

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)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions