autofac

.net Core 2.*使用autofac注入

谁说我不能喝 提交于 2019-12-27 01:29:07
创建项目 1.创建一个.net core 项目 2.创建一个类库 2.1创建interface文件夹 2.2创建Service文件夹 好了给大家看项目目录 对的。我创建了一个IUserService和一个UserService 然后给大家贴一下代码 using System; using System.Collections.Generic; using System.Text; namespace AutofaceTest.Service.Interface { public interface IUserService { string GetUserName(); } } using AutofaceTest.Service.Interface; using System; using System.Collections.Generic; using System.Text; namespace AutofaceTest.Service.Service { public class UserService : IUserService { public string GetUserName() { return "恩很~是我"; } } } 添加引用 需要通过nuget添加引用 需要的引用如下 1.Autofac 2.Autofac.Configuration 3

Self-hosted WCF: automatically create service hosts and enable dependency injection with Autofac

十年热恋 提交于 2019-12-25 05:32:41
问题 I'm working on a Windows Service with a number of self-hosted WCF services. I'm using Autofac for DI/IoC. WCF services and endpoints are set up in app.config, and by enumerating the configured services, the Windows service is able to automatically create and open a ServiceHost for each configured WCF service. To enable dependency injection, I add a call to the AddDependencyInjectionBehavior (docs) method for each new instance of ServiceHost , but the method specifically requests a

How to build a graph of resolved instances with Autofac?

家住魔仙堡 提交于 2019-12-25 03:27:19
问题 After all registrations, I am doing ContainerBuilder.RegisterCallback and subscribing to all IComponentRegistration.Preparing and IComponentRegistration.Activating events to be able to handle all activations. With this two events I am able to build a tree, the order of events looks like this: Preparing: Root Preparing: FirstLevel_A Activating: FirstLevel_A Preparing: FirstLevel_B Preparing: SecondLevel_C Activating: SecondLevel_C Activating: FirstLevel_B Activating: Root But what if some

AutoFac MVC Web API

情到浓时终转凉″ 提交于 2019-12-25 03:12:57
问题 I have a simple AutoFac example working but now want to apply this to my web api project AND have the correct separation between the layers. The issue I am seeing is the standard controller x "does not have a default constructor" but i am stumped so asking for advice.. I am calling RegisterApiControllers as well as RegisterControllers.. this is what I have in my DependencyInjectionContainer public static class DependencyInjectionContainer { public static ContainerBuilder Builder; public

Inject into asp.net web api model with autofac

蹲街弑〆低调 提交于 2019-12-25 03:00:13
问题 I want to use IValidatableObject.Validate() to check some aspects of my model against a repository before processing requests. However, with the config below _dalForValidation is never set in on Models.App , in other words the default empty constructor is always being called. private static void ConfigureAutofac() { var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<DataAccessFacade>().As<IDataAccess>()

Could not load type Castle.Proxies.IReadinessProxy when running xUnit integration tests in parallel with Autofac

和自甴很熟 提交于 2019-12-25 01:18:28
问题 I am having an issue that I've been many days unable to resolve. I use xUnit with a given-then-when abstraction to make tests more readable. I am using a wrapper over EventStore and running some integration tests. They all go well.. except one that fails when running all in parallel (and xUnit runs in parallel), but if I run them all sequentially they all succeed. I cannot understand why this would be an issue because every single fact is supposed to run the constructor (the given) and the

Autofac list of types that implements interface / C# Asp.net

眉间皱痕 提交于 2019-12-24 23:52:35
问题 I had the problem with Autofac and memory leak in NServiceBus Scheduler. But fortunately I fixed that. Autofac and memory leak with BeginLifetimeScope / DbContext has been disposed / C# asp.net But now I'm trying to refactor this part slightly. My code: public void Start() { List<Type> jobTypes = new List<Type> { typeof(ExpiryDateTask) }; foreach (var jobType in jobTypes) { _schedule.Every(TimeSpan.FromSeconds(30), () => { using (var scope = _lifetimeScope.BeginLifetimeScope()) { var job =

Autofac - How to register a class<,> to interface<>

这一生的挚爱 提交于 2019-12-24 21:28:46
问题 I'm trying to register my repository class which is take two generic parameter to repository interface (one parameter generic). public class RepositoryBase<T, O> : IDataAccess<T> public interface IDataAccess<T> 回答1: Autofac don't know how to set the second generic parameter of RepositoryBase when you only want to provide one generic argument in the interface. So you need to somehow specify the second argument in order to instantiate the service unless you are happy with object , dynamic or

Register callback in Autofac and build container again in the callback

做~自己de王妃 提交于 2019-12-24 19:28:39
问题 I have a dotnet core application. My Startup.cs registers types/implementations in Autofac. One of my registrations needs previous access to a service. var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterSettingsReaders(); // this makes available a ISettingsReader<string> that I can use to read my appsettings.json containerBuilder.RegisterMyInfrastructureService(options => { options.Username = "foo" //this should come from appsettings }); containerBuilder.Populate(services

How do you add a runtime string parameter into a dependency resolution chain?

泄露秘密 提交于 2019-12-24 16:20:35
问题 How could I setup my chosen DI for this kind of setup: public abstract class BaseRepo { public BaseRepo(string token) { } } public RepoA : BaseRepo, IRepoA { // implementation of interface here } public ViewModelA { IRepoA _repo; public ViewModelA(IRepoA repo) { this._repo = repo; } public DoMethod() { this._repo.DoSomeStuff(); } } In real scenario, the token parameter on the base class is resolved after the user has been logged in. I was thinking of just configuring the interfaces for DI