Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Hosting WCF services in a Windows Forms Application

, 8 Nov 2006 CPOL
Demonstrates how to host serveral singleton services in a Windows Forms application.
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace DCCLocomotiveFactory
{
    [ServiceContract]
    public interface IDCCLocomotiveContract
    {
        /// <summary>
        /// Stops the locomotive
        /// </summary>
        [OperationContract]
        void Stop();

        /// <summary>
        /// Stop the locomotive for an emergency
        /// </summary>
        [OperationContract]
        void EmergencyStop();

        /// <summary>
        /// Gets the running direction of that locomotive
        /// </summary>
        /// <returns></returns>
        [OperationContract(IsOneWay=false)]
        Direction GetDirection();

        /// <summary>
        /// Changes the direction of that locomotive
        /// </summary>
        /// <param name="direction">New direction</param>
        [OperationContract]
        void ChangeDirection(Direction direction);

        /// <summary>
        /// Sets the new speed value
        /// </summary>
        /// <param name="speed">Speed value (0 - 28)</param>
        [OperationContract]
        void SetSpeed(byte speed);

        /// <summary>
        /// Gets the current locomotive speed
        /// </summary>
        /// <returns>Speed value</returns>
        [OperationContract(IsOneWay=false)]
        byte GetSpeed();

        /// <summary>
        /// Switch the main light
        /// </summary>
        /// <param name="state">ON if true, OFF if false</param>
        [OperationContract]
        void SwitchLight(bool state);

        /// <summary>
        /// Gets the main light status
        /// </summary>
        /// <returns>true if ON, false otherwise</returns>
        [OperationContract(IsOneWay=false)]
        bool GetLightState();

        /// <summary>
        /// Sets the state of a function
        /// </summary>
        /// <param name="idx">Function number (1 - 8)</param>
        /// <param name="state">ON if true, OFF if false</param>
        [OperationContract]
        void SetFunction(uint idx, bool state);

        /// <summary>
        /// Gets the state of a function
        /// </summary>
        /// <param name="idx">Function number (1 - 8)</param>
        /// <returns>true if ON, false otherwise</returns>
        [OperationContract(IsOneWay=false)]
        bool GetFunction(uint idx);
    }


    /// <summary>
    /// Implements the IDCCLocomotiveContract
    /// </summary>
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    class DCCLocomotiveService : IDCCLocomotiveContract, IDisposable
    {
        const int
            MinFunction = 1,
            FunctionNumber = 8;
        const byte
            StopSpeed = 0,
            EmergencyStopSpeed = 1,
            MaxSpeed = 28,
            MinSpeed = 0;

        protected byte m_speed = 0;
        protected bool m_light = false;
        protected Direction m_direction = Direction.Forward;
        protected bool[] m_functions = new bool[FunctionNumber] { false, false, false, false, false, false, false, false };
 
        #region IDCCLocomotiveContract Members

        public void Stop()
        {
            m_speed = StopSpeed;
        }

        public void EmergencyStop()
        {
            m_speed = EmergencyStopSpeed;
        }

        public Direction GetDirection()
        {
            return m_direction;
        }

        public void ChangeDirection(Direction direction)
        {
            m_direction = direction;
        }

        public void SetSpeed(byte speed)
        {
            if (speed >= MinSpeed && speed <= MaxSpeed)
                m_speed = speed;
        }

        public byte GetSpeed()
        {
            return m_speed;
        }

        public void SwitchLight(bool state)
        {
            m_light = state;
        }

        public bool GetLightState()
        {
            return m_light;
        }

        public void SetFunction(uint idx, bool state)
        {
            if (idx >= MinFunction && idx <= FunctionNumber)
                m_functions[idx - 1] = state;
        }

        public bool GetFunction(uint idx)
        {
            if (!(idx >= MinFunction && idx <= FunctionNumber))
                throw new IndexOutOfRangeException();

            return m_functions[idx - 1];
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            m_speed = 0;
            m_light = false;
        }

        #endregion
    }

    /// <summary>
    /// Direction enumeration
    /// </summary>
    public enum Direction : byte
    {
        Reverse = 0,
        Forward
    }


    class Constants
    {
        public const string
            LocoServerBaseAddress = "net.tcp://localhost:80{0}/LocServer/";

        public class ColumnName
        {
            public const string
                Name = "Name",
                Address = "Address";
        }
    }
}

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)

Share

About the Author

orouit
Architect Consistel - Singapore
Singapore Singapore
Software Architect, COM, .NET and Smartcard based security specialist.
 
I've been working in the software industry since I graduated in Electrical and Electronics Engineering. I chose software because I preferred digital to analog.
 
I started to program with 6802 machine code and evolved to the current .NET technologies... that was a long way.
 
For more than 20 years I have always worked in technical positions as I simply like to get my hands dirty and crack my brain when things don't go right!
 
After 12 years in the smart card industry I can claim a strong knowledge in security solutions based on those really small computers! I'm currently back in the business to design the licensing system for the enterprise solution I'm currenly working on, using a .NET smart card (yes they can run .NET CLR!)
 
View my profile on LinkedIn
 
You can contact me for professional consulting by using the forum.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 8 Nov 2006
Article Copyright 2006 by orouit
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid