我有一个ASP.NET核心项目,我正try 在其中使用依赖项注入. 然而,我得到了这个例外:
模型绑定的复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数.记录类型必须有一个主构造函数.或者,给‘googleAuthHelperService’参数一个非空默认值.
这是因为try 将接口IGoogleAuthHelperService注入到我的控制器类中:
[Route("/auth")]
public async Task<IActionResult> AuthAsync(IGoogleAuthHelperService googleAuthHelperService)
{
string response = await GoogleAuthHelperService.ReadHttpBodyAsync(Request.Body);
string code = GoogleAuthHelperService.ParseGoogleResponseCode(response);
//string code = Request.Body.
await googleAuthHelperService.GetTokenAsync(code);
if (response != null)
{
return View("Error");
}
else
{
return View("_Auth");
}
}
public interface IGoogleAuthHelperService
{
public Task<string> GetTokenAsync(string code);
}
public class GoogleAuthHelperService : IGoogleAuthHelperService
{
private GoogleAuthSettings _settings;
public GoogleAuthHelperService()
{
}
public GoogleAuthHelperService(IGoogleAuthSettingsService googleAuthSettingsService)
{
if (googleAuthSettingsService.Settings != null)
{
_settings = googleAuthSettingsService.Settings;
}
else
{
_settings = new GoogleAuthSettings();
}
}
public async Task<string> GetTokenAsync(string code)
{
HttpHelper httpHelper = new(_settings.GoogleTokenUrl); // googleauthsettings.token
_settings.AuthorizationCode = code;
string content = BuildGoogleTokenBody();
}
public static async Task<string> ReadHttpBodyAsync(Stream stream)
{
using (StreamReader streamReader = new(stream))
{
string content = await streamReader.ReadToEndAsync();
return content;
}
}
}
如您所见,我将一个IGoogleAuthSettingsService类注入到我的构造函数中,该构造函数用于从appsettings.json文件中获取设置:
public interface IGoogleAuthSettingsService
{
public GoogleAuthSettings? Settings {get; }
}
public class GoogleAuthSettingsService : IGoogleAuthSettingsService
{
public GoogleAuthSettings? Settings { get; set; }
}
即使当我使用IGoogleAuthSettingsService删除构造函数时,我也收到了相同的异常,并且我有点迷茫:
public interface IGoogleAuthHelperService
{
public Task<string> GetTokenAsync(string code);
}
public class GoogleAuthHelperService : IGoogleAuthHelperService
{
private GoogleAuthSettings _settings;
public GoogleAuthHelperService()
{
}
public GoogleAuthHelperService(IGoogleAuthSettingsService googleAuthSettingsService)
{
if (googleAuthSettingsService.Settings != null)
{
_settings = googleAuthSettingsService.Settings;
}
else
{
_settings = new GoogleAuthSettings();
}
}
public async Task<string> GetTokenAsync(string code)
{
HttpHelper httpHelper = new(_settings.GoogleTokenUrl); // googleauthsettings.token
_settings.AuthorizationCode = code;
string content = BuildGoogleTokenBody();
}
}
下面是我将服务注册为Singleton的Program.cs:
using GmailCleaner.Models;
using GmailCleaner.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
IGoogleAuthSettingsService? googleAuthService = builder.Configuration.Get<GoogleAuthSettingsService>();
builder.Services.AddSingleton<IGoogleAuthSettingsService>(googleAuthService);
GoogleAuthHelperService googleAuthHelperService = new();
builder.Services.AddSingleton<IGoogleAuthHelperService, GoogleAuthHelperService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
app.Run();
我也在我的Program.cs中try 了这样的注射:
GoogleAuthHelperService googleAuthHelperService = new();
builder.Services.AddSingleton<IGoogleAuthHelperService>(googleAuthHelperService);
有谁知道如何修复我正在注射的类吗?