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.

WCF Utilities


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.

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();
        }
    }
}

Logging utility based on EnterpriseLibrary , code is as below
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 :

Boymak said...

HırdavatFırsat.com Sizin satın alırkenki avantajınız olmaya kararlıdır!

Hırdavat ürünleri baştada dediğimiz gibi gereksiz ,ufak tefek vs diye adlandırıldığı için bir çok kişinin aklına kalitesiz yada kullan at(tek kullanımlık) malzeme gibiymiş gelir. Aslında alıcağınız kaliteli bir ürün ile uzun süreler aynı performansı alıcağınız için bu konuda dikkat etmenizi tavsiye ediyoruz.Peki kaliteli ürün pahalıdır diye düşünüyorsanız,mutlaka HırdavatFırsat.com Adresimizdeki ürünlerimize göz atın. En kaliteli ürünleri En uygun Fiyatlarla satmayı misyon edindik.Hırdavatfirsat.com üzerinde 1000 den fazla hırdavat ürünü olmasına rağmen bu bile Hırdavat dünyasının bir kısmını göstermektedir.
Hırdavat üzerine arayıpta sitemizde bulamadığınız ürün olursa mutlaka bize ulaşın veya fiyat teklifi isteyin. En kısa sürede cevap alacaksınız.