Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Startup.cs中正确创建服务?无法创建错误类型的实例_C#_Asp.net Core_Asp.net Core Mvc_Asp.net Identity - Fatal编程技术网

C# 如何在Startup.cs中正确创建服务?无法创建错误类型的实例

C# 如何在Startup.cs中正确创建服务?无法创建错误类型的实例,c#,asp.net-core,asp.net-core-mvc,asp.net-identity,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Identity,当在发布后访问构造函数时(在get过程中没有问题),我会遇到这个错误 {“无法创建类型为“MyWebsite.Website.Main.Areas.Identity.Pages.Account.LoginWith2faModel+IIInputModel”的实例。模型绑定的复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数 在我的StartUp.cs中,我清楚地为创建InputModel实例的接口包含了一个临时服务。我无法理解为什么会出现这种错误。是因为接口和类都是在我的LoginWit

当在发布后访问构造函数时(在get过程中没有问题),我会遇到这个错误

{“无法创建类型为“MyWebsite.Website.Main.Areas.Identity.Pages.Account.LoginWith2faModel+IIInputModel”的实例。模型绑定的复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数

在我的
StartUp.cs
中,我清楚地为创建InputModel实例的接口包含了一个
临时服务
。我无法理解为什么会出现这种错误。是因为
接口
都是在我的LoginWith2faModel类中定义的吗?
我这样问是因为在另一个项目中,我有一个类似的设置,没有问题。同样,我有一个构造函数,它接受
接口
,我将
接口
的具体实现传递给该类,并在构造函数中对其进行初始化。这和那之间的唯一区别是asp.net核心的DI非常感谢lp!

Startup.cs
services.AddTransient();
//之前,我将上述设置为:
//services.AddTransient();
LoginWith2faModel.cs
private readonly-SignInManager\u-SignInManager;
专用只读ILogger\u记录器;
带有2个模型的公共登录(SignInManager SignInManager、ILogger logger、IInputModel inputModel)
{
_signInManager=signInManager;
_记录器=记录器;
输入=输入模型;
}
[BindProperty]
公共IIInputModel输入{get;set;}
公共布尔记忆{get;set;}
公共字符串ReturnUrl{get;set;}
公共接口IIInputModel
{
字符串TwoFactorCode{get;set;}
bool RememberMachine{get;set;}
}
公共类输入模型:IIInputModel
{
公共字符串双因素代码{get;set;}
公共布尔记忆机{get;set;}
}

错误页面是什么样子的?更改
公共IIInputModel输入{get;set;}
以使用具体类型…
公共InputModel输入{get;set;}
我之前尝试过@phuzi。之后,由于类型不匹配错误,我还必须将构造函数参数类型更改为
InputModel
。但我完全理解您的意思,它应该可以工作。即使我创建了
IInputModel
类型的新属性并将其实例化为新的
InputModel
它也不会抛出错误错误!有什么建议吗?问题是绑定无法实例化
IInputModel
。您应该使用一个单独的视图模型,该模型具有具体类型,可以轻松映射回基础模型。@steve这是一段时间以前的事了。您是否整理了2FA?我正在尝试使用当前的示例,但遇到了困难…该视图模型是什么错误页面看起来像?更改
public IIInputModel输入{get;set;}
以使用具体类型…
public InputModel输入{get;set;}
我之前尝试过@phuzi。之后,由于类型不匹配错误,我还必须将构造函数参数类型更改为
InputModel
。但我完全理解您的意思,它应该可以工作。即使我创建了
IInputModel
类型的新属性并将其实例化为新的
InputModel
它也不会抛出错误错误!有什么建议吗?问题是绑定无法实例化
IInputModel
。您应该使用一个单独的视图模型,该模型具有具体类型,可以轻松映射回基础模型。@steve这是一段时间以前的事了。您是否整理了2FA?我正在尝试使用当前示例,但很难。。。
services.AddTransient<LoginWith2faModel.InputModel>();
//previously I had the above set to:
//services.AddTransient<LoginWith2faModel.IInputModel,LoginWith2faModel.InputModel>();
private readonly SignInManager<IdentityUser> _signInManager;
private readonly ILogger<LoginWith2faModel> _logger;
public LoginWith2faModel(SignInManager<IdentityUser> signInManager, ILogger<LoginWith2faModel> logger, IInputModel inputModel)
{
    _signInManager = signInManager;
    _logger = logger;
    Input = inputModel;
}

[BindProperty]
public IInputModel Input { get; set; }
public bool RememberMe { get; set; }
public string ReturnUrl { get; set; }
public interface IInputModel
{
    string TwoFactorCode { get; set; }
    bool RememberMachine { get; set; }
}

public class InputModel : IInputModel
{
    public string TwoFactorCode { get; set; }
    public bool RememberMachine { get; set; }
}