silverlight记录网站访问总人数和在线人数的方式问题

最近想用silverlight做过网站,在做记录网站访问总人数和在线人数这个小功能时候,不想采用读取数据库,然后再写入数据库的方式,因为觉的只是简单的记录,没必要那么麻烦,于是采取直接读取写入txt记事本的方法,没想到其中碾碾转转,采取几种方式,最终才实现完成,下面我想谈谈自己的完成的整个过程,包括失败的:

     方式一:采取的是在客户端直接读取写入方式。

                     代码如下:StreamResourceInfo text = Application.GetResourceStream(new Uri("txt/河流总集合.txt", UriKind.RelativeOrAbsolute));

                                                         using (StreamReader read = new StreamReader(text.Stream))

                                                                  {

                                                                                  string line = read.ReadLine();

                                                                                  read.Close();

                                                                  }

 

                                                         using (StreamWriter writer= new StreamWriter(text.Stream))

                                                                  {

                                                                                 writer.WriterLine((Convert.ToInt32(line) + 1).ToString());

                                                                                  read.Close();

                                                                  }

                    这种方式在客户端读取是没问题的,但是在写入writer时候就会抛出异常,说是权限问题,不知道是否有其他的方法在客户端直接写入的?

   方式二:采用独立存储文件方式IsolatedStorageFile

                   代码如下:            读txt
                                                  IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
                                                 Stream stream = new IsolatedStorageFileStream("MyText.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, isf);
                                                 System.IO.TextReader reader = new System.IO.StreamReader(stream);
                                                 string sLine = reader.ReadLine();
                                                 reader.Close(); // Close the reader
                                                 stream.Close(); // Close the stream
                                                //写txt
                                               IsolatedStorageFile isf1 = IsolatedStorageFile.GetUserStoreForApplication();
                                              Stream stream1 = new IsolatedStorageFileStream("MyText.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write, isf1);
                                              System.IO.TextWriter writer = new System.IO.StreamWriter(stream1);
                                             writer.WriteLine((Convert.ToInt32(sLine) + 1).ToString());
                                             writer.Close(); // Close the reader
                                             stream1.Close(); // Close the stream

                  这种方式在单机运行,没发布时候是没有问题的,具体问题见:http://bbs.csdn.net/topics/390304940,到现在我也不明白为什么会这样,以IP形式运行就会出现异常,以localhost形式就没问题,不知道是否也是权限访问问题没?

    方式三:在论坛里,luoSaiMingJavaAndC网友给我指点了,比较麻烦点的方式-WCF,调用发布服务的形式,在此十分感谢luoSaiMingJavaAndC给我的思路,思路如下:现在后台发布好服务,然后在前台调用。

                    后台服务模块:       [WebMethod]
        public string Readtxt(string path)
        {
            string line = "";
            try
            {
                using (StreamReader myreader = new StreamReader("F:/" + path + ".txt"))
                {
                    line = myreader.ReadLine();
                    myreader.Close();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return line;
        }
        /// <summary>
        /// 登录时候添加在线人数、总人数 +1
        /// </summary>
        /// <param name="path"></param>
        /// <param name="Readline"></param>
        [WebMethod]
        public void WritetxtAdd(string path, string Readline)
        {
            //string line = "";
            try
            {
                using (StreamWriter mywrite = new StreamWriter("F:/" + path + ".txt"))
                {
                    mywrite.WriteLine((Convert.ToInt32(Readline) + 1).ToString());
                    mywrite.Close(); // Close the reader
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            //return line;
        }
        /// <summary>
        /// 注销时候、退出时候在线人数、总人数-1
        /// </summary>
        /// <param name="path"></param>
        /// <param name="Readline"></param>
        [WebMethod]
        public void WritetxtSub(string path, string Readline)
        {
            //string line = "";
            try
            {
                using (StreamWriter mywrite = new StreamWriter("F:/" + path + ".txt"))
                {
                    if (Convert.ToInt32(Readline) - 1 >= 0)
                    {
                        mywrite.WriteLine((Convert.ToInt32(Readline) - 1).ToString());
                    }
                    else
                    {
                        mywrite.WriteLine("0");
                    }
                    mywrite.Close(); // Close the reader
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            //return line;
        }

 

客户端调用服务代码:          

            MytxtSoapClient = new Mytxt.MytxtSoapClient();
            MytxtSoapClient.ReadtxtCompleted += new EventHandler<Mytxt.ReadtxtCompletedEventArgs>(MytxtSoapClient_ReadtxtCompleted);
            MytxtSoapClient.ReadtxtAsync("访问总人数");
            MytxtSoapClient1 = new Mytxt.MytxtSoapClient();
            MytxtSoapClient1.ReadtxtCompleted += new EventHandler<Mytxt.ReadtxtCompletedEventArgs>(MytxtSoapClient_ReadtxtCompleted1);
            MytxtSoapClient1.ReadtxtAsync("当前在线人数");

       /// <summary>
        /// 先读访问总人数txt,再访问总人数+1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MytxtSoapClient_ReadtxtCompleted(object sender, Mytxt.ReadtxtCompletedEventArgs e)
        {
            string readtxt = e.Result.ToString();
            MytxtSoapClient2 = new Mytxt.MytxtSoapClient();
            MytxtSoapClient2.WritetxtAddAsync("访问总人数", readtxt);
           // login = true;
        }
        /// <summary>
        /// 先读当前在线人数txt,再当前在线人数+1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //bool login = false;
        private void MytxtSoapClient_ReadtxtCompleted1(object sender, Mytxt.ReadtxtCompletedEventArgs e)
        {
            string readtxt = e.Result.ToString();
            MytxtSoapClient3 = new Mytxt.MytxtSoapClient();
            MytxtSoapClient3.WritetxtAddAsync("当前在线人数", readtxt);
            //InitialPage myp = this.Parent as InitialPage;
            //myp.gotopage(new MainPage());
            //login = true;
        }
        /// <summary>
        /// 先读当前在线人数txt,再当前在线人数-1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MytxtSoapClient_ReadtxtCompleted2(object sender, Mytxt.ReadtxtCompletedEventArgs e)
        {
            string readtxt = e.Result.ToString();
            MytxtSoapClient4 = new Mytxt.MytxtSoapClient();
            MytxtSoapClient4.WritetxtSubAsync("当前在线人数", readtxt);
        }

这种方式也有个问题必须要控制好,那就是同步异步问题,我在实现的过程中就出现一点问题,在登陆页面登陆时候实现了总人数、在线人数均+1,但是跳到主页面时候,却没有同步更新,主页面读取的还是原来的数据,必须要刷新一次才显示新的txt数据。

登陆页面的登陆按钮事件:

        private void Login_click(object sender, RoutedEventArgs e)
        {

            MytxtSoapClient = new Mytxt.MytxtSoapClient();
            MytxtSoapClient.ReadtxtCompleted += new EventHandler<Mytxt.ReadtxtCompletedEventArgs>(MytxtSoapClient_ReadtxtCompleted);
            MytxtSoapClient.ReadtxtAsync("访问总人数");
            MytxtSoapClient1 = new Mytxt.MytxtSoapClient();
            MytxtSoapClient1.ReadtxtCompleted += new EventHandler<Mytxt.ReadtxtCompletedEventArgs>(MytxtSoapClient_ReadtxtCompleted1);
            MytxtSoapClient1.ReadtxtAsync("当前在线人数");
            /
                InitialPage myp = this.Parent as InitialPage;
                myp.gotopage(new MainPage());

        }

      这里我以为是按代码的顺便来的,顺着我的思路,先读取txt再写入+1,之后才跳转到主页面MainPage的,结果断点跟踪时候发现不是。

主页面读取:

        public MainPage()

{

            MytxtSoapClient = new Mytxt.MytxtSoapClient();
            MytxtSoapClient.ReadtxtCompleted += new EventHandler<Mytxt.ReadtxtCompletedEventArgs>(MytxtSoapClient_ReadtxtCompletedOnlyread);
            MytxtSoapClient.ReadtxtAsync("访问总人数");
            MytxtSoapClient1 = new Mytxt.MytxtSoapClient();
            MytxtSoapClient1.ReadtxtCompleted += new EventHandler<Mytxt.ReadtxtCompletedEventArgs>(MytxtSoapClient_ReadtxtCompleted1onlyread);
            MytxtSoapClient1.ReadtxtAsync("当前在线人数");

}

到这里可能是同步问题所导致的,luoSaiMingJavaAndC又给我指点了linq  to  rx 思路,说是 对异步控制很好,让我尝试一下!对于linq这种方式我不是很熟悉,我是菜鸟来的,以前读取数据库都是那种方式:先连接数据库再读取数据、操作数据,最后再关闭的,很古老的方式,对于这种linq绑定数据库控件方式了解不多。听取luoSaiMingJavaAndC意见,我去学习了解linq方式,最后终于解决了这个问题,十分感谢luoSaiMingJavaAndC对我的帮助,衷心的感谢!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值