none
Convert WCF Client Console App to Windows Service RRS feed

  • Question

  • Hi

    I am trying to convert my WCF client app into a Windows Service. Can anyone provide an example of how to close the connection between the client and server in the OnStop class of the Windows service.

    This is the code I used to close the connection in the console app:

    client.Unsubscribe();
    client.Close();

    Here is the code I used to connect to the WCF server:

    InstanceContext instanceContext = new InstanceContext(new CallbackHandler());

    ServiceReference1.AppDuplexClient client = new ServiceReference1.AppDuplexClient(instanceContext, "Username");

    client.ClientCredentials.UserName.UserName = username;
    client.ClientCredentials.UserName.Password = password;

    client.Subscribe();


    Thanks

    Tuesday, February 19, 2013 3:14 AM

Answers

  • Hi,

    Check bellow code snippet for Windows Service 

     #region SERVICE CONSTRUCTOR
            /// <summary>
            /// SERVICE CONSTRUCTOR
            /// </summary>
            public MailService()
            {
                InitializeComponent();
    
                this.ServiceName = "MailService";
                this.EventLog.Source = "MailService";
                this.EventLog.Log = "AppCustomeLog";
    
                // These Flags set whether or not to handle that specific
                //  type of event. Set to true if you need it, false otherwise.
                this.CanHandlePowerEvent = true;
                this.CanHandleSessionChangeEvent = true;
                this.CanPauseAndContinue = true;
                this.CanShutdown = true;
                this.CanStop = true;
    
                if (!EventLog.SourceExists("MailService"))
                {
                    EventLog.CreateEventSource("MailService", "AppCustomeLog");
                }
    
            }
            #endregion
    
            #region Main
            /// <summary>
            /// The Main Thread: This is where your Service is Run.
            /// </summary>
            static void Main()
            {
                ServiceBase.Run(new MailService());
            }
            #endregion 
    
    #region OnStart
            /// <summary>
            /// OnStart: Put startup code here
            ///  - Start threads, get inital data, etc.
            /// </summary>
            /// <param name="args"></param>
            protected override void OnStart(string[] args)
            {
                InitializeTimer();
                EventLog.WriteEntry("MailService", "Service:" + this.ServiceName + "Started");
                base.OnStart(args);
            }
            #endregion
    
            #region InitializeTimer
            /// <summary>
            /// InitializeTimer
            /// </summary>
            private void InitializeTimer()
            {
                EventLog.WriteEntry("MailService", "Timer is null");
    
                if (timer == null)
                {
                    timer = new Timer();
                    timer.AutoReset = true;
                    timer.Interval = Convert.ToDouble(ConfigurationManager.AppSettings["TimeInterval_Miliseconds"]);
                    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
                    timer.Start();
    
                    EventLog.WriteEntry("MailService", "Timer Started");
    
                }
            }
            #endregion
    
            #region timer_Elapsed
            private void timer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
            {
                if (string.Compare(ConfigurationManager.AppSettings["EmailNotifierSwitch"], "On", true, CultureInfo.InvariantCulture) == 0)
                {
                    this.EmailNotifier();
                }
    
                //Log the event
                EventLog.WriteEntry("MailService", this.ServiceName + " Service ran successfully");
            }
            #endregion
    
            #region OnStop
            /// <summary>
            /// OnStop: Put your stop code here
            /// - Stop threads, set final data, etc.
            /// </summary>
            protected override void OnStop()
            {
                timer.Enabled = false;
                EventLog.WriteEntry("MailService", string.Concat(this.ServiceName, " Service stopped"));
                base.OnStop();
            }
            #endregion
    
            #region OnPause
            /// <summary>
            /// OnPause: Put your pause code here
            /// - Pause working threads, etc.
            /// </summary>
            protected override void OnPause()
            {
                EventLog.WriteEntry("MailService", string.Concat(this.ServiceName , " Service is Poused"));
                base.OnPause();
            }
            #endregion
    
            #region OnContinue
            /// <summary>
            /// OnContinue: Put your continue code here
            /// - Un-pause working threads, etc.
            /// </summary>
            protected override void OnContinue()
            {
                base.OnContinue();
            }
            #endregion
    
            #region OnShutdown
            /// <summary>
            /// OnShutdown(): Called when the System is shutting down
            /// - Put code here when you need special handling
            ///   of code that deals with a system shutdown, such
            ///   as saving special data before shutdown.
            /// </summary>
            protected override void OnShutdown()
            {
                EventLog.WriteEntry("MailService", this.ServiceName + " Service is Shutingdown");
                base.OnShutdown();
            }
            #endregion

    Happy Coding,

    Thanks


    Knight Data Access Layer - .NET ORM, LINQ Provider, and more https://www.kellermansoftware.com/p-47-net-data-access-layer.aspx

    • Proposed as answer by radcorpindia Tuesday, February 19, 2013 1:34 PM
    • Marked as answer by robo.bas Thursday, February 21, 2013 10:40 PM
    Tuesday, February 19, 2013 1:31 PM