Asp.Net 核心 class 没有传递数据
Asp.Net Core class not passing data
在 asp.net 核心 2.1 身份中,我使用 class 将登录名从 ExternalLogin.cshtml.cs 和 Login.cshtml 中移动到另一个 table通过 class AddUserToStudentTable.
编辑 - 我弄错了 DTO 的术语,但认为它只是一个推动数据的 class。我只是使用了错误的命名约定。
class是
public class StudentNameDTO : IStudentNameDTO
{
public string StudentGoogleNameLogin { get; set; } = string.Empty;
public bool IsExternal { get; set; } = false;
}
Startup 使用的是 AddSingleton,但我也尝试过 AddTransient,没有任何区别。
services.AddSingleton<IStudentNameDTO, StudentNameDTO>();
而且我使用的是通常的构造函数注入,它是通过出色的 VS 2017 自动完成的
然而,在传递数据时,我总是得到
的错误
evaluation of method () calls into native method system System.Environment.FailFast().
这一切都随着
崩溃了
NullReferenceException: Object reference not set to an instance of an object.
ASPNZBat.Business.AddUserToStudentTable.AddUserToStudent(string Email) in AddUserToStudentTable.cs + if (_studentNameDTO.IsExternal == true) ASPNZBat.Areas.Identity.Pages.Account.ExternalLoginModel.OnGetCallbackAsync(string returnUrl, string remoteError) in ExternalLogin.cshtml.cs + _addUserToStudentTable.AddUserToStudent(Email);
我也尝试过在启动时使用 AddTransient 和 AddScope,但没有区别。工作了几个小时后,我开始怀疑自己的编程能力....
请注意,当有数据通过时,它工作正常。但是当没有数据时 - null - 而不是使用它只是崩溃。我什至将它包装在一个布尔值中,看看我是否可以用它捕获输出,但它也在布尔值处崩溃了。
数据进入
if (info.Principal.Identity.Name != null)
{
_studentNameDTO.IsExternal = true;
_studentNameDTO.StudentGoogleNameLogin = info.Principal.Identity.Name;
string Email = info.Principal.FindFirstValue(ClaimTypes.Email);
_addUserToStudentTable.AddUserToStudent(Email);
}
数据出来
string StudentName = string.Empty;
if (_studentNameDTO.IsExternal == true)
{
StudentName = _studentNameDTO.StudentGoogleNameLogin;
}
它不喜欢传递空数据,我也不明白。
这是它的 github 帐户 https://github.com/Netchicken/ASPNZBatV2/tree/master/ASPNZBat
您似乎遇到了 过度依赖注入。
Aside: You almost certainly don't want to be using AddSingleton
here, a singleton is something that you want your application to have no more than one instance of during its execution. In this instance that would mean that if you had two users logging (or whatever the process is here) in at the same time they would both share the same instance of StudentNameDTO
.
根据 AddUserToStudentTable.cs 中的代码,您看到 NullReferenceException
的原因是这里没有任何东西在使用之前分配给 _studentNameDTO
。它没有被注入到任何地方,也没有被传递到 class 任何地方,它被声明为 private
因此无法从 class 外部访问并且只能 read 来自第 36 和 38 行。
也就是说,并非代码中的所有内容都需要或应该通过依赖注入进行实例化。您的 StudentNameDTO
不是 class 依赖 的东西,而是它消耗/修改的东西。粗略地看一下你的代码,它看起来像获取存储在 StudentNameDTO
中的所有数据的地方在 ExternalLoginModel.OnGetCallbackAsync
中,所以这是你在调用 [=20= 之前应该 var studentNameDto = new StudentNameDTO()
的地方] 和 将 StudentNameDTO
的实例传递到方法 中,例如(从第 97 行开始):
if (info.Principal.Identity.Name != null)
{
var studentNameDto = new StudentNameDTO
{
IsExternal = true,
_studentNameDTO.StudentGoogleNameLogin = info.Principal.Identity.Name
};
string Email = info.Principal.FindFirstValue(ClaimTypes.Email);
_addUserToStudentTable.AddUserToStudent(studentNameDto, Email);
}
在 asp.net 核心 2.1 身份中,我使用 class 将登录名从 ExternalLogin.cshtml.cs 和 Login.cshtml 中移动到另一个 table通过 class AddUserToStudentTable.
编辑 - 我弄错了 DTO 的术语,但认为它只是一个推动数据的 class。我只是使用了错误的命名约定。
class是
public class StudentNameDTO : IStudentNameDTO
{
public string StudentGoogleNameLogin { get; set; } = string.Empty;
public bool IsExternal { get; set; } = false;
}
Startup 使用的是 AddSingleton,但我也尝试过 AddTransient,没有任何区别。
services.AddSingleton<IStudentNameDTO, StudentNameDTO>();
而且我使用的是通常的构造函数注入,它是通过出色的 VS 2017 自动完成的
然而,在传递数据时,我总是得到
的错误evaluation of method () calls into native method system System.Environment.FailFast().
这一切都随着
崩溃了NullReferenceException: Object reference not set to an instance of an object. ASPNZBat.Business.AddUserToStudentTable.AddUserToStudent(string Email) in AddUserToStudentTable.cs + if (_studentNameDTO.IsExternal == true) ASPNZBat.Areas.Identity.Pages.Account.ExternalLoginModel.OnGetCallbackAsync(string returnUrl, string remoteError) in ExternalLogin.cshtml.cs + _addUserToStudentTable.AddUserToStudent(Email);
我也尝试过在启动时使用 AddTransient 和 AddScope,但没有区别。工作了几个小时后,我开始怀疑自己的编程能力....
请注意,当有数据通过时,它工作正常。但是当没有数据时 - null - 而不是使用它只是崩溃。我什至将它包装在一个布尔值中,看看我是否可以用它捕获输出,但它也在布尔值处崩溃了。
数据进入
if (info.Principal.Identity.Name != null)
{
_studentNameDTO.IsExternal = true;
_studentNameDTO.StudentGoogleNameLogin = info.Principal.Identity.Name;
string Email = info.Principal.FindFirstValue(ClaimTypes.Email);
_addUserToStudentTable.AddUserToStudent(Email);
}
数据出来
string StudentName = string.Empty;
if (_studentNameDTO.IsExternal == true)
{
StudentName = _studentNameDTO.StudentGoogleNameLogin;
}
它不喜欢传递空数据,我也不明白。
这是它的 github 帐户 https://github.com/Netchicken/ASPNZBatV2/tree/master/ASPNZBat
您似乎遇到了 过度依赖注入。
Aside: You almost certainly don't want to be using
AddSingleton
here, a singleton is something that you want your application to have no more than one instance of during its execution. In this instance that would mean that if you had two users logging (or whatever the process is here) in at the same time they would both share the same instance ofStudentNameDTO
.
根据 AddUserToStudentTable.cs 中的代码,您看到 NullReferenceException
的原因是这里没有任何东西在使用之前分配给 _studentNameDTO
。它没有被注入到任何地方,也没有被传递到 class 任何地方,它被声明为 private
因此无法从 class 外部访问并且只能 read 来自第 36 和 38 行。
也就是说,并非代码中的所有内容都需要或应该通过依赖注入进行实例化。您的 StudentNameDTO
不是 class 依赖 的东西,而是它消耗/修改的东西。粗略地看一下你的代码,它看起来像获取存储在 StudentNameDTO
中的所有数据的地方在 ExternalLoginModel.OnGetCallbackAsync
中,所以这是你在调用 [=20= 之前应该 var studentNameDto = new StudentNameDTO()
的地方] 和 将 StudentNameDTO
的实例传递到方法 中,例如(从第 97 行开始):
if (info.Principal.Identity.Name != null)
{
var studentNameDto = new StudentNameDTO
{
IsExternal = true,
_studentNameDTO.StudentGoogleNameLogin = info.Principal.Identity.Name
};
string Email = info.Principal.FindFirstValue(ClaimTypes.Email);
_addUserToStudentTable.AddUserToStudent(studentNameDto, Email);
}