Click here to Skip to main content
13,041,923 members (77,748 online)
Click here to Skip to main content


67 bookmarked
Posted 8 Nov 2006

Hosting WCF services in a Windows Forms Application

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

namespace ServiceHostUtility
    class ThreadedServiceHost<TService, TContract>
        const int SleepTime = 100;
        private ServiceHost m_serviceHost = null;
        private Thread m_thread;
        private string 
        private bool m_running;
        private Binding m_binding;

        public ThreadedServiceHost(string serviceAddress, string endpointAddress, Binding binding)
            m_binding = binding;
            m_serviceAddress = serviceAddress;
            m_endpointAddress = endpointAddress;

            m_thread = new Thread(new ThreadStart(ThreadMethod));

        void ThreadMethod()
                m_running = true;

                // Start the host
                m_serviceHost = new ServiceHost(typeof(TService), new Uri(m_serviceAddress));
                m_serviceHost.AddServiceEndpoint(typeof(TContract), m_binding, m_endpointAddress);

                while (m_running)
                    // Wait until thread is stopped

            catch (Exception)
                if (m_serviceHost != null)

        /// <summary>
        /// Request the end of the thread method.
        /// </summary>
        public void Stop()
            lock (this)
                m_running = false;

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.


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


About the Author

Architect Connect In Private
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've been back into business to design the licensing system for the enterprise solution for Consistel using a .NET smart card (yes they can run .NET CLR!)

I'm currently designing a micro-payment solution using the NXP DESFire EV1 with the ACSO6 SAM of ACS. I can then add a full proficient expertise on those systems and NFC payments.
This technology being under strict NDA by NXP I cannot publish any related article about it, however I can provide professional consulting for it.

You can contact me for professional matter by using the forum or via my LinkedIn profile.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 8 Nov 2006
Article Copyright 2006 by orouit
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid