none
求助 .net framework 2.0 升级到 .net framework 4.0 RRS feed

  • 问题

  • 大家好,我们团队现在用开发环境是vs2010 .net 2.o framework asp.net, 昨天项目从.net 2.0 升级到。.net 4.0项目以后发现网站运行十分缓慢,页面95%的页面都无打开,即使打开首页也需要好几分钟,经过我们排查只有,发现移除一个附加httpmodel 以后项目就正常运行了,但是我们必须要次httpmodel 功能才正常,否则有部分功能无法使用,下附该部分代码,为什么2.0正常,到4.0以后就出问题了,请大家有类似经验的多多指教。


    asp.net windows phone sivlerlght

    2012年2月25日 1:13

答案

  • ..net Framework 2.0 与 4.0 彼此独立,不是升级取代的关系,不必在安装 4.0 之前卸载 2.0,可以令它们共存。
     
    --
    Alexis Zhang
     
    http://mvp.support.microsoft.com/profile/jie
    http://blogs.itecn.net/blogs/alexis
     
    推荐以 NNTP Bridge 桥接新闻组方式访问论坛以获取最佳用户体验。
     
    本帖是回复帖,原帖作者是楼上的 "and yu"
     
    大家好,我们团队现在用开发环境是vs2010 .net 2.o framework asp.net, 昨天项目从.net 2.0 升级到。.net 4.0项目以后发现网站运行十分缓慢,页面95%的页面都无打开,即使打开首页?残枰眉阜种樱?
     
    2012年3月1日 21:30
    版主

