autofac

Using autofac with moq

血红的双手。 提交于 2019-12-09 08:44:17
问题 I need to register my Autofac container with specific interface, for this case I want to resolved mock. How can I do it? I've tried: var AppContainer = ApplicationContainer.GetApplicationContainer(); var cb = new ContainerBuilder(); cb.RegisterType<Mock<IStudyLoader>>().As<IStudyLoader>().SingleInstance(); cb.Update(AppContainer); I don't want to change my code to resolve something else than IStudyLoader , but Mock<IStudyLoader> is not substitutable for IStudyLoader ; e.g Mock<IStudyLoader>

How to use Autofac to resolve instance per request dependencies for types in a child lifetime scope created by Nancy

假如想象 提交于 2019-12-09 06:53:29
问题 We have several applications hosted in Windows services that self host a Nancy endpoint in order to expose instrumentation about the operation of the applications. We use Autofac as our IOC. Several repositories are registered into the root container in a core DLL shared by all applications; this container is then passed to Nancy as its container using a bootstrapper derived from the Nancy.Autofac.Bootstrapper . What we found was that when a web request is received by Nancy it resolves a

Is it possible to get container type in AutoFac

不羁岁月 提交于 2019-12-09 06:07:20
问题 For example, I have registered class C1 with one parameter in constructor of type System.Type . I have another class (C2) with injected parameter of type C1. And I want receive typeof(C2) automatically in C1 constructor. Is it possible in some way? Example code: public class C1 { public C1(Type type) {} // ... } public class C2 { public C2(C1 c1) {} // ... } // Registration containerBuilder.Register(???); containerBuilder.Register<C2>(); 回答1: This should do it: builder.RegisterType<C1>();

Register RavenDb using Autofac?

与世无争的帅哥 提交于 2019-12-09 04:25:46
问题 Can anyone guide me on how I could register RavenDB using Autofac? builder.Register<DocumentStore>( .. what after that? 回答1: Here is a sample console program that illustrates not only how to wire up the document store, but also how to set it up so you can just inject your document session: using System.Threading.Tasks; using Autofac; using Raven.Client; using Raven.Client.Document; namespace ConsoleApplication1 { internal class Program { private static void Main() { var builder = new

How to determine which constructor Autofac uses when resolving

梦想的初衷 提交于 2019-12-09 03:08:29
I'm using a custom JsonConverter and JsonSerializerSettings.TypeNameHandling = TypeNameHandling.Objects to create the required instances during deserialization. The instances are created by resolving the types from an Autofac IOC container. Everything works fine, except... I have several "core objects" that request a unique Id in the constructor from a service (which is correctly injected into the constructor). When deserializing this should not happen because it is fairly expensive and the Ids will be populated from the Json file anyway once the instance has been created. Currently, when

Multiple navigation control through dependency injection

怎甘沉沦 提交于 2019-12-08 20:57:37
My base content class. I used this class as a theme for my project. I do not know this info relevant or not. In here I create an abstract method that would overload the navigation method. public abstract class BaseContentPage : ContentPage { public readonly BaseViewModel BaseViewModel; public BaseContentPage(BaseViewModel baseViewModel) { BaseViewModel = baseViewModel; } public abstract void Navigate(SelectedItemChangedEventArgs e); } In my locator where I build the dependency Injection public class Locator. in this class mainly focus on adding this class to the container to make the all code

Understanding Autofac lifetime scopes

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 20:06:41
问题 From the documentation of Autofac, I understand that it keeps a reference to every IDisposable implementor that it creates. Therefore it can lead to OutOfMemoryException. So the suggested way to resolve dependencies is by using a ILifetimeScope. Assume IService implements IDisposable. class MaintenanceTask { private IService service; public MaintenanceTask(ILifetimeScope lifetimeScope) { service = lifetimeScope.Resolve<IService>(); } //...do your work } But the problem with this approach is

Moq with Autofac Func<Owned<IClass>>

◇◆丶佛笑我妖孽 提交于 2019-12-08 18:50:00
问题 I'm trying to swtich some code I have to use Owned Instances instead of passing around the container. public class SyncManager { private Func<Owned<ISynchProcessor>> _syncProcessor = null; public SyncManager(Func<Owned<ISynchProcessor>> syncProcessor) { _syncProcessor = syncProcessor; } private void Handle() { using (var service = _syncProcessor()) { service.Value.Process(); } } } Now I want to Moq the Func< Owned> and inject it but the calls to _syncProcessor() don't resolve anything So I

How to handle constructor exception when using Autofac WcfIntegration

本秂侑毒 提交于 2019-12-08 18:41:51
问题 Is there a way to handle an exception thrown by the constructor of a WCF service, when that constructor takes in a dependency, and it is the instantiation of the dependency by the IoC container (AutoFac in this case) that causes the exception ? Consider a WCF service with the following constructor: public InformationService(IInformationRetriever informationRetriever) { _informationRetriever = informationRetriever; } //... the service later makes use of the injected InformationRetriever The

Autofac. How to use custom method(property) to resolve some interface?

。_饼干妹妹 提交于 2019-12-08 18:15:41
问题 I have the following interfaces : public interface IConfigurationProvider<TSettings> where TSettings : ISettings, new() { TSettings Settings { get; } } public interface ISettings { } I have the following implementation of IConfigurationProvider: public class ConfigurationProvider<TSettings> : IConfigurationProvider<TSettings> where TSettings : ISettings, new() { public ConfigurationProvider() { this.BuildConfiguration(); } public TSettings Settings { get; private set; } private void