Cześć. Piszę aplikację w asp.net core mvc i dzisiaj napotkałem na problem przy pobieraniu danych z bazy:
Wygląda na to, że problem występuje przy połączeniu z bazą jednak ciężko jest to sprawdzić, ponieważ ustawiając breakpointa program nie zatrzymuje się, a wyrzuca od razu Exception. Breakpointy zacząłem ustawiać od samego początku aby namierzyć problem, ale ciężko namierzyć. Z tego co udało mi się zaobserwować to pojawia się on w tym miejscu:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using WoodStore.AppCore.Models;
using WoodStoreApp.AppCore;
namespace WoodStore.AppCore
{
public class SqlConnector
{
public static SqlConnection SqlConnection
{
get
{
SqlConnection conn = new SqlConnection();
List<object> connectionModels = Serialization.Deserialize(new FileStream("JSON/sqlconfiguration.json", FileMode.Open), typeof(ConnectionModel));
foreach (object c in connectionModels)
{
if (IsConnection(((ConnectionModel)c).ConnectionString))
{
conn.ConnectionString = Crypto.GetString(((ConnectionModel)c).ConnectionString);
break;
}
}
return conn;
}
}
private static bool IsConnection(string connectionString)
{
var tmp = Crypto.GetString(connectionString);
using (SqlConnection conn = new SqlConnection(Crypto.GetString(connectionString)))
{
try
{
conn.Open();
conn.Close();
return true;
}
catch (Exception)
{
return false;
}
}
}
}
}
Program dochodzi do linii 40 i wrzucił exception w innym miejscu. Ten wyjątek zawsze występuje w tym miejscu (w linii 4):
[HttpPost]
public ActionResult Login(AccountView model)
{
if (UserModel.GetLoginDatas.Where(x => x.Mail == model.User.Mail && x.Password == model.User.Password).ToList().Count != 0)
{
}
model.ReturnURL = ReturnURL;
return View();
}
Przy kolejnych próbach nie udało mi się potwierdzić tego problemu. Z czasem zauważyłem, że wyjątek pojawia się w dowolnych momentach wykonywania tego fragmentu kodu.
Pozostały kod:
using AppCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using WoodStoreApp.AppCore;
namespace WoodStore.Models
{
public class UserModel
{
public int UserID { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public DateTime JoinDate { get; set; }
public string AccountType { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Adress { get; set; }
public string PostCode { get; set; }
public string City { get; set; }
public string Voivodeship { get; set; }
public string Phone { get; set; }
public string Mail { get; set; }
public string Fax { get; set; }
public static List<UserModel> GetLoginDatas = SqlOperations.GetData("t_users").Tables[0].AsEnumerable().Select(x => new UserModel() { Mail = x.Field<string>("Mail"), Password = Crypto.GetString(x.Field<string>("Password")) }).ToList();
}
}
using System.Data;
using System.Data.SqlClient;
using WoodStore.AppCore;
namespace AppCore
{
public class SqlOperations
{
public static DataSet GetData(string tableName)
{
DataSet result = new DataSet();
using (SqlConnection conn = SqlConnector.SqlConnection)
{
using (SqlDataAdapter adapter = new SqlDataAdapter(string.Format("select * from {0}", tableName), conn))
{
adapter.Fill(result);
}
}
return result;
}
}
}