作为Windows服务运行的控制台应用程序会引发错误

时间:2012-05-04 13:13:52

标签: c# .net wcf windows-services console-application

所以我有一个控制台应用程序,等待客户端按Enter键,然后将文件从硬编码位置复制到另一个位置。作为一个控制台应用程序,这很好用但是当我将它转换为Windows服务时,当客户端按下回车键时,我得到以下错误代码:

"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."

我已检查过以确保端口已打开且防火墙未阻止它。一切似乎都很清楚,所以我对发生的事情感到困惑。下面我将发布我的客户端和主机代码,请看看它,让我知道是否有我错过的东西,以便我可以继续前进。

这是主持人

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

这是客户

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(这在带有逻辑的库中)

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

1 个答案:

答案 0 :(得分:2)

您在启动后立即关闭ServiceHost

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

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

因为它在using语句中,所以在Close方法返回之前它将被处理掉(相当于调用OnStart)。

相反,在serviceHost类中设置Program字段,然后输入:

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

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

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

        base.OnStop();
    }