Autofac | An addictive NET IoC container | Dependency Injection library

 by   autofac C# Version: v7.0.1 License: MIT

kandi X-RAY | Autofac Summary

kandi X-RAY | Autofac Summary

Autofac is a C# library typically used in Programming Style, Dependency Injection applications. Autofac has no bugs, it has no vulnerabilities, it has a Permissive License and it has medium support. You can download it from GitHub.

Autofac is an IoC container for Microsoft .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .NET classes as components.
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              Autofac has a medium active ecosystem.
              It has 4179 star(s) with 819 fork(s). There are 230 watchers for this library.
              OutlinedDot
              It had no major release in the last 12 months.
              There are 19 open issues and 1090 have been closed. On average issues are closed in 230 days. There are 1 open pull requests and 0 closed requests.
              It has a neutral sentiment in the developer community.
              The latest version of Autofac is v7.0.1

            kandi-Quality Quality

              Autofac has 0 bugs and 0 code smells.

            kandi-Security Security

              Autofac has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              Autofac code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              Autofac is licensed under the MIT License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              Autofac releases are available to install and integrate.
              Installation instructions, examples and code snippets are available.

            Top functions reviewed by kandi - BETA

            kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
            Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Autofac
            Get all kandi verified functions for this library.

            Autofac Key Features

            No Key Features are available at this moment for Autofac.

            Autofac Examples and Code Snippets

            No Code Snippets are available at this moment for Autofac.

            Community Discussions

            QUESTION

            Audit.NET Adding UserName to AuditLog when logging EF but can't reach HttpContext
            Asked 2022-Mar-25 at 11:27

            Context

            I am trying wireup Audit.Net in an MVC5 app with .Net Framework 4.8.

            Dataprovider: EntityFramework

            Output: Cosmos

            DI Provider: Autofac

            The Problem

            I have tried the following call backs to try and write the username to the AuditEvent.

            ...

            ANSWER

            Answered 2021-Oct-08 at 14:00

            The key (which might help in search terms) is async. A quick search for async httpcontext mvc gets us to this existing question with a lot of information about async/await and HttpContext usage.

            The super short version: in MVC, the HttpContext gets carried around in the thread synchronization context (which is also where, say, the culture and other thread settings are carried around) but it's expensive to cart that context from thread to thread so the default is to not do that. You need to enable it explicitly to work.

            Here's a blog article explaining the various knobs you can turn in web.config to get it to work but the net result is, basically, to make sure is set (well, set that value to 4.5 or higher).

            If that's already set, then... maybe there's something else at play, like a call has the ConfigureAwait(false) set on it so it's not returning to a thread context that has the HttpContext. But, more than likely, that httpRuntime flag should fix it.

            Source https://stackoverflow.com/questions/69489412

            QUESTION

            Can Entity Framework Core bet setup to use a backup connection string?
            Asked 2022-Feb-11 at 13:19

            My company is using 2 Windows servers. 1 Server is running as a backup server and other than SQL replication, the backup server requires manual intervention to get it running as the primary. I have no control over this, but I do have control of the apps/services running on the servers.

            What I have done is I got all the services to be running on both and added Rabbit MQ as a clustered message broker to kind of distribute the work between the servers. This is all working great and when I take a server down, nothing is affected.

            Anyway, to the point of the question, the only issue I see is that the services are using the same SQL server and I have nothing in place to automatically switch server if the primary goes down.

            So my question is, is there a way to get Entity Framework to use an alternative connection string should one fail?

            I am using the module approach with autofac as dependency injection for my services. This is the database registration.

            ...

            ANSWER

            Answered 2021-Aug-02 at 12:47

            You can define on custom retry strategy on implementing the interface IExecutionStrategy. If you want reuse the default SQL Server retry strategy, you can derive from SqlServerRetryingExecutionStrategy on override the method ShouldRetryOn :

            Source https://stackoverflow.com/questions/68620027

            QUESTION

            autofac webapi owin integration problems with middleware exection order not correct
            Asked 2022-Feb-04 at 14:26

            I'm confused with Autofac Examples : WebApiExample.OwinSelfHost, the startup class is following:

            ...

            ANSWER

            Answered 2021-Nov-24 at 16:41

            Looks like you've found a bug! I've filed an issue about it on your behalf. You can read more technical details about it there, but the short version is that over the years we've changed some Autofac internals to support .NET Core and this looks like something we've missed.

            The workaround until this is fixed will be to register the middleware in reverse order, which isn't awesome because once the fix is applied you'll have to reverse them back. :(

            Source https://stackoverflow.com/questions/70081612

            QUESTION

            Registration of ephemeral components in Autofac
            Asked 2022-Feb-01 at 13:57

            I am using Autofac in an application that reads and writes information from a number of "devices". These devices can come and go at any point and their lifetime is outside of the control of the application. They can also be found on different interfaces; USB and IP networks. All devices have a common IDevice interface.

            What would the suggested mechanism be for integrating the discovery of this within Autofac? Since the devices themselves are ephemeral I know that I would need to be able to remove them from the container, I also found in a SO search that this isn't possible (Is it possible to remove an existing registration from Autofac container builder?)

            I am now leaning more to the idea of having a factory registered within Autofac (IDeviceFactory or similar) and then querying that factory for the current device list when needed. That factory would maintain it's own internal list of available hardware interfaces (USB COM ports or ethernet interfaces) and then the list of devices available on those hardware interfaces.

            It just feels like I am potentially losing out on a lot of the benefits of Autofac if I don't use it for the IDevice itself. Should I be investigating modifying the service resolver pipeline?

            I don't want to fall in to the trap of desperately trying to "Autofac all the things!" if it isn't the right tool, but I also don't want to unnecessarily write my own component registry that lives alongside Autofac.

            ...

            ANSWER

            Answered 2022-Feb-01 at 13:57

            Autofac isn't going to be the way here, go with the factory.

            For performance reasons, Autofac needs to be able to cache registrations, one of the many reasons you can't remove things from the container. Hypothetically you could create a lifetime scope with the devices in it and dispose/recreate that every time the device list changes, but that's going to make your app unstable since anything resolved from that scope will also be disposed and recreated.

            It's also possible, it sounds like, that the device list may change as some class logic is executing. You'll want the latest list at the time you need to work with the devices, which means querying at that point - just like you'd query data from a database as you need it rather than inject it as a dependency.

            Just go with the factory.

            Source https://stackoverflow.com/questions/70938388

            QUESTION

            ASP.NET Core BackgroundService runs several times
            Asked 2022-Jan-26 at 17:47

            I currently have an ASP.NET Core application with the .NET Core SDK v5.0.403.

            In this application, I have several BackgroundService defined, one of which being the following:

            ...

            ANSWER

            Answered 2022-Jan-26 at 17:47

            azure app service

            This is almost certainly due to multiple instances of your app running.

            What would be the safest way to have the backgroundservice running only once, beside going through some data persist / check in DB?

            An external "lease" is the safest approach. You can build one yourself using CosmosDb / Redis as a backend, or you can use a built-in approach by factoring your background service out of your web app into either an Azure WebJob or an Azure Function. Both of those services use leases automatically for timer-triggered jobs.

            Source https://stackoverflow.com/questions/70867082

            QUESTION

            .NET Core apps on Linux (Redhat) creating mysterious ".net" directories/files in user home directories
            Asked 2022-Jan-25 at 06:09

            We have a series of .NET Core console apps that are installed in a particular directory:

            ...

            ANSWER

            Answered 2022-Jan-25 at 00:31

            Are the apps published as Single File Applications? If so, the documentation have some pointers.

            Looking at the fact that it's extracting 3rd-party libraries, I'm guessing this may be relevant:

            Previously in .NET Core 3.0, when a user runs your single-file app, .NET Core host first extracts all files to a directory before running the application. .NET 5 improves this experience by directly running the code without the need to extract the files from the app.

            And this explains the location:

            If extraction is used the files are extracted to disk before the app starts:

            • If environment variable DOTNET_BUNDLE_EXTRACT_BASE_DIR is set to a path, the files will be extracted to a directory under that path.
            • Otherwise if running on Linux or MacOS, the files will be extracted to a directory under $HOME/.net.
            • If running on Windows, the files will be extracted to a directory under %TEMP%/.net.

            Source https://stackoverflow.com/questions/70841540

            QUESTION

            Autofac - how to register open generics as named or keyed types for use in a factory or using IIndex<,>
            Asked 2022-Jan-18 at 17:15

            I am using Net Core 5 and had not yet had a need to use anything other than the built in Dependency Injection. I now have a need to be able to resolve a specific service based on the value selected from a dropdown list of services in a UI.

            Out of all the DI containers out there I have decided to delve a bit further into Autofac and think that using either named services and a factory pattern or keyed services will be the way to go but I am running into a bit of difficulty, here is an example;

            I have an Interface that look like this

            ...

            ANSWER

            Answered 2022-Jan-18 at 17:15

            There's a bit to unpack here and, while I can kind of explain why things aren't working the way you want, the short answer is you're probably going to have to redesign how you're doing what you're doing. Which is likely not the answer you want, but... it's the answer.

            To understand why, we need to ignore Autofac entirely for a moment and just look at the types you're working with.

            You have an interface:

            Source https://stackoverflow.com/questions/70732191

            QUESTION

            How to configure dependency injection container with Func?
            Asked 2022-Jan-03 at 15:49

            BusinessAction is used to represent an action that can be performed by a user. Each action is related to the specific entity, so if for example, that entity is Order, business actions could be CancelOrder, IssueRefund, etc.

            ...

            ANSWER

            Answered 2022-Jan-03 at 15:49

            What you're trying to do is possible, but it's not a common thing and isn't something magic you'll get out of the box. You'll have to write code to implement it.

            Before I get to that... from a future perspective, you might get help faster and more eyes on your question if your repro is far more minimal. The whole BusinessAction isn't really needed; the RequestHandler isn't needed... honestly, all you need to repro what you're doing is:

            Source https://stackoverflow.com/questions/70555786

            QUESTION

            Resolve service with Autofac and IHostBuilder
            Asked 2021-Dec-01 at 21:11

            I have a MVVM wpf app in which i'm trying to implement dependency injection with Autofac.

            App.xaml.cs :

            ...

            ANSWER

            Answered 2021-Dec-01 at 21:11

            The service provider from IHost can be used to resolve main view model

            Source https://stackoverflow.com/questions/70190239

            QUESTION

            How to dealing with multy source Database using DbContext
            Asked 2021-Nov-27 at 13:43

            I used clean arch steps to create the project, but the problem that i have more then three aggregate that i need to put them in referents Database.

            I tried to use DbContext for each aggregate like this:

            ...

            ANSWER

            Answered 2021-Nov-27 at 13:43

            Each DbContext can config their own entity

            Let assume we have 10 entities for 3 DbContext, we can separate them out 2 entities for ADbContext, 5 for BDbContext and 3 for CDbContext. Manage the configuration like FK, between them wisely, otherwise, that would turn to be chaos. Here is how to doing this, using fluent API

            Source https://stackoverflow.com/questions/70108413

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install Autofac

            Our Getting Started tutorial walks you through integrating Autofac with a simple application and gives you some starting points for learning more. Register components with a ContainerBuilder and then build the component container.

            Support

            Refer to the Contributor Guide for setting up and building Autofac source. You can also open this repository right now in VS Code.
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            CLONE
          • HTTPS

            https://github.com/autofac/Autofac.git

          • CLI

            gh repo clone autofac/Autofac

          • sshUrl

            git@github.com:autofac/Autofac.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link