autofac

IoC容器Autofac之实例引入(一)

强颜欢笑 提交于 2020-02-02 19:57:31
先不必尝试理解IOC,先来看段代码。 一、一个没有使用IoC的例子 public class MPGMovieLister { public Movie[] GetMPG() { var finder = new ListMovieFinder(); var allMovies = finder.FindAll(); return allMovies.Where(m => m.Name.EndsWith(".MPG")).ToArray(); } } public class ListMovieFinder { public List<Movie> FindAll() { return new List<Movie> { new Movie { Name = "Die Hard.wmv" }, new Movie { Name = "My Name is John.MPG" } }; } } 类MPGMovieLister的作用是列出所有的mpg类型的电影,其中调用了类ListMovieFinder类的方法FindAll()来获取所有的电影。 这段代码看起来还不错,已经符合当前的需求了。 二、当需求发生变动时,非IoC遭遇到的困境 假如这个时候,movie的列表获取不是直接创建一个list获取,而要求从某个文本文件读取,或者是数据库获取,又或者从web service中获取

Autofac - SingleInstance HttpClient

一个人想着一个人 提交于 2020-01-30 21:06:18
问题 Have read in various places that HttpClient should be reused rather than a new instance every time. https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ I am using Autofac on a project. Would this be a good way of making a single instance of HttpClient available to to inject into services? builder.Register(c => new HttpClient()).As<HttpClient>().SingleInstance(); Seems to work :) 回答1: That's perfectly fine if you want one per whole application lifecycle however you might want to

Autofac - SingleInstance HttpClient

*爱你&永不变心* 提交于 2020-01-30 21:04:18
问题 Have read in various places that HttpClient should be reused rather than a new instance every time. https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ I am using Autofac on a project. Would this be a good way of making a single instance of HttpClient available to to inject into services? builder.Register(c => new HttpClient()).As<HttpClient>().SingleInstance(); Seems to work :) 回答1: That's perfectly fine if you want one per whole application lifecycle however you might want to

Protecting webapi with IdentityServer and Autofac - can't get claims

蹲街弑〆低调 提交于 2020-01-25 16:34:22
问题 I'm trying to protect my webapi with IdentityServer and OpenID Connect using Autofac. I'm using OWIN. But for some reason I can't get claims of the user. It seems that AccessTokenValidation is not triggered at all. That makes me think there is something wrong in the order of my declarations at my startup. Here is my startup. public class Startup { public void Configuration(IAppBuilder appBuilder) { // Add authentication this.AddAuthentication(appBuilder); HttpConfiguration config = new

.net core3.1 下由Autofac接管IOC

让人想犯罪 __ 提交于 2020-01-25 13:25:19
我们都知道,.net core天生自带IOC容器,但是他的功能其实并不强大,而且有坑:在构造注入的时候,他默认找参数最少的构造函数。 这里,我讲解如何使用Autofac去接管IOC,至于为什么要选Autofac,这个其实我也是随大众,不过后面我讲到关于使用Autofac实现AOP的时候,或许你能发现他的好处 第一步,引包 第二步,修改startup.cs文件 首先,在类的内部定义一个新的方法 public void ConfigureContainer(ContainerBuilder builder) { // 在这里添加服务注册 builder.RegisterType<ProductRepository>().As<IProductRepository>();//注册 } 在这个方法里,完成服务注册。对于服务注册这块,关于Autofac的API如果有需要更多的了解的话,可以查阅 Autofac指南 第三步,修改Program.cs文件 UseServiceProviderFactory(new AutofacServiceProviderFactory())//使用AutoFac做IOC和AOP 第四步,正式使用 对于使用IOC,虽然Autofac有多种骚姿势,但是我这里还是推荐使用构造注入,如果要问为什么,其实关键在于,你不用在每个类库中都去拉包,到处都拉包的话

exception while invoking the constructor autofac dependency error

南笙酒味 提交于 2020-01-25 11:14:04
问题 Message : Autofac.Core.DependencyResolutionException : An exception was thrown while invoking the constructor 'Void .ctor()' on type 'ChromeDriver'. ---> unknown error: unrecognized Blink revision: 2ac50e7249fbd55e6f517a28131605c9fb9fe897 (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.3 x86_64) (See inner exception for details.) ----> System.InvalidOperationException : unknown error: unrecognized Blink revision: 2ac50e7249fbd55e6f517a28131605c9fb9fe897 (Driver info: chromedriver

How do I resolve a WebAPI dependency in Autofac that requires a parameter from the route?

点点圈 提交于 2020-01-25 08:28:25
问题 I am struggling with wiring dependencies through autofac in my WebApi 2 project. I have a following interface and class that i'd like to inject in my GET and POST controller actions, public interface IRepository { IContext Context { get; } void SomeOperation(); } public MyRepository : IRepository { IContext _context; public MyRepository(IContext context) { _context = context; } public Context { get { return _context; } } public void SomeOperation { // Perform some operation using _context; }

Autofac fails to resolve enumerable of typed HttpClients

大兔子大兔子 提交于 2020-01-25 00:31:24
问题 I have a number of services which require usage of typed HttpClient from HttpClientFactory. Though I can resolve one service I can't resolve IEnumerable of this services. interface IMyHttpClient { } class MyHttpClient: IMyHttpClient { public MyHttpClient(HttpClient client) { } } class Program { static void Main(string[] args) { var services = new ServiceCollection(); services.AddHttpClient() .AddHttpClient<IMyHttpClient, MyHttpClient>(); var builder = new ContainerBuilder(); // Exception goes

What is the proper way to create objects which require parameters using autofac?

。_饼干妹妹 提交于 2020-01-23 17:42:05
问题 I think I get most things about dependency inversion and using an IoC container, but one thing still does not appear clear to me. How do I use autofac to automate the following factory: public class WidgetFactory { public static IWidget Create(int foo, double bar) { return new Widget(foo, bar); } } public class Widget { private readonly int foo; private readonly double bar; public Widget(int foo, double bar) { this.foo = foo; this.bar = bar; } } elsewhere... public class FoobarUser { public

Autofac Dependency Injection in Azure Function

荒凉一梦 提交于 2020-01-23 06:20:12
问题 I am trying to implement DI using Autofac IOC in Azure function. I need to build the container, but not sure where to put the code to build the container 回答1: I think for now you would need to do something ugly like: public static string MyAwesomeFunction(string message) { if (MyService == null) { var instantiator = Initialize(); MyService = instantiator.Resolve<IService>(); } return MyService.Hello(message); } private static IService MyService = null; private static IContainer Initialize() {