Jimmy
Jimmy

Reputation: 951

Console App running as a Windows Service throws an error

So I had a console application that waits for a client to press enter and then copies a file from a hard coded location to another location. As a console application this works great however when I converted it to a windows service I get the following error code when the client presses enter:

"Could not connect to http://localhost:8080/myService. TCP error code 10061: 
 No connection could be made because the target machine actively refused it 127.0.0.1:8080."

I have checked to make sure that the ports are open and that the firewall is not blocking it. Everything appears to be in the clear so I am confused as to what is going on. Below I will post my client and host code please take a look at it and let me know if there is something I missed so that I can move forward on this.

This is the Host

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.ServiceModel;
using myServiceLib;

namespace myServiceHost
{
    class Program : ServiceBase
    {
        static void Main(string[] args)
        {
            ServiceBase.Run(new Program());
        }

        public Program()
        {
             this.ServiceName = "myService";
        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            using (ServiceHost serviceHost = new ServiceHost(typeof(myService)))
            {
                serviceHost.Open();
            }
        }
    }
}

This is the Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace myServiceClient
{
    class Program
    {
        static void Main(string[] args)

        Console.WriteLine("***** Prepairing to transfer files ***** \n");

        using (FileXferClient myMethod = new FileXferClient())
        {
            Console.WriteLine("Press any key to begin transfer...");
            Console.ReadLine();
            string fileName = "test.txt";
            string sourcePath = @"C:\TestFromC";
            string targetPath = @"C:\TestFromC\Test Folder";

            myMethod.FileXfer(fileName, sourcePath, targetPath);

            Console.WriteLine("File transfer is complete!");
        }
            Console.ReadLine();
        }
    }
}

myServiceLib (this at library with the logic)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace myServiceLib
{
    public class myService : IFileXfer
    {
        public myService()
        {
            Console.WriteLine("Awaiting Files...");
        }
        public void FileXfer(string fileName, string sourcePath, string targetPath)
        {
            string passFileName = fileName;
            string passSourcePath = sourcePath;
            string passTargetPath = targetPath;

            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            System.IO.File.Copy(sourcePath, destFile, true);

            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                foreach (string s in files)
                {
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace myServiceLib 
{
    [ServiceContract(Namespace = "http://Intertech.com")]
    public interface IFileXfer
    {
        //Transfer files from one the target machine to the destination location
        [OperationContract]
        void FileXfer(string fileName, string sourcePath, string targetPath);
    }
}

Upvotes: 0

Views: 1141

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239664

You're closing the ServiceHost immediately after starting it:

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);

        using (ServiceHost serviceHost = new ServiceHost(typeof(myService)))
        {
            serviceHost.Open();
        }
    }

Because it's inside a using statement, it will be disposed (equivalent to calling Close) before your OnStart method even returns.

Instead, make serviceHost a field in the Program class, then have:

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);

        serviceHost = new ServiceHost(typeof(myService));
        serviceHost.Open();
    }

And

    protected override void OnStop()
    {
        serviceHost.Close();

        base.OnStop();
    }

Upvotes: 2

Related Questions