autofac

Register Generic Type with Autofac

给你一囗甜甜゛ 提交于 2019-11-28 20:18:04
I have UnitofWork class and it implement IUnitOfWork. I try to register that with autofac : var builder = new ContainerBuilder(); builder .RegisterGeneric(typeof(UnitOfWork<Repository<>,>)) .As(typeof(IUnitOfWork)) .InstancePerDependency(); Implementation is: public class UnitOfWork<T, O> : IUnitOfWork where T : Repository<O> where O : BaseEntity { } public interface IUnitOfWork : IDisposable { void SaveChanges(); } Gives an error "Type expected" but this one work on another project: public class Repository<T> : GenericRepository<T> where T : BaseEntity { public Repository(IDbContext context)

SignalR + Autofac + OWIN: Why doesn't GlobalHost.ConnectionManager.GetHubContext work?

痞子三分冷 提交于 2019-11-28 19:12:05
I'm trying to use OWIN, SignalR and Autofac in a single project. I'm setting things up with regards to signalR as follows: // Create the AutoFac container builder: var builder = new ContainerBuilder(); // ...[Register various other things in here]... // register signalR Hubs builder.RegisterHubs(Assembly.GetExecutingAssembly()); // Build the container: var container = builder.Build(); // Configure SignalR with the dependency resolver. app.MapSignalR(new HubConfiguration { Resolver = new AutofacDependencyResolver(container) }); My issue is that when I use the Autofac SignalR integration, I can

Register global filters in ASP.Net MVC 4 and Autofac

做~自己de王妃 提交于 2019-11-28 18:38:19
I have a filter like this one: public class CustomFilterAttribute : ActionFilterAttribute, IAuthorizationFilter { public MyPropery Property { get; set; } .... } I need it to be run for every actions in my project I tried to register it in the GlobalFilters but my property doesn't get injected I tried This solution to register my filter but it doesn't get called Any idea on how to do that? Pete There's a new way of registering MVC global filters in AutoFac. First, remove the filter registration from your RegisterGlobalFilters because we will have Autofac handle adding them to our controllers

.NET 4, AllowPartiallyTrustedCallers attribute, and security markings like SecurityCritical

僤鯓⒐⒋嵵緔 提交于 2019-11-28 18:15:30
I'm new C# and am trying to understand the new security features of .NET-4 . To fill in some details, I'm currently trying to update AutofacContrib.Moq to work with the latest Moq. I had no problems doing this for .NET-3.5 and under. But in .NET-4 the security restrictions result in numerous security exceptions. Moq has a a single method, GetObjectData , that's marked with the SecurityCritical attribute. AutofacContrib.Moq has the AllowPartiallyTrustedCallers attribute set which is the source of the exceptions. It seems that rather than adding the SecurityRules attribute with a SecurityLevel

Multiple implementations for one interface with DI

﹥>﹥吖頭↗ 提交于 2019-11-28 17:33:55
Right now I'm trying to teach myself the Dependency Injection pattern with the IOC-container from Autofac. I've come up with a very simple example, which is presented below. Although the example is simple, I fail to get it working properly. Here are my classes/interfaces: Two monsters, both implementing the IMonster interface: interface IMonster { void IntroduceYourself(); } class Vampire : IMonster { public delegate Vampire Factory(int age); int mAge; public Vampire(int age) { mAge = age; } public void IntroduceYourself() { Console.WriteLine("Hi, I'm a " + mAge + " years old vampire!"); } }

Adding services after container has been built

不羁岁月 提交于 2019-11-28 16:55:08
问题 Is it possible to register a service at run-time, meaning after the ContainerBuilder has been built and the Container has been created (and ContainerBuilder disposed of)? 回答1: Yes you can, using the Update method on ContainerBuilder : var newBuilder = new ContainerBuilder(); newBuilder.Register...; newBuilder.Update(existingContainer); 回答2: Since ContainerBuilder.Update has been deprecated, the new recommendation is to use child lifetime scope. Adding Registrations to a Lifetime Scope Autofac

'Autofac Circular Component Dependency Detected' Error

一曲冷凌霜 提交于 2019-11-28 12:38:43
I am new to IoC and am using Autofac in my current project. I have the following 2 classes: public class UserService : IUserService { private readonly IUserRepository _repo; private readonly IMailService _mailService; public UserService(IUserRepository repo, IMailService mailService) { _repo = repo; _mailService = mailService; } } public class MailService : IMailService { private readonly IMailRepository _repo; private readonly IUserService _userService; public MailService(IMailRepository repo, IUserService userService) { _repo = repo; _userService = userService; } } Initially, my UserService

Using Autofac to inject a dependency into the Main entry point in a console app

空扰寡人 提交于 2019-11-28 10:19:48
Say I have a simple console application: public static class Program { private static ILog Log { get; set; } public static void Main() { Log.Write("Hello, world!"); } } What is the simplest way I can use Autofac to inject the Log instance and ensure that the Log property will not be null at run time? The issue is that I can't pass it in through Main() and I'm trying to avoid service location using the container (just because). What you should do is extract all logic from your main into a class. This class can have a constructor with dependencies. You resolve this class in the main and call it.

Get same instance of a component registered with Autofac as InstancePerLifetimeScope in Global.asax methods as is injected into a controllers?

这一生的挚爱 提交于 2019-11-28 09:41:21
问题 I have a situation where I need to manually instantiate some objects in Application_BeginRequest that are dependent on some of the same components that I've registered with Autofac. I'd like to use the same instances of components that I've registered with Autofac with InstancePerLifetimeScope for injection into my MVC and WebAPI controllers. My config for both MVC and Web API works as expected, and an example of a component registration looks like so: builder.Register(c => new MyDbContext())

How to configure Autofac and SignalR in a MVC 5 application

和自甴很熟 提交于 2019-11-28 09:13:57
问题 I am trying to configure an MVC 5 application to use SignalR 2.2.x and inject a service into my NotificationsHub . We are using Autofac for MVC but I am not sure on how to correctly configure this. Autofac documentation exists for NuGet Autofac.Integration.SignalR (3.0.2) and Autofac.Integration.Mvc (3.3.4) . What I am doing so far is register the hubs via: ContainerBuilder builder = new ContainerBuilder(); // Register MVC controllers. builder.RegisterControllers(typeof(MvcApplication)