首页 新闻 会员 周边

创建window服务中产生的问题

0
悬赏园豆:10 [已解决问题] 解决于 2014-06-19 09:00

我写了一个类库程序来当window服务的服务核心、一个控制台程序来启动服务,但是启动之后就出现上面的错误,不明白是什么,请各位帮忙看一下,万分感谢啊!!!!!!!


这是window服务核心代码:
namespace Wrox
{
    public class QuoteServer
    {
        private TcpListener listener;
        private int port;
        private string filename;
        private List<string> quotes;
        private Random random;
        private Task listenerTask;

        public QuoteServer()
            : this("F:\\我的桌面\\C#\\ConsoleApplication26\\ConsoleApplication26\\bin\\Debug\\quote.txt")
        { 
        }

        public QuoteServer(string filename) : this(filename,7890) 
        {
        }

        public QuoteServer(string filename, int port)
        {
            Contract.Requires<ArgumentNullException>(filename != null);
            Contract.Requires<ArgumentException>(port >= IPEndPoint.MinPort && port <= IPEndPoint.MaxPort);

            this.filename = filename;
            this.port = port;
        }
        
         protected void ReadQuotes()
        {
            try
            {
                quotes = File.ReadLines(filename).ToList();
                if (quotes.Count == 0)
                {
                    //throw new QuoteException("quotes file is empty");
                }
                random = new Random();
            }
            catch (IOException ex)
            {
                //throw new QuoteException("I/O Error", ex);
            }
        }

         protected string GetRandomQuoteofTheDay()
         {
             int index = random.Next(0, quotes.Count);
             return quotes[index];
         }

         public void Start()
         {
             ReadQuotes();
             listenerTask = Task.Factory.StartNew(Listener, TaskCreationOptions.LongRunning);
         }

         protected void Listener()
         {
             try
             {
                 IPAddress ipAddress = IPAddress.Any;
                 listener = new TcpListener(ipAddress, port);
                 listener.Start();
                 while (true)
                 {
                     Socket clientSocket = listener.AcceptSocket();
                     string message = GetRandomQuoteofTheDay();
                     var encoder = new UnicodeEncoding();
                     byte[] buffer = encoder.GetBytes(message);
                     clientSocket.Send(buffer, buffer.Length, 0);
                     clientSocket.Close();
                 }
             }
             catch (SocketException ex)
             {
                 Trace.TraceError(String.Format("QuoteServer {0}", ex.Message));
                 //throw new QuoteException("socket error", ex);
             }
         }

        public void stop()
        {
            listener.Stop();
        }

        public void Suspend()
        {
            listener.Stop();
        }

        public void Resume()
        {
            Start();
        }

        public void RefreshQuotes()
        {
            ReadQuotes();
        }
    }
}

这是控制台代码:
namespace ConsoleApplication26
{
    class Program
    {
        static void Main(string[] args)
        {
            var qs = new Wrox.QuoteServer("F:\\我的桌面\\C#\\ConsoleApplication26\\ConsoleApplication26\\bin\\Debug\\quote.txt", 4567);
            qs.Start();
            Console.Write("成功");
            Console.ReadLine();
            qs.stop();
        }
    }
}

李贵发的主页 李贵发 | 初学一级 | 园豆:123
提问于:2014-06-18 12:15
< >
分享
最佳答案
0

这个问题提示已经说得很清楚了

收获园豆:10
404 NotFound! | 初学一级 |园豆:184 | 2014-06-18 14:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册