autofac

Override Autofac registration - Integration tests with DI

夙愿已清 提交于 2019-12-30 09:33:28
问题 I write integration tests for my application, and use my container for this. I want to be able to register all the components as I do in real running, and then override some of the components and switch them to use stubs implementations. I wouldn't want to seperate the DI and have a container for tests only because I want to test the real thing. Doing this also seems ugly: public class MyRegistrations { public static RegisterAll(bool isInTest= false) { if (isTest) { // Register test fakes }

Solving Autofac issue Inheritance security rules violated while overriding member GetService

纵然是瞬间 提交于 2019-12-30 07:51:51
问题 I've got an ASP.NET MVC application using Autofac. I've added the appropriate packages via: Install-Package Autofac Install-Package Autofac.Mvc4 When I ran the web application, this error was throw: Inheritance security rules violated while overriding member: 'Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(System.Type)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden. How can this be solved? 回答1: I had

How to make AutoFac use same instance of nested dependency per top-level object? (SignalR dependency injection per hub)

 ̄綄美尐妖づ 提交于 2019-12-30 07:34:08
问题 I am trying to set up my AutoFac registration in such a way that this test passes: [Test] public void Autofac_registration_test() { // Given var builder = new ContainerBuilder(); RegisterServices(builder); var container = builder.Build(); // When var firstHub = container.Resolve<Hub>(); var secondHub = container.Resolve<Hub>(); // Then firstHub.Should().NotBe(secondHub); firstHub.FooRepo.Context.Should().Be(firstHub.BarRepo.Context); firstHub.FooRepo.Context.Should().NotBe(secondHub.FooRepo

DependencyResolver.SetResolver Not working

ⅰ亾dé卋堺 提交于 2019-12-30 04:40:27
问题 I'm trying to set the container on a new app using the Dependency.SetResolver method and using autofac with autofac mvc 5 integration. The problem is that setting the resolver doesn't appear to do anything. The default resolver will always be used and will always expect a default constructor. Any ideas? Edit - The global.asax.cs , I've simplified it down to: var b = new ContainerBuilder(); b.RegisterType<UserInfo>().As<IUserSession>().InstancePerHttpRequest(); var container = b.Build();

Getting autofac to work with mvc6 beta5

拜拜、爱过 提交于 2019-12-30 04:34:09
问题 I am trying to get autofac working with an mvc6 application I am working on. I found this blog article however it seems to be a little dated. It looks like its using the beta3 bits I am using this clr version 1.0.0-beta5-11911 My project has these 2 references "Autofac": "4.0.0-alpha2", "Autofac.Dnx": "4.0.0-alpha2", Within the article is talks about how to modify the startup.cs // Create the Autofac container builder. var builder = new Autofac.ContainerBuilder(); // Add any Autofac modules

Resolving Autofac components in background Tasks in ASP.NET

99封情书 提交于 2019-12-29 08:50:09
问题 Using Autofac in ASP.NET along with the ContainerDisposalModule, how can i support fire and forget calls that have component dependencies that need to be resolved? The problem I'm running into is that the ASP.NET request completes and disposes the lifetime scope of the request before the Task is ran, so any components that need to be resolved in the new thread fail with the message "Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already

net core WebApi——依赖注入Autofac

萝らか妹 提交于 2019-12-28 17:51:01
目录 前言 Autofac 添加一个Util来随时调用 小结 代码地址 预告 前言 周末加班,下午犯困,整理下之前鼓捣过的东西,看过我之前的webapi系列的读者知道,我之前试过Aspect,但是升级到3.0之后没往下去试了,当时还留了个坑,这不,现在果断移除了换成这个了。 Autofac 这个第三方类库呢,是Ioc的容器,可以简化我们很大的工作量,比如说在之前我们需要写个类去声明接口与实现,而用了这个容器呢,就不需要了,当然还是需要些配置的。 首先,引入第三方类库,不多说。 然后开始配置吧,首先先来看Program,添加 AutofacServiceProviderFactory 。 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .UseServiceProviderFactory(new AutofacServiceProviderFactory()); 改完之后呢,我们果断就来到了Startup,只要是配置,当然要看入口文件跟这个配置文件了。 3

net core WebApi——依赖注入Autofac

自作多情 提交于 2019-12-28 16:12:25
原文: net core WebApi——依赖注入Autofac 目录 前言 Autofac 添加一个Util来随时调用 小结 代码地址 预告 前言 周末加班,下午犯困,整理下之前鼓捣过的东西,看过我之前的webapi系列的读者知道,我之前试过Aspect,但是升级到3.0之后没往下去试了,当时还留了个坑,这不,现在果断移除了换成这个了。 Autofac 这个第三方类库呢,是Ioc的容器,可以简化我们很大的工作量,比如说在之前我们需要写个类去声明接口与实现,而用了这个容器呢,就不需要了,当然还是需要些配置的。 首先,引入第三方类库,不多说。 然后开始配置吧,首先先来看Program,添加 AutofacServiceProviderFactory 。 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .UseServiceProviderFactory(new AutofacServiceProviderFactory()); 改完之后呢,我们果断就来到了Startup,只要是配置

Dependency Injection in HtmlHelper extension method?

人盡茶涼 提交于 2019-12-28 06:28:10
问题 I want to implement property renderers as handlers. I am using Autofac as a DI container in the app. How can I get objects implementing IPropertyHandler in HtmlHelper extension without using globally accessible container (service location)? Is it a way to register own HtmlHelper in Autofac? Maybe MVC framework provide another way? public static class HtmlHelperExtensions { public static MvcHtmlString Editor(this HtmlHelper html, object model) { return new Renderer(new List<IPropertyHandler>()

【netcore入门】在Windows IIS上部署.NET Core 2.1项目

北战南征 提交于 2019-12-27 07:30:48
部署之前先检查下面2个先决条件是否满足 1、安装了 IIS 模块 win7 在 控制面板→程序和功能→打开或关闭Windows功能→勾选Internet 信息服务(Internet Information Services)安装IIS功能 win server 控制面板→程序和功能→打开或关闭Windows功能→添加角色和功能→下一步选择web服务器(IIS)→勾选安装即可 2、安装了 AspNetCoreModule 托管模块 检查 Internet 信息服务(IIS)管理器 服务器的模块里面有没有 托管模块 如果没有点击下面的链接下载安装 这个是netcore2.1版本 https://pan.baidu.com/s/1AUWntFGHlFYqaWa7aNi9YA 安装完之后最好重启一下系统 3、前两个条件都满足之后,就可以部署IIS站点了,跟平时部署站点的唯一区别就是 应用程序池,选择【无托管代码】,其他基本和 .net 一模一样 解决方案的发布就不再描述了 按照上面两步操作之后依然不能正常访问的话 可以配置下 web.config 里的日志输出目录,跟踪日志查找原因 这里有一个坑需要注意一下,就是日志输出的目录默认是 logs 文件夹下面的,但是它又不会自动创建这个logs目录,简直就是sb操作 所以修改下目录为程序当前目录(.\)即可,然后将目录开关