autofac

How to register two WCF service contracts with autofac

扶醉桌前 提交于 2019-12-04 02:23:05
I have a WCF service that implements two service contracts... public class MyService : IService1, IService2 and I am self-hosting the service... host = new ServiceHost(typeof(MyService)); Everything was working fine when the service implemented only one service contract, but when I attempt to set up autofac to register both like this: host.AddDependencyInjectionBehavior<IService1>(_container); host.AddDependencyInjectionBehavior<IService2>(_container); ... it throws an exception on the second one, reporting: The value could not be added to the collection, as the collection already contains an

A .NET Unit Test without a parameterless constructor, to facilitate dependency injection

一曲冷凌霜 提交于 2019-12-04 00:52:14
I'm trying to have the unit tests not rely on calling container.Resolve<T>() for their dependencies. I'm currently using AutoFac 2.2.4, and tried xUnit.NET and NUnit , but both have this issue : No parameterless constructor defined for this object How do I get past this issue? Is it a particular unit testing framework that will support this, or just how said framework is configured? Should I not be doing this? Or can I set up the test class to work with the constructor that has it's only dependency? Here's some of the code: public class ProductTests : BaseTest { readonly private

Autofac MultiTenant - how do I route to a subdomain?

不打扰是莪最后的温柔 提交于 2019-12-03 21:45:14
n00b here. Re-asking question because I didn't tag it right. I am trying to utilize Autofac's MutliTenant feature. I got an example "working" from the source files. I've scanned the docs and am having trouble figuring out how to "route" the tenants. Currently, I'd like to utilize a single code base for a basic CRUD app. The CRUD app will be used by several different sites, just focused on specific services for the individual site. I'm wanting to do this eventually: codebase.website1.com (Tenant 1) codebase.website2.com (Tenant 2) codebase.website3.com (Tenant 3) Any thoughts or references?

How do I make sure that there is one NHibernate ISession per request using Autofac?

邮差的信 提交于 2019-12-03 20:08:35
I have the following code in an Autofac Module that is used in my Application_Start method: builder.Register(c => new Configuration().Configure().BuildSessionFactory()) .SingletonScoped(); builder.Register(c => c.Resolve<ISessionFactory>().OpenSession()) .HttpRequestScoped(); builder.Register<NHibernateSomethingRepository>().As<ISomethingRepository>(); The constructor for the repository takes an ISession as argument. But I end up with one session for the whole application, even though I explicitly asked for it to be HttpRequestScoped. I have configured the ContainerDisposal HTTP module.

Autofac - how to resolve Func for ISomething from Singleton where ISomething is InstancePerHttpRequest

这一生的挚爱 提交于 2019-12-03 18:55:13
问题 I'm trying to use Autofac to inject dependencies into FluentValidation in an MVC 4 app. I think I've got the strategy worked out, but I'm getting stuck with resolving my per-request ISomething from a singleton. Here's the scenario: I've got a validator that derives from FluentValidation's AbstractValidator. I've read that FluentValidation validators perform best as singletons, so my constructor expects a Func and stores that Factory for use later. When the validator is used, it should ask the

Autofac and ASP.NET Web API ApiController

走远了吗. 提交于 2019-12-03 17:48:06
问题 I have been using autofac with MVC 3 for a while and love it. I recently upgraded a project to MVC 4 and everything seems to be working except for Web Api ApiControllers. I am getting the following exception. An error occurred when trying to create a controller of type 'MyNamespace.Foo.CustomApiController'. Make sure that the controller has a parameterless public constructor. This seems to me to be an issue with DI via autofac. Am I missing something or is there something in the works. I know

MVC 4 Autofac and Generic Repository pattern

我的未来我决定 提交于 2019-12-03 17:15:00
问题 I am utilizing the Unit Of Work and Generic Repository pattern in my MVC 4 app. The problem I am trying to solve is creating Repository stubs for every entity in my system. In order to utilize the Autofac Ioc I am having to create a repository class and interface for every entity so that I can register it in Autofac. app start... builder.RegisterType<SchoolDetailRepository>().As<ISchoolDetailRepository>().InstancePerHttpRequest(); Repository class public class SchoolDetailRepository :

Could not load file or assembly Autofac, Version=3.3.0.0

北城以北 提交于 2019-12-03 17:13:22
问题 After upgrading my project from Autofac 2.6.3.862 to 3.4.0.0, I had the following error. I even didn't add any reference to Autofac 3.3.0.0 in any project in solution. === Pre-bind state information === LOG: DisplayName = Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da (Fully-specified) LOG: Appbase = file:///C:/Projects/Drive/temp/drive/Src/Web/ LOG: Initial PrivatePath = C:\Projects\Drive\temp\drive\Src\Web\bin Calling assembly : Autofac.Configuration, Version=3.3

How do I create a Quartz.NET’s job requiring injection with autofac

邮差的信 提交于 2019-12-03 17:03:08
问题 I am trying to get Quartz.net (2.1.2) to work with an IoC container (autofac), as I have services I need to use in the scheduled jobs. I have found similar posts on the subject, but I can't seem to find one with a specific registration example for autofac. The following post deals with the same issue I am having: How to schedule task using Quartz.net 2.0? However, the part I believe I am missing is when the answer says "And don't forget to register the job in the IoC container". I am unsure

Massive controller constructor argument list when using DI in MVC

橙三吉。 提交于 2019-12-03 16:45:08
I am working on ASP.NET MVC3 solution that uses dependency injection with autofac. Our controllers are being created by autofac and properly and all required objects are being properly passed in. Those objects usually include services, repositories, and mappers converting domain object to MVC (view) models. So the controller constructor looks somewhat like: public abcController( ILogger logger, IabcRepository abcRepository, IabcService abcService, IMapper<AbcDomain, AbcViewModel> abcMapper, ... ) Unfortunately, in time, those constructor parameter lists tend to grow quite quickly. Some of our