C# 实体框架在第一次请求时响应非常晚
我的WebAPI代码托管在GoDaddy服务器上 我有两个问题:C# 实体框架在第一次请求时响应非常晚,c#,asp.net-mvc-4,c#-4.0,asp.net-web-api,asp.net-web-api2,C#,Asp.net Mvc 4,C# 4.0,Asp.net Web Api,Asp.net Web Api2,我的WebAPI代码托管在GoDaddy服务器上 我有两个问题: 在服务器上更新代码后,对于第一次调用,action方法在35秒后返回响应。从这个问题上来说,我是 假设应用程序池进行回收,然后需要时间, 但我不确定 当用户在第二天向服务器发送请求时,所花费的时间与此相同。我不知道为什么每个请求都需要 很多时间。在这种情况下,不会回收应用程序池 类库项目名称:BusinessEntites public class BE_User { [Key] public Int64 User
public class BE_User
{
[Key]
public Int64 UserID { get; set; }
public String UserName { get; set; }
public String Password { get; set; }
}
类库项目名称:IDAL
public interface IAccount
{
Task<KeyValuePair<String, BE_User>> GetUserLoginDetails(BE_User userDetails);
}
public class Dal_Account : IAccount
{
public async Task<KeyValuePair<String, BE_User>> GetUserLoginDetails(
BE_User userDetails)
{
try
{
using (var userContext = new ModelGeneration())
{
userContext.Configuration.ProxyCreationEnabled = false;
var data = await userContext.tblUser
.FirstOrDefaultAsync(i => i.UserName == userDetails.UserName &&
i.Password == userDetails.Password);
if (data == null)
return new KeyValuePair<string, BE_User>("User not found", null);
return new KeyValuePair<String, BE_User>("", data);
}
}
catch (Exception ex)
{
return new KeyValuePair<String, BE_User>(ex.Message, null);
}
}
}
公共接口IAccount
{
任务GetUserLoginDetails(BE_用户详细信息);
}
类库项目名称:DatabaseDesign
internal class UserMap : EntityTypeConfiguration<BE_User>
{
public UserMap()
{
HasKey(x => x.UserID);
Property(x => x.UserName).IsRequired();
Property(x => x.Password).IsRequired().HasMaxLength(100);
ToTable("tblUser");
}
}
public class ModelGeneration : DbContext
{
public ModelGeneration()
: base("ConnectionString")
{
if (System.Configuration.ConfigurationManager.AppSettings["UpdateDatabase"] ==
Convert.ToInt16(UpdateDatabase.Yes).ToString())
{
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<
ModelGeneration, DatabaseDesign.Migrations.Configuration>());
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister = Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType
&& type.BaseType.GetGenericTypeDefinition() ==
typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
}
public virtual IDbSet<BE_User> tblUser { get; set; }
}
internal sealed class Configuration
: DbMigrationsConfiguration<DatabaseDesign.ModelGeneration>
{
public Configuration()
{
if (System.Configuration.ConfigurationManager.AppSettings["UpdateDatabase"] ==
Convert.ToInt16(UpdateDatabase.Yes).ToString())
{
AutomaticMigrationsEnabled = true;
}
}
private void User(DatabaseDesign.ModelGeneration context)
{
BE_User SuperAdminUser = new BE_User
{
UserName = "Administrator",
Password = "Some encrypted Password",
};
context.tblUser.Add(SuperAdminUser);
}
protected override void Seed(DatabaseDesign.ModelGeneration context)
{
User(context);
context.SaveChanges();
}
}
内部类UserMap:EntityTypeConfiguration
{
公共用户映射()
{
HasKey(x=>x.UserID);
属性(x=>x.UserName).IsRequired();
属性(x=>x.Password).IsRequired().HasMaxLength(100);
ToTable(“tblUser”);
}
}
类库项目名称:DatabaseDesign
internal class UserMap : EntityTypeConfiguration<BE_User>
{
public UserMap()
{
HasKey(x => x.UserID);
Property(x => x.UserName).IsRequired();
Property(x => x.Password).IsRequired().HasMaxLength(100);
ToTable("tblUser");
}
}
public class ModelGeneration : DbContext
{
public ModelGeneration()
: base("ConnectionString")
{
if (System.Configuration.ConfigurationManager.AppSettings["UpdateDatabase"] ==
Convert.ToInt16(UpdateDatabase.Yes).ToString())
{
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<
ModelGeneration, DatabaseDesign.Migrations.Configuration>());
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister = Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType
&& type.BaseType.GetGenericTypeDefinition() ==
typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
}
public virtual IDbSet<BE_User> tblUser { get; set; }
}
internal sealed class Configuration
: DbMigrationsConfiguration<DatabaseDesign.ModelGeneration>
{
public Configuration()
{
if (System.Configuration.ConfigurationManager.AppSettings["UpdateDatabase"] ==
Convert.ToInt16(UpdateDatabase.Yes).ToString())
{
AutomaticMigrationsEnabled = true;
}
}
private void User(DatabaseDesign.ModelGeneration context)
{
BE_User SuperAdminUser = new BE_User
{
UserName = "Administrator",
Password = "Some encrypted Password",
};
context.tblUser.Add(SuperAdminUser);
}
protected override void Seed(DatabaseDesign.ModelGeneration context)
{
User(context);
context.SaveChanges();
}
}
公共类模型生成:DbContext
{
公共模型生成()
:base(“连接字符串”)
{
if(System.Configuration.ConfigurationManager.AppSettings[“UpdateDatabase”]==
Convert.ToInt16(UpdateDatabase.Yes.ToString())
{
Database.SetInitializer(
新的MigrateDatabaseToLatestVersion<
模型生成、数据库设计、迁移、配置>();
}
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
var typesToRegister=程序集
.getExecutionGassembly()
.GetTypes()
.Where(type=>!String.IsNullOrEmpty(type.Namespace))
.Where(type=>type.BaseType!=null&&type.BaseType.IsGenericType
&&type.BaseType.GetGenericTypeDefinition()==
typeof(EntityTypeConfiguration));
foreach(TypeStoreRegister中的变量类型)
{
dynamic configurationInstance=Activator.CreateInstance(类型);
modelBuilder.Configurations.Add(configurationInstance);
}
}
公共虚拟IDbSet tblUser{get;set;}
}
类库项目名称:DatabaseDesign
internal class UserMap : EntityTypeConfiguration<BE_User>
{
public UserMap()
{
HasKey(x => x.UserID);
Property(x => x.UserName).IsRequired();
Property(x => x.Password).IsRequired().HasMaxLength(100);
ToTable("tblUser");
}
}
public class ModelGeneration : DbContext
{
public ModelGeneration()
: base("ConnectionString")
{
if (System.Configuration.ConfigurationManager.AppSettings["UpdateDatabase"] ==
Convert.ToInt16(UpdateDatabase.Yes).ToString())
{
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<
ModelGeneration, DatabaseDesign.Migrations.Configuration>());
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister = Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType
&& type.BaseType.GetGenericTypeDefinition() ==
typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
}
public virtual IDbSet<BE_User> tblUser { get; set; }
}
internal sealed class Configuration
: DbMigrationsConfiguration<DatabaseDesign.ModelGeneration>
{
public Configuration()
{
if (System.Configuration.ConfigurationManager.AppSettings["UpdateDatabase"] ==
Convert.ToInt16(UpdateDatabase.Yes).ToString())
{
AutomaticMigrationsEnabled = true;
}
}
private void User(DatabaseDesign.ModelGeneration context)
{
BE_User SuperAdminUser = new BE_User
{
UserName = "Administrator",
Password = "Some encrypted Password",
};
context.tblUser.Add(SuperAdminUser);
}
protected override void Seed(DatabaseDesign.ModelGeneration context)
{
User(context);
context.SaveChanges();
}
}
内部密封类配置
:dbmigrations配置
{
公共配置()
{
如果(System.Configuration.ConfigurationManager.AppSettings[“UpdateDatabase”]==
Convert.ToInt16(UpdateDatabase.Yes.ToString())
{
AutomaticMiggerationsEnabled=真;
}
}
私有void用户(DatabaseDesign.ModelGeneration上下文)
{
BE\u用户超级管理员=新BE\u用户
{
用户名=“管理员”,
Password=“一些加密密码”,
};
context.tblUser.Add(超级管理员);
}
受保护的覆盖无效种子(DatabaseDesign.ModelGeneration上下文)
{
用户(上下文);
SaveChanges();
}
}
类库项目名称:DAL
public interface IAccount
{
Task<KeyValuePair<String, BE_User>> GetUserLoginDetails(BE_User userDetails);
}
public class Dal_Account : IAccount
{
public async Task<KeyValuePair<String, BE_User>> GetUserLoginDetails(
BE_User userDetails)
{
try
{
using (var userContext = new ModelGeneration())
{
userContext.Configuration.ProxyCreationEnabled = false;
var data = await userContext.tblUser
.FirstOrDefaultAsync(i => i.UserName == userDetails.UserName &&
i.Password == userDetails.Password);
if (data == null)
return new KeyValuePair<string, BE_User>("User not found", null);
return new KeyValuePair<String, BE_User>("", data);
}
}
catch (Exception ex)
{
return new KeyValuePair<String, BE_User>(ex.Message, null);
}
}
}
public class Dal_账户:IAccount
{
公共异步任务GetUserLoginDetails(
BE_用户详细信息)
{
尝试
{
使用(var userContext=new ModelGeneration())
{
userContext.Configuration.ProxyCreationEnabled=false;
var data=await userContext.tblUser
.FirstOrDefaultAsync(i=>i.UserName==userDetails.UserName&&
i、 Password==userDetails.Password);
如果(数据==null)
返回新的KeyValuePair(“未找到用户”,null);
返回新的KeyValuePair(“,数据);
}
}
捕获(例外情况除外)
{
返回新的KeyValuePair(例如消息,null);
}
}
}
WebAPI项目
[Route("api/v1/GetUserLoginDetails"), HttpPost]
public async Task<IHttpActionResult> GetUserLoginDetails(BE_User userDetails)
{
var result = await _account.GetUserLoginDetails(userDetails);
return Ok(new { ErrorMessage = result.Key, result = result.Value });
}
[路由(“api/v1/GetUserLoginDetails”),HttpPost]
公共异步任务GetUserLoginDetails(BE_用户详细信息)
{
var result=await_account.GetUserLoginDetails(userDetails);
返回Ok(新的{ErrorMessage=result.Key,result=result.Value});
}
我是否需要改进代码或修改设置?1)默认情况下,在一定程度的不活动后,应用程序池将关闭。我认为默认值是15分钟。对此有一个配置设置。2) 第一次编译EF查询以及为sql查询创建执行计划需要一些时间。此外,在第一次请求时,sql缓存将是冷的,这会增加对执行时间的影响。-如果我有这个问题,我会尝试想出一种方法来保持这些总是热的,就像其他地方的服务偶尔发出一次请求来保持热。我可以问一下,我如何修改Godaddy中应用程序池的超时设置?不知道。可能会写信给GoDaddy支持部门并询问他们?@zespri:我接到了GoDaddy客户服务部的电话。他们表示不支持对IIS池设置进行任何更改。因为设置是固定的。请在此处找到设置的屏幕截图。干得好。如果要更改这些,请查找其他提供商。