asp.net-mvc – 使用mvc-mini-profiler

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 使用mvc-mini-profiler前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用带有EFCodeFirst的mvc-mini-profiler我正在创建一个DbProfiledConnection并将其传递给DbContext,如下所示.应用程序继续按照预期工作,sql不会暴露给Profiler.
  1. public class WebContext : DbContext
  2. {
  3. static DbConnection _connection = new sqlConnection(ConfigurationManager.ConnectionStrings["WebContext"].ConnectionString);
  4. static DbConnection _profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(_connection);
  5.  
  6. public WebContext()
  7. : base(_profiledConnection,true)
  8. {
  9.  
  10. }

哎哟我的坏

我已修改它,以便当我的WebContext构造在我的UnitOfWork中,我传入一个ProfiledDbConnection

  1. public UnitOfWork()
  2. {
  3. var profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(connection);
  4. this.context = new MyContext(profiledConnection);
  5. }

我已经检查过,并且在Application_BeginRequest中设置了MiniProfier Current,并且当我尝试查询数据库时在ProfiledDbProviderServices类中抛出一个错误时返回一个ProfiledDbConnection.

  1. protected override string GetDbProviderManifestToken(DbConnection connection)
  2. {
  3. return tail.GetProviderManifestToken(connection);
  4. }

方法返回“提供程序未返回ProviderManifestToken字符串”.错误

解决方法

我怀疑这与静态字段初始化器有关.网络应用程序上的连接不应该是静态的(最多是请求特定的).

关键是:ProfiledDbConnection是什么实际出来的?只有当您正在对当前请求进行分析时,Get方法才会返回ProfilingDbConnection,并且根据该请求针对MiniProfiler实例对连接进行概要分析.

如果使用静态字段,则有两种情况:

>静态字段在没有请求上下文(或非开发者请求上下文)的情况下被初始化):不会发生分析,因为MiniProfiler.Current为null>静态字段被初始化,但是一切都按照第一个请求进行记录,这个请求很快死亡

猜你在找的asp.Net相关文章