我如何与.net Maui中的IhttpClientFactory正确使用DI
我在.net Maui中还没有发现有关HTTPCLIENT的任何信息。
有人知道该服务是否:
builder.services.addhttpclient< imyservice,myService>();
在毛伊岛的启动Mauipragram.cs中可以吗?然后将HTTPClient注入将要使用的位置。我已经尝试了所有事情,但它似乎不起作用。仅添加httpclient的Singleton对我有效,但似乎并不最佳。
ps。:我必须安装nuget软件包microsoft.extensions.http,以便使用AddHttpClient服务。
更新:
工作代码:
mauipragram.cs
builder.Services.AddTransient<Service<Display>, DisplayService>();
builder.Services.AddTransient<Service<Video>, VideoService>();
builder.Services.AddTransient<Service<Image>, ImageService>();
builder.Services.AddTransient<Service<Log>, LogService>();
builder.Services.AddSingleton(sp => new HttpClient() { BaseAddress = new Uri("https://api.myapi.com") });
videosviewmodel.cs使用服务的示例
[INotifyPropertyChanged]
public partial class VideosViewModel
{
readonly Service<Video> videoService;
[ObservableProperty]
ObservableCollection<Video> videos;
[ObservableProperty]
bool isEmpty;
[ObservableProperty]
bool isRefreshing;
public VideosViewModel(Service<Video> videoService)
{
this.videoService = videoService;
}
[ICommand]
internal async Task LoadVideosAsync()
{
#if ANDROID || IOS || tvOS || Tizen
UserDialogs.Instance.ShowLoading("Henter videoer fra databasen...");
#endif
await Task.Delay(2000);
Videos = new();
try
{
await foreach (Video video in videoService.GetAllAsync().OrderBy(x => x.Id))
{
Videos.Add(video);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
IsRefreshing = false;
#if ANDROID || IOS || tvOS
UserDialogs.Instance.HideLoading();
#endif
if (Videos.Count is 0)
{
IsEmpty = true;
}
else
{
IsEmpty = false;
}
}
}
[ICommand]
async Task UploadVideoAsync()
{
await Shell.Current.DisplayAlert("Upload en video", "Under opbygning - kommer senere!", "OK");
}
}
不工作代码:
mauipragram.cs
builder.Services.AddHttpClient<Service<Display>, DisplayService>(sp => sp.BaseAddress = new Uri("https://api.myapi.com"));
builder.Services.AddHttpClient<Service<Video>, VideoService>(sp => sp.BaseAddress = new Uri("https://api.myapi.com"));
builder.Services.AddHttpClient<Service<Image>, ImageService>(sp => sp.BaseAddress = new Uri("https://api.myapi.com"));
builder.Services.AddHttpClient<Service<Log>, LogService>(sp => sp.BaseAddress = new Uri("https://api.myapi.com"));
videosviewmodel.cs 与上述工作代码相同。
具体不起作用的是,我在orderby(x =&gt; x.id)上获得对象参考异常,特别是在ViewModel中突出显示X.ID。删除Orderby方法不再例外,但是视图没有显示一个随机空帧以外的数据。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请勿使用Builder.Services.AddhttpClient在MAUI中。
使用一个实例。
Do not use builder.Services.AddHttpClient in MAUI.
Use one instance.
Builder.Services.AddhttpClient为我工作。这是我设置矿山的方式:在主程序中,我添加了.addhttpclient()
和appshell.xaml,我删除了DataTemplate的定义,使其像这样:
请注意,请注意我给ShellContents的X:名称。 DataTemplates在AppShell.xAML背后的代码中定义了,我可以将Func对象与IHTTPCLIENTFACTORY作为DI参数,看起来像这样:
对于App类,我们可以注入IHTTPCLIENTFACTORY,它可以将其传递给Appshell:
以及每个人。页面我将在我的github存储库中获得IHTTPCLIENTFACTORY
一个完整的解决方案:
builder.Services.AddHttpClient works for me. Here is how I setup mine: in the main program, I added .AddHttpClient()
And in AppShell.xaml I removed the DataTemplate definition, made it barebone like this:
Notice that I gave an x:Name to the ShellContents. The DataTemplates are defined in the code behind of AppShell.xaml where I can pass a Func object with IHttpClientFactory as a DI parameter, which looks like this:
For the App class we can inject IHttpClientFactory, which is passed to AppShell:
And in each individual page I would have iHttpClientFactory available from the Func DI parameter
A complete solution for this example is in my GitHub repository: https://github.com/yogigrantz/MAUIWithHttpClientFactory