Comments (4)
you can give me a PR
from autofac.annotation.
I resolved, feel free to include this into your library, please cite me
Here is how to access the container from everywhere
var container = ApplicationContext.Instance.getContainer();
Here is the code to automatically build the container
using Autofac;
using Autofac.Annotation;
using Domain.Repository;
using Infrastructure.Data.Repository;
using InfrastructureMongo.Repository;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Context
{
public sealed class ApplicationContext
{
private static IContainer container;
private ApplicationContext()
{
//build();
autodiscovery();
}
private void autodiscovery()
{
var builder = new ContainerBuilder();
List<Assembly> loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray();
var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();
toLoad.ForEach(
path => {
try {
loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path)));
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
);
builder.RegisterModule(new AutofacAnnotationModule(loadedAssemblies.ToArray()));
container = builder.Build();
}
public IContainer getContainer()
{
return container;
}
public static ApplicationContext Instance { get { return Nested.instance; } }
private class Nested
{
static Nested()
{
}
internal static readonly ApplicationContext instance = new ApplicationContext();
}
}
}
bye
from autofac.annotation.
thanks!!
from autofac.annotation.
sorry, still developing.
This doesn't work into web applications
from autofac.annotation.
Related Issues (20)
- 并行注入多个builder.RegisterModule, Autowired标注使用的地方会出现null的情况 HOT 2
- 两个模块循环引用报错 HOT 3
- Value标签的值能从nacos里面获取吗? HOT 3
- 4.0.2升级到4.0.4的报错问题 HOT 2
- .net 4.8无法安装 HOT 2
- Component注解的InitMethod无法是父类方法 HOT 4
- Pointcut排除namespace 正则表达式如何写 HOT 3
- 如何给方法加注释,AOP中获取注释 HOT 5
- IOC注入,可以在构造函数中使用吗 HOT 5
- Autowired(name: )可以是对象属性? HOT 16
- [Pointcut(AttributeType = typeof(LogAttribute))]不起作用 HOT 3
- 子类继承父类,AOP不起作用 HOT 41
- 我想用AspectArround写一个方法自动存取缓存的AOP,当获得缓存值时,如何跳过当前方法执行下一个拦截器。 HOT 10
- 使用AspectArround做缓存时,内存一直增加 HOT 19
- 在asp.net core框架下使用[PropertySource(Dynamic = typeof(IConfigurationProvider))]无效 HOT 18
- Question about internal classes with [Component] annotation HOT 4
- asp.netcore7 razar pages中无法使用 HOT 5
- 请问下拦截器是不是不支持带泛型参数的方法使用? HOT 4
- Value的IgnoreUnresolvablePlaceholders=true未起作用 HOT 3
- [建议] 完善ConditionOnClass的Wiki部分说明 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autofac.annotation.