Forgive me, I'm new to the model/view model structure. I am trying to call a dependency to a method (IAuthenticationService) and it returns as null and I am unsure why. I tried adjusting a few of my methods and it still returns null. Any help is much appreciated. I am using .net MAUI.
The service I made.
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);
}
}
My MauiProgram.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();
Where Im trying to call it.
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);
}
The above line var test, authentication service returns null.
I have tried creating a ViewModel with the same structure and it still fails. I am at a loss and cant find where I went wrong, I have read multiple articles and have tried what they mentioned and it still returns null. I know for a fact it's something on my end. I just cant find why.
CodePudding user response:
you need to include the parameter in your constructor
IAuthenticationService AuthenticationService;
public ProfileInformation(IAuthenticationService authService)
{
InitializeComponent();
this.AuthenticationService = authService;
}
CodePudding user response:
I think that you have to inject your service in the constructor.
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);
}