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

A Smart Card Framework for .NET

, 20 Feb 2013 CPOL
Describes a framework to use the PCSC Smart Card API with .NET.
smartcardapi_demo.zip
Interop.GemCardExLib.dll
Interop.SCARDSSPLib.dll
DemoSCFmwk.exe
GemCard.dll
GemCardEx.dll
smartcardapi_doc.zip
smartcardapi_doc
banner.jpg
darkcorner.jpg
GemCard
gradleft.jpg
gradtop.jpg
graycorner.jpg
minus.jpg
plus.jpg
titletile.jpg
smartcardapi_source.zip
Smartcard_API
DemoSCFmwk
Properties
GemCardEx
GemCardEx.aps
GemCardEx.def
GemCardEx.rgs
GemCardEx.suo
GemCardEx.vcproj.CORUSCANT.han.user
GemCardEx.vcproj.vspscc
GemCardExps.def
Release
GemCardEx.dll
SCardDatabaseEx.rgs
Smartcard Framework 2005.suo
GemCard
GemCard.csproj.user
SmartcardFrameworkWithWCFSCardService.zip
SmartcardFramework
DemoSCardService
Properties
Service References
SCardNPService
configuration.svcinfo
configuration91.svcinfo
DemoSCardService.SCardNPService.APDUResponse.datasource
Reference.svcmap
service.wsdl
SCardService
configuration.svcinfo
configuration91.svcinfo
DemoSCardService.SCardService.APDUResponse.datasource
Reference.svcmap
service.wsdl
SCardServiceHost
Properties
Settings.settings
Smartcard Framework 2010.suo
Smartcard Framework 2010.v11.suo
Smartcard_API
DemoSCFmwk
Properties
GemCard
GemCard.csproj.user
SmartCardPlayer
Properties
Smartcard_Test
ApduExchange
ApduExchange.csproj.user
App.ico
Thumbs.db
GSMHelper
Properties
ReadPhonebook
Properties
Settings.settings
ReadPhonebook.csproj.user
SmartcardService
Properties
SmartcardService.csproj.user
Smartcard_Framework_2010.zip
Smartcard_Framework_2010
Smartcard Framework 2010.suo
Smartcard_API
DemoSCFmwk
DemoSCFmwk.csproj.user
Properties
GemCard
GemCard.csproj.user
SmartCardPlayer
Properties
Smartcard_Test
ApduExchange
ApduExchange.csproj.user
App.ico
GSMHelper
Properties
ReadPhonebook
Properties
Settings.settings
ReadPhonebook.csproj.user
Smartcard_Framework_64.zip
Smartcard_Framework
Smartcard_API
DemoSCFmwk
Properties
GemCard
GemCard.csproj.user
SmartCardPlayer
Properties
Smartcard_Test
ApduExchange
ApduExchange.csproj.user
App.ico
GSMHelper
Properties
ReadPhonebook
Properties
Settings.settings
ReadPhonebook.csproj.user
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;

namespace GemCard.Service.Host
{
    /// <summary>
    /// Base class for the service host managers
    /// </summary>
    /// <typeparam name="I"></typeparam>
    /// <typeparam name="T"></typeparam>
    abstract class ServiceHostManager<I, T> : IDisposable
        where T : class
        where I : class
    {
        #region Fields

        private ServiceHost serviceHost = null;
        private string baseAddress;
        private string serviceAddress;

        #endregion

        #region Events

        public EventHandler Opened;
        public EventHandler Closed;

        #endregion

        protected ServiceHostManager(string baseAddress, string serviceAddress)
        {
            this.baseAddress = baseAddress;
            this.serviceAddress = serviceAddress;
        }

        /// <summary>
        /// This method must be implemented to create the service binding
        /// </summary>
        /// <returns></returns>
        protected abstract Binding CreateServiceBinding();

        /// <summary>
        /// This method must be implemented to create the MEX binding
        /// </summary>
        /// <returns></returns>
        protected abstract Binding CreateMetadataExchangeBinding();

        /// <summary>
        /// starts the service
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {
            bool started = false;

            if (serviceHost == null)
            {
                serviceHost = new ServiceHost(typeof(T), new Uri(baseAddress));
                serviceHost.AddServiceEndpoint(typeof(I), CreateServiceBinding(), serviceAddress);

                // Attach event handlers
                serviceHost.Opened += serviceHost_Opened;
                serviceHost.Closed += serviceHost_Closed;

                AddMetadataEchange(serviceHost, baseAddress, serviceAddress);
            }

            if (serviceHost != null)
            {
                switch (serviceHost.State)
                {
                    case CommunicationState.Closed:
                        {
                            serviceHost = null;
                            started = Start();
                            break;
                        }

                    case CommunicationState.Opened:
                    case CommunicationState.Opening:
                        {
                            started = true;
                            break;
                        }

                    case CommunicationState.Created:
                        {
                            serviceHost.Open();
                            started = true;
                            break;
                        }
                }
            }

            return started;
        }

        /// <summary>
        /// Stops the service
        /// </summary>
        public void Stop()
        {
            if (serviceHost != null)
            {
                switch (serviceHost.State)
                {
                    case CommunicationState.Opened:
                        {
                            serviceHost.Close();
                            while (serviceHost.State != CommunicationState.Closed)
                            {
                                // Wait!
                            }

                            // Detach event handlers
                            serviceHost.Opened -= serviceHost_Opened;
                            serviceHost.Closed -= serviceHost_Closed;
                            serviceHost = null;
                            break;
                        }
                }
            }
        }

        /// <summary>
        /// Gets the current service state
        /// </summary>
        public CommunicationState State
        {
            get
            {
                CommunicationState state = CommunicationState.Closed;

                if (serviceHost != null)
                {
                    state = serviceHost.State;
                }

                return state;
            }
        }

        public void Dispose()
        {
            Stop();
        }

        #region Private methods

        private void AddMetadataEchange(ServiceHost host, string baseAddress, string serviceAddress)
        {
            ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
            host.Description.Behaviors.Add(mBehave);

            host.AddServiceEndpoint(typeof(IMetadataExchange),
                CreateMetadataExchangeBinding(),
                Path.Combine(baseAddress, serviceAddress, "mex"));
        }

        private void serviceHost_Closed(object sender, EventArgs e)
        {
            if (Closed != null)
            {
                Closed(this, e);
            }
        }

        private void serviceHost_Opened(object sender, EventArgs e)
        {
            if (Opened != null)
            {
                Opened(this, e);
            }
        }

        #endregion
    }
}

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 | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 20 Feb 2013
Article Copyright 2006 by orouit
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid