Autofac | An addictive NET IoC container | Dependency Injection library
kandi X-RAY | Autofac Summary
kandi X-RAY | Autofac Summary
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
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Autofac
Autofac Key Features
Autofac Examples and Code Snippets
Community Discussions
Trending Discussions on Autofac
QUESTION
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:00The 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.
QUESTION
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:47You 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
:
QUESTION
I'm confused with Autofac Examples : WebApiExample.OwinSelfHost, the startup class is following:
...ANSWER
Answered 2021-Nov-24 at 16:41Looks 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. :(
QUESTION
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:57Autofac 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.
QUESTION
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:47azure 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.
QUESTION
We have a series of .NET Core console apps that are installed in a particular directory:
...ANSWER
Answered 2022-Jan-25 at 00:31Are 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.
QUESTION
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:15There'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:
QUESTION
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:49What 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:
QUESTION
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:11The service provider from IHost
can be used to resolve main view model
QUESTION
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:43Each 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
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Autofac
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page