| |
| |
| |
| public class ServiceCollectionServiceLocator: ServiceLocatorImplBase |
| { |
| private readonly IServiceProvider services; |
| |
| public ServiceCollectionServiceLocator(IServiceProvider services) |
| { |
| this.services = services ?? throw new ArgumentNullException(); |
| } |
| |
| protected override object DoGetInstance(Type serviceType, string key) |
| { |
| if (serviceType == null) |
| { |
| throw new ArgumentNullException(nameof(serviceType)); |
| } |
| |
| return services.GetRequiredService(serviceType); |
| } |
| |
| protected override IEnumerable<object> DoGetAllInstances(Type serviceType) |
| { |
| if (serviceType == null) |
| { |
| throw new ArgumentNullException(nameof(serviceType)); |
| } |
| |
| var enumerableType = typeof(IEnumerable<>).MakeGenericType(serviceType); |
| |
| var instance = services.GetServices(enumerableType); |
| return ((IEnumerable)instance).Cast<object>(); |
| } |
| } |
在Mvvmlight中使用Microsoft.Extensions.DependencyInjection
| public class AppLocator : ViewModelBase |
| { |
| public IConfiguration Configuration { get; private set; } |
| |
| public AppLocator() |
| { |
| #region Microsoft.Extensions.DependencyInjection |
| Configuration = new ConfigurationBuilder() |
| .SetBasePath(Directory.GetCurrentDirectory()) |
| .AddJsonFile("appsettings.json", false, true) |
| .Build(); |
| |
| var provider = new ServiceCollectionServiceLocator(Configure()); |
| ServiceLocator.SetLocatorProvider(() => provider); |
| #endregion |
| |
| #region AutoFac |
| |
| |
| |
| #endregion |
| |
| #region gRpc |
| try |
| { |
| var server = new Server() |
| { |
| Services = { |
| Greeter.BindService(ServiceLocator.Current.GetInstance<GreeterService>()), |
| Categories.BindService(ServiceLocator.Current.GetInstance<CategoryService>()), |
| MovieRpc.BindService(ServiceLocator.Current.GetInstance<MovieService>())}, |
| Ports = { new ServerPort("192.168.31.37", 19840, ServerCredentials.Insecure) } |
| }; |
| server.Start(); |
| } |
| catch (Exception ex) |
| { |
| LogHelper.Error(ex.Message); |
| MessageBox.Show("程序执行遇到错误.\n详情请查看日志信息.", "gRpc", |
| MessageBoxButton.OK, MessageBoxImage.Error); |
| } |
| #endregion |
| } |
| |
| private IServiceProvider Configure() |
| { |
| var container = new ServiceCollection(); |
| |
| |
| container.Configure<AppSettings>(Configuration.GetSection(nameof(AppSettings))); |
| |
| |
| container.AddDbContext<MyContext>(options => |
| { |
| options.UseSqlite("Data Source=stk.db3"); |
| options.UseLoggerFactory(LoggerFactory.Create(builder => |
| { |
| builder.AddFilter((category, level) => |
| category == DbLoggerCategory.Database.Command.Name |
| && level == LogLevel.Information); |
| builder.AddConsole(); |
| })); |
| }); |
| container.AddSingleton<GreeterService>(); |
| container.AddSingleton<CategoryService>(); |
| container.AddSingleton<MovieService>(); |
| container.AddSingleton<StereoscopicPlayer>(); |
| container.AddSingleton(new MotionService(100, 5, 10000)); |
| |
| |
| container.AddSingleton<MainViewModel>(); |
| container.AddSingleton<CategoryViewModel>(); |
| container.AddSingleton<MovieViewModel>(); |
| container.AddSingleton<PlayTestViewModel>(); |
| container.AddSingleton<PlayRecordViewModel>(); |
| container.AddSingleton(new LogViewModel()); |
| container.AddSingleton<PlatformTestViewModel>(); |
| |
| |
| return container.BuildServiceProvider(); |
| } |
| |
| #region AutoFac Configure |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #endregion |
| |
| |
| public MainViewModel Main => ServiceLocator.Current.GetInstance<MainViewModel>(); |
| public CategoryViewModel Category => ServiceLocator.Current.GetInstance<CategoryViewModel>(); |
| public MovieViewModel Movie => ServiceLocator.Current.GetInstance<MovieViewModel>(); |
| public PlayTestViewModel PlayTest => ServiceLocator.Current.GetInstance<PlayTestViewModel>(); |
| public PlayRecordViewModel PlayRecord => ServiceLocator.Current.GetInstance<PlayRecordViewModel>(); |
| public LogViewModel Log => ServiceLocator.Current.GetInstance<LogViewModel>(); |
| public PlatformTestViewModel PlatformTest => ServiceLocator.Current.GetInstance<PlatformTestViewModel>(); |
| |
| public override void Cleanup() |
| { |
| |
| } |
| } |
使用 HttpClientFactory
| ... |
| container.AddHttpClient(); |
| ... |
| var client = _httpClientFactory.CreateClient(); |
| var html = await client.GetStringAsync("http://www.baidu.com"); |
参考资料
https://www.cnblogs.com/hippieZhou/p/10637348.html
https://www.cnblogs.com/ryzen/p/12444366.html#4538407
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