About Hasmukh patel

My Photo
Harrow, London, United Kingdom
Dot-Net developer with expertise in Web, WPF, Win-form applications. Have worked on Asp.net,mvc , WPF and Win-forms projects in c#.net language having Sql-Server/Oracle as database with service oriented architecture using test driven development. Having complete knowledge of SDLC and have successfully worked and implemented it on projects.

Host WCF Services with generic utility

Generic WCF Service Host
Generic service host allows you to host any WCF service which writes logs on each start so you don’t need to write manual log for individual service.
Logging utility based on EnterpriseLibrary , code is as below
using System;
using System.ServiceModel;
using DemoLib.Utilities.LoggingUtilities;
using DemoLib.Utilities.Properties;
 
namespace DemoLib.Utilities.ServiceUtilities
{
    public class GenericServiceHost<T> : ServiceHost
        where T : class
    {
        public GenericServiceHost()
            : base(typeof (T))
        {
        }
 
        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        {
            Log.LogInformation(string.Format(Resources.StartingService, typeof (T).FullName));
            return base.OnBeginOpen(timeout, callback, state);
        }
 
        protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        {
            Log.LogInformation(string.Format(Resources.StopingService, typeof (T).FullName));
            return base.OnBeginClose(timeout, callback, state);
        }
 
        protected override void OnAbort()
        {
            Log.LogWarning(string.Format(Resources.AbortingSerivce, typeof (T).FullName));
            base.OnAbort();
        }
 
        protected override void OnFaulted()
        {
            Log.LogError(string.Format(Resources.ServiceFault, typeof (T).FullName));
            base.OnFaulted();
        }
    }
}
To install Enterprise Library 5.0 - Logging Application Block, run the following command in the Package Manager Console
PM> Install-Package EnterpriseLibrary.Logging
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
 
namespace DemoLib.Utilities.LoggingUtilities
{
    public  static class Log
    {
        public static void LogError(string message)
        {
            LogMessage(message, "Error"TraceEventType.Error);
        }
 
        public static void LogWarning(string message)
        {
            LogMessage(message, "Warning"TraceEventType.Warning);
        }
 
        public static void LogInformation(string message)
        {
            LogMessage(message, "Information"TraceEventType.Information);
        }
 
        public static void LogDebug(string message)
        {
            LogMessage(message, "Debug"TraceEventType.Verbose);
        }
 
        private static void LogMessage(string message, string category, TraceEventType eventType)
        {
            var logEntry = new LogEntry
            {
                Message = message,
                Severity = eventType,
                MachineName = Environment.MachineName,
                Categories = new List<string>() { category }
            };
 
            Logger.Write(logEntry);
        }
    }
}
Examples of generic service host utility
        #region Private methods
 
        private ServiceHost _authServiceHost;
        private ServiceHost _userServiceHost;
        ServiceHost _emailServiceHost;
 
        #region note for port sharing service
            //type services.msc and set "NetTcpPortSharing" start type to manual
            // start service 
            //net start NetTcpPortSharing
            // add SID in to SMSvcHost.exe.config (C:\Windows\Microsoft.NET\Framework\v4.0.30319) or task mgr (services)-> NetTcpPortSharing -> go to process -> go to file location 
            // get SID wmic useraccount get name,sid
            #endregion
 
        private void StartServices()
        {
            _authServiceHost = new GenericServiceHost<AuthoService>();
            _authServiceHost.Open();
 
            _userServiceHost = new GenericServiceHost<UserService>();
            _userServiceHost.Open();
 
            _emailServiceHost = new GenericServiceHost<EmailService>();
            _emailServiceHost.Open();
        }
 
        private void StopServices()
        {
            if (_authServiceHost != null)
                _authServiceHost.Close();
 
            if (_userServiceHost != null) 
                _userServiceHost.Close();
 
            if (_emailServiceHost != null) 
                _emailServiceHost.Close();
        }
 
        #endregion

1 comment :

for ict 99 said...

It is a very good utility for implementing generics in WCF. Great JOB.

WCF Training | Dot Net Training in Chennai