Code Monkey home page Code Monkey logo

Comments (4)

yuzd avatar yuzd commented on May 20, 2024 1

you can give me a PR

from autofac.annotation.

MassimilianoCuccia avatar MassimilianoCuccia commented on May 20, 2024

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.

yuzd avatar yuzd commented on May 20, 2024

thanks!!

from autofac.annotation.

MassimilianoCuccia avatar MassimilianoCuccia commented on May 20, 2024

sorry, still developing.
This doesn't work into web applications

from autofac.annotation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.