private static void RegisterProfiler(ContainerBuilder builder) { if (ConfigurationHelpers.ShouldBeProfiling()) { builder.Register((c, p) => new TextReporter(p.TypedAs <Func <Stream> >())) .As <TextReporter>() .As <ITransformReports>(); builder.Register(c => new TimingStorage()) .OnRelease( storage => { // Write all the profiling results out to disk. Do this the ugly way // because we don't know if any of the other items in the container have // been removed yet. Func <Stream> factory = () => new FileStream( Path.Combine(FileConstants.LogPath(), DefaultProfilerFileName), FileMode.Append, FileAccess.Write, FileShare.Read); var reporter = new TextReporter(factory); reporter.Transform(storage.FromStartTillEnd()); }) .As <IStoreIntervals>() .As <IGenerateTimingReports>() .SingleInstance(); builder.Register(c => new Profiler( c.Resolve <IStoreIntervals>())) .SingleInstance(); } }
/// <summary> /// Initializes a new instance of the <see cref="ExplorerUserInterfaceModule"/> class. /// </summary> /// <param name="container">The IOC container that will hold the references.</param> /// <param name="resetEvent">The reset event that is used to signal the application that it is safe to shut down.</param> public ExplorerUserInterfaceModule(IContainer container, AutoResetEvent resetEvent) { { Debug.Assert(container != null, "The container should exist."); Debug.Assert(resetEvent != null, "The reset event should exist."); } m_Container = container; m_ResetEvent = resetEvent; m_IsProfiling = ConfigurationHelpers.ShouldBeProfiling(); m_ShowWelcomePage = Settings.Default.ShowWelcomePageOnStartup; }
/// <summary> /// Override to add registrations to the container. /// </summary> /// <param name="builder">The builder through which components can be registered.</param> /// <remarks> /// Note that the ContainerBuilder parameter is not the same one /// that the module is being registered by (i.e. it can have its own defaults). /// </remarks> protected override void Load(ContainerBuilder builder) { base.Load(builder); // Register the global application objects { builder.Register(c => new DependencyInjectionProxy( c.Resolve <IContainer>())) .As <IDependencyInjectionProxy>(); builder.Register(c => new FeedbackReportCollector( c.Resolve <IFileSystem>())) .As <ICollectFeedbackReports>(); builder.Register(c => new FeedbackReportTransmitter( c.Resolve <IReportSender>())) .As <ISendFeedbackReports>(); builder.Register(c => new TimingReportCollection()) .SingleInstance(); builder.Register( c => { Func <string, IDisposable> result = description => null; if (ConfigurationHelpers.ShouldBeProfiling()) { // Autofac 2.4.5 forces the 'c' variable to disappear. See here: // http://stackoverflow.com/questions/5383888/autofac-registration-issue-in-release-v2-4-5-724 var ctx = c.Resolve <IComponentContext>(); Func <TimingReport, string> reportBuilder = report => { var stream = new MemoryStream(); Func <Stream> streamFactory = () => stream; var transformer = ctx.Resolve <TextReporter>( new Autofac.Core.Parameter[] { new TypedParameter(typeof(Func <Stream>), streamFactory) }); transformer.Transform(report); // normally you can't touch the stream anymore, but it turns out you // can touch the actual buffer var buffer = stream.GetBuffer(); var outputString = Encoding.Unicode.GetString(buffer, 0, buffer.Length); // Note that the result may have terminating characters, multiple of them // because we don't know the amount of data written to the buffer. return(outputString.Replace("\0", string.Empty)); }; result = description => new TimingIntervalHelper( ctx.Resolve <SystemDiagnostics>(), ctx.Resolve <IGenerateTimingReports>(), ctx.Resolve <TimingReportCollection>(), reportBuilder, description); } return(result); }) .SingleInstance(); builder.Register(c => new FileSystem()) .As <IFileSystem>(); } }