全部回复

  • using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.SessionState;


    namespace TongCheng.Mobile.Wap
    {

        /// <summary>
        ///
        /// </summary>
        public class UrlAppendParams : System.Web.IHttpModule
        {
            private long refId = 0;
            private string authCode = string.Empty;


            public void Init(HttpApplication application)
            {
                application.AcquireRequestState += new EventHandler(Application_AcquireRequestState);
                application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState);
                application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler);


                application.PostRequestHandlerExecute += new EventHandler(Application_ApplyUrlFilter);
            }


            #region Application_AcquireRequestState
            private void Application_AcquireRequestState(object sender, EventArgs e)
            {
                HttpApplication application = (HttpApplication)sender;


                this.refId = this.GetRefIdByRequest();


                if (this.refId > 0)
                {
                    UnionInfo unionInfo = new UnionInfo(this.refId);
                    unionInfo.AddToSession(SessionKey.UnionInfo);
                }


                this.authCode = this.GetAuthCodeByRequest();


                if (!string.IsNullOrEmpty(this.authCode))
                {
                    AuthenticationInfo authInfo = new AuthenticationInfo(this.authCode);


                    authInfo.AddToSession(SessionKey.AuthenticationInfo);
                }
            }
            #endregion


            #region Application_PostAcquireRequestState
            private void Application_PostAcquireRequestState(object source, EventArgs e)
            {
                HttpApplication application = (HttpApplication)source;


                UrlParamsFilterHttpHandler resourceHttpHandler = HttpContext.Current.Handler as UrlParamsFilterHttpHandler;


                if (resourceHttpHandler != null)
                {
                    // set the original handler back
                    HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler;
                }


                //// -> at this point session state should be available
                //Debug.Assert(application.Session != null, "it did not work :(");


                #region REFID
                if (this.refId < 1)
                {
                    this.refId = this.GetRefIdByRequest();
                }
                UnionInfo unionInfo = null;


                if (this.refId > 0)
                {
                    unionInfo = new UnionInfo(this.refId);

                    unionInfo.AddToSession(SessionKey.UnionInfo);
                }
                else
                {
                    unionInfo = UnionInfo.GetCurrentUnionInfo();
                }


                if (HttpContext.Current != null && unionInfo != null)
                {
                    if (!HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
                        HttpContext.Current.Items.Add(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo), unionInfo);
                    else
                        HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] = unionInfo;
                }
                #endregion


                #region AUTHCODE
                if (String.IsNullOrEmpty(this.authCode))
                {
                    this.authCode = this.GetAuthCodeByRequest();
                }
                AuthenticationInfo authInfo = null;


                if (!string.IsNullOrEmpty(this.authCode))
                {
                    authInfo = new AuthenticationInfo(this.authCode);


                    authInfo.AddToSession(SessionKey.AuthenticationInfo);
                }
                else
                {
                    authInfo = AuthenticationInfo.GetCurrentAuthenticationInfo();
                }


                if (HttpContext.Current != null && authInfo != null)
                {
                    if (!HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
                        HttpContext.Current.Items.Add(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo), authInfo);
                    else
                        HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] = authInfo;
                }
                #endregion
            }
            #endregion


            #region Application_PostMapRequestHandler
            private void Application_PostMapRequestHandler(object source, EventArgs e)
            {
                HttpApplication application = (HttpApplication)source;


                if (application.Context.Handler is IReadOnlySessionState || application.Context.Handler is IRequiresSessionState)
                {
                    // no need to replace the current handler
                    return;
                }


                // swap the current handler
                application.Context.Handler = new UrlParamsFilterHttpHandler(application.Context.Handler);
            }
            #endregion


            #region Application_ApplyUrlFilter
            private void Application_ApplyUrlFilter(Object sender, EventArgs e)
            {
                HttpApplication application = sender as HttpApplication;
                application.Response.Filter = new UrlAppendParamsFilter(application.Response.Filter);
            }
            #endregion


            #region
            /// <summary>
            ///
            /// </summary>
            /// <returns></returns>
            private long GetRefIdByRequest()
            {
                long refId = 0;
                if (!string.IsNullOrEmpty(HttpContext.Current.Request["refid"])
                    && Validator.IsInt64(HttpContext.Current.Request["refid"]))
                {
                    refId = Int64.Parse(HttpContext.Current.Request["refid"]);
                }
                else if (!string.IsNullOrEmpty(HttpContext.Current.Request["unionId"])
                    && Validator.IsInt64(HttpContext.Current.Request["unionId"]))
                {
                    refId = Int64.Parse(HttpContext.Current.Request["unionId"]);
                }


                return refId;
            }
            #endregion


            #region
            /// <summary>
            ///
            /// </summary>
            /// <returns></returns>
            private string GetAuthCodeByRequest()
            {
                string auth = string.Empty;
                if (!string.IsNullOrEmpty(HttpContext.Current.Request["auth"]))
                {
                    auth = HttpContext.Current.Request["auth"];
                }


                return auth;
            }
            #endregion


            public void Dispose()
            {


            }


            #region CLASS UrlAppendParamsFilter
            private class UrlAppendParamsFilter : Stream
            {
                private Stream Sink { get; set; }
                private long _position;
                private System.Text.StringBuilder oOutput = new StringBuilder();
                public UrlAppendParamsFilter(Stream sink)
                {
                    Sink = sink;
                }
                public override bool CanRead
                {
                    get { return true; }
                }
                public override bool CanSeek
                {
                    get { return true; }
                }
                public override bool CanWrite
                {
                    get { return true; }
                }
                public override long Length
                {
                    get { return 0; }
                }
                public override long Position
                {
                    get { return _position; }
                    set { _position = value; }
                }
                public override long Seek(long offset, System.IO.SeekOrigin direction)
                {
                    return Sink.Seek(offset, direction);
                }
                public override void SetLength(long length)
                {
                    Sink.SetLength(length);
                }
                public override void Close()
                {
                    Sink.Close();
                }
                public override void Flush()
                {
                    Sink.Flush();
                }
                public override int Read(byte[] buffer, int offset, int count)
                {
                    return Sink.Read(buffer, offset, count);
                }
                public override void Write(byte[] buffer, int offset, int count)
                {
                    string authCode = UrlAppendParamsFilter.GetAuthCodeByRequest();
                    long refId = UrlAppendParamsFilter.GetRefIdByRequest();
                    AuthenticationInfo authInfo = null;
                    UnionInfo unionInfo = null;
                    if (HttpContext.Current != null)
                    {
                        if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
                            authInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] as AuthenticationInfo;
                        if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
                            unionInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] as UnionInfo;
                    }


                    if ((refId <= 0 && unionInfo == null)
                        && (string.IsNullOrEmpty(authCode) && authInfo == null))
                    {
                        Sink.Write(buffer, 0, buffer.Length);
                        return;
                    }


                    string content = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
                    Regex regex = new Regex(RegexResource.HREF, RegexOptions.IgnoreCase);
                    Regex regexs = new Regex(RegexResource.HREFS, RegexOptions.IgnoreCase);
                    Regex action_regex = new Regex(RegexResource.ACTION, RegexOptions.IgnoreCase);
                    if (regex.IsMatch(content))
                    {
                        content = Regex.Replace(content, RegexResource.HREF, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    }
                    if (regexs.IsMatch(content))
                    {
                        content = Regex.Replace(content, RegexResource.HREFS, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    }
                    if (action_regex.IsMatch(content))
                    {
                        content = Regex.Replace(content, RegexResource.ACTION, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    }
                    byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(content);
                    Sink.Write(data, 0, data.Length);
                }


                public static string ReplaceParams(Match match)
                {
                    string result = string.Empty;
                    if (match.Value.StartsWith("http", StringComparison.CurrentCultureIgnoreCase)
                       || match.Value.StartsWith("wtai://", StringComparison.CurrentCultureIgnoreCase)
                       || match.Value.EndsWith(".css", StringComparison.CurrentCultureIgnoreCase)
                       || match.Value.EndsWith(".js", StringComparison.CurrentCultureIgnoreCase)
                       || match.Value.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase)
                       || match.Value.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase)
                       || match.Value.EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase))
                    {
                        result = match.Value;
                    }
                    else
                    {
                        #region
                        long refId = UrlAppendParamsFilter.GetRefIdByRequest();


                        if (refId <= 0)
                        {
                            UnionInfo unionInfo = null;
                            if (HttpContext.Current != null)
                            {
                                if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
                                    unionInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] as UnionInfo;
                            }
                            if (unionInfo != null)
                            {
                                refId = unionInfo.UnionId;
                            }
                        }
                        #endregion


                        #region
                        string authCode = UrlAppendParamsFilter.GetAuthCodeByRequest();
                        if (string.IsNullOrEmpty(authCode))
                        {
                            AuthenticationInfo authInfo = null;


                            if (HttpContext.Current != null)
                            {
                                if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
                                    authInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] as AuthenticationInfo;
                            }
                            if (authInfo != null)
                            {
                                authCode = authInfo.AuthCode;
                            }
                        }
                        #endregion


                        result = match.Value;


                        if (!string.IsNullOrEmpty(authCode) && result.IndexOf("auth=") == -1)
                        {
                            if (result.IndexOf('?') == -1)
                            {
                                result = result.Insert(result.Length, "?auth=" + authCode);
                            }
                            else
                            {
                                result = result.Insert(result.Length, "&amp;auth=" + authCode);
                            }
                        }


                        if (refId > 0 && result.IndexOf("refid=") == -1)
                        {
                            if (result.IndexOf('?') == -1)
                            {
                                result = result.Insert(result.Length, "?refid=" + refId);
                            }
                            else
                            {
                                result = result.Insert(result.Length, "&amp;refid=" + refId);
                            }
                        }
                    }
                    return result;
                }


                public class RegexResource
                {
                    public static readonly string HREF = @"(?<=href=')(?<Link>.*?)(?=')";
                    public static readonly string HREFS = @"(?<=href=\"")(?<Link>.*?)(?=\"")";
                    //public static readonly string ACTION = "(<FORM.*ACTION=\")([^\"]*)(\"[^>]*>)";
                    public static readonly string ACTION = @"(?<=<\s*form\s.*?action\s*="").*?(?="".*?>)";
                }


                /// <summary>
                ///
                /// </summary>
                /// <returns></returns>
                private static string GetAuthCodeByRequest()
                {
                    string auth = string.Empty;
                    if (!string.IsNullOrEmpty(HttpContext.Current.Request["auth"]))
                    {
                        auth = HttpContext.Current.Request["auth"];
                    }


                    return auth;
                }


                /// <summary>
                ///
                /// </summary>
                /// <returns></returns>
                private static long GetRefIdByRequest()
                {
                    long refId = 0;
                    if (!string.IsNullOrEmpty(HttpContext.Current.Request["refid"])
                        && Validator.IsInt64(HttpContext.Current.Request["refid"]))
                    {
                        refId = Int64.Parse(HttpContext.Current.Request["refid"]);
                    }
                    else if (!string.IsNullOrEmpty(HttpContext.Current.Request["unionId"])
                        && Validator.IsInt64(HttpContext.Current.Request["unionId"]))
                    {
                        refId = Int64.Parse(HttpContext.Current.Request["unionId"]);
                    }


                    return refId;
                }
            }
            #endregion
        }


        // a temp handler used to force the SessionStateModule to load session state
        public class UrlParamsFilterHttpHandler : IHttpHandler, IRequiresSessionState
        {
            internal readonly IHttpHandler OriginalHandler;


            public UrlParamsFilterHttpHandler()
            {
                OriginalHandler = HttpContext.Current.Handler;
            }


            public UrlParamsFilterHttpHandler(IHttpHandler originalHandler)
            {
                OriginalHandler = originalHandler;
            }


            public void ProcessRequest(HttpContext context)
            {
                //// do not worry, ProcessRequest() will not be called, but let's be safe
                //throw new InvalidOperationException("MyHttpHandler cannot process requests.");
            }


            public bool IsReusable
            {
                // IsReusable must be set to false since class has a member!
                get { return false; }
            }
        }
    }


    asp.net windows phone sivlerlght

    2012年2月25日 1:16
  • 分而治之法,测试一下看一下此module里面哪个method最慢,然后有针对性的处理。
    2012年2月25日 3:28
  • 多谢你的指教,我监控发现httpModel 运行的时间很长,具体的出问题的方法是是Application_PostMapRequestHandler 运行的时间很长,具体代码如下。

                      

      private void Application_PostMapRequestHandler(object source, EventArgs e)
            {

                HttpApplication application = (HttpApplication)source;


                if (application.Context.Handler is IReadOnlySessionState || application.Context.Handler is IRequiresSessionState)
                {
                    // no need to replace the current handler 
                    return;
                }


                // swap the current handler 
                application.Context.Handler = new UrlParamsFilterHttpHandler(application.Context.Handler);
            }

      // a temp handler used to force the SessionStateModule to load session state 
        public class UrlParamsFilterHttpHandler : IHttpHandler, IRequiresSessionState
        {
            internal readonly IHttpHandler OriginalHandler;


            public UrlParamsFilterHttpHandler()
            {
                OriginalHandler = HttpContext.Current.Handler;
                HttpContext.Current.Trace.Write(MethodInfo.GetCurrentMethod().Name, DateTime.Now.ToLongTimeString());
            }


            public UrlParamsFilterHttpHandler(IHttpHandler originalHandler)
            {
                OriginalHandler = originalHandler;
                HttpContext.Current.Trace.Write(MethodInfo.GetCurrentMethod().Name, DateTime.Now.ToLongTimeString());
            }


            public void ProcessRequest(HttpContext context)
            {
                HttpContext.Current.Trace.Write(MethodInfo.GetCurrentMethod().Name, DateTime.Now.ToLongTimeString());
                //// do not worry, ProcessRequest() will not be called, but let's be safe 
                //throw new InvalidOperationException("MyHttpHandler cannot process requests.");
            }


            public bool IsReusable
            {
                // IsReusable must be set to false since class has a member! 
                get { return false; }
            }
        }


    asp.net windows phone sivlerlght

    2012年2月27日 3:14
  • 建议您去ASP.Net 论坛去试试看,ASP.NET的专家会帮你找出相关的原因。

    Http://forums.asp.net/

    或者是中文的asp.net 与AJAX论坛:

    http://social.microsoft.com/forums/zh-CN/295/threads/

    希望能帮得到您。


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us

    2012年2月28日 5:53
  • ..net Framework 2.0 与 4.0 彼此独立,不是升级取代的关系,不必在安装 4.0 之前卸载 2.0,可以令它们共存。
     
    --
    Alexis Zhang
     
    http://mvp.support.microsoft.com/profile/jie
    http://blogs.itecn.net/blogs/alexis
     
    推荐以 NNTP Bridge 桥接新闻组方式访问论坛以获取最佳用户体验。
     
    本帖是回复帖,原帖作者是楼上的 "and yu"
     
    大家好,我们团队现在用开发环境是vs2010 .net 2.o framework asp.net, 昨天项目从.net 2.0 升级到。.net 4.0项目以后发现网站运行十分缓慢,页面95%的页面都无打开,即使打开首页?残枰眉阜种樱?
     
    2012年3月1日 21:30
    版主