实体框架在第一次请求时响应很晚

时间:2015-10-26 21:54:10

标签: c# asp.net-mvc-4 c#-4.0 asp.net-web-api asp.net-web-api2

我的WebAPI代码托管在GoDaddy服务器上。

我有两个问题:

  
      
  1. 在服务器上更新代码后,action方法在第一次调用后35秒后返回响应。从这个问题来看,我是   假设应用程序池回收然后需要时间,   但我不确定。

  2.   
  3. 当用户第二天向服务器发送请求时,会花费相同的时间。我不确定为什么每个请求需要一个   很多时间。在这种情况下,应用程序池不会被回收。

  4.   

类库项目名称:BusinessEntites

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);
}

类库项目名称: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");
    }
}

类库项目名称:DatabaseDesign

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; }
}

类库项目名称:DatabaseDesign

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();
    }
}

类库项目名称:DAL

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);
        }
    }
}

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 });
}

我是否需要进行任何代码改进或是否需要修改任何设置?

0 个答案:

没有答案