.net 依赖项注入返回Null

7tofc5zh  于 2022-12-27  发布在  .NET
关注(0)|答案(2)|浏览(209)

请原谅,我是模型/视图模型结构的新手。我正在尝试调用一个方法(IAuthenticationService)的依赖项,但它返回null,我不确定原因。我尝试调整了一些方法,但它仍然返回null。非常感谢任何帮助。我使用的是.net MAUI。
我所做的贡献。

public interface IAuthenticationService
    {
        User User { get; set; }
        Task Update(int userId, string firstName, string lastname);

    }

    public class AuthenticationService : IAuthenticationService
    {
        private IHttpService _httpService;
        private NavigationManager _navigationManager;
        private ILocalStorageService _localStorageService;

        public User User { get;  set; }

        public AuthenticationService(
            IHttpService httpService,
            NavigationManager navigationManager,
            ILocalStorageService localStorageService
        ) {
            _httpService = httpService;
            _navigationManager = navigationManager;
            _localStorageService = localStorageService;
        }

        public async Task Update(int userId, string firstName, string lastname)
        {

                User = await _httpService.Put<User>($"URL{userId}", new { firstName, lastname});
                var output = JsonSerializer.Serialize(User);
                await SecureStorage.SetAsync("UserInfo", output);

        }

    }

我的毛伊岛程序. cs

var builder = MauiApp.CreateBuilder();
        builder
            .UseSkiaSharp()
            .UseMauiApp<App>().UseMauiCommunityToolkit()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });
        builder.Services
                .AddScoped<IAuthenticationService, AuthenticationService>()
                .AddScoped<IUserService, UserService>()
                .AddScoped<IHttpService, HttpService>()
                .AddScoped<ILocalStorageService, LocalStorageService>();

        builder.Services.AddScoped(x => {
            var apirl = new Uri("URL");

            return new HttpClient() { BaseAddress = apiUrl };
        });

        builder.Services.AddMauiBlazorWebView();
        #if DEBUG
        builder.Services.AddBlazorWebViewDeveloperTools();
#endif
        
        builder.Services.AddSingleton<WeatherForecastService>();

        return builder.Build();

我试着称之为。

public partial class ProfileInformation : ContentPage
{
    IAuthenticationService AuthenticationService;
    private User user = new User();
    public ProfileInformation()
    {
        InitializeComponent();

    }

    async void Button2_Clicked(object sender, EventArgs e)
    {
int id = "1"
string namef = "test"
string namel = "test"
        var test = AuthenticationService.Update(id, namef, namel);
        
    }

以上行var测试,验证服务返回null.
我试过用同样的结构创建一个视图模型,但还是失败了。我不知所措,找不到哪里出错了。我读了很多文章,试过他们提到的方法,但它仍然返回null。我知道这是我这边的问题。我只是找不到原因。

rggaifut

rggaifut1#

需要在构造函数中包含参数

IAuthenticationService AuthenticationService;

public ProfileInformation(IAuthenticationService authService)
{
    InitializeComponent();

    this.AuthenticationService = authService;
}
pkwftd7m

pkwftd7m2#

我认为您必须在构造函数中注入服务。

public partial class ProfileInformation : ContentPage
{
    IAuthenticationService AuthenticationService;
    private User user = new User();
    public ProfileInformation(IAuthenticationService authenticationService)
    {
        AuthenticationService = authenticationService;
        InitializeComponent();

    }

    async void Button2_Clicked(object sender, EventArgs e)
    {
int id = "1"
string namef = "test"
string namel = "test"
        var test = AuthenticationService.Update(id, namef, namel);
        
    }

相关问题