Beispiel #1
0
        /// <summary>
        /// Load the composite http service list.
        /// </summary>
        /// <param name="create">Create the composition collection or release.</param>
        public void Compose(bool create = true)
        {
            if (create)
            {
                // Read from the configuration file
                // all the directories that contain
                // any composite services.
                Nequeo.Composite.Configuration.Reader reader = new Nequeo.Composite.Configuration.Reader();
                string[] paths = reader.GetServicePaths();

                AggregateCatalog catalog = new AggregateCatalog();

                // For each directory found search for composite server assemplies
                // and add the reference of the assemblies into the handler collection
                foreach (string path in paths)
                {
                    catalog.Catalogs.Add(new DirectoryCatalog(path, "*HttpService.dll"));
                }

                // Add the collection catalog to the composite container
                _container = new CompositionContainer(catalog);
                _container.ComposeParts(this);
            }
            else
            {
                // Release the exported composite services.
                _container.ReleaseExports <IHttpServerContext, IContentMetadata>(HttpServerContext);
                _container.Dispose();

                // Release the handle.
                HttpServerContext = null;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Removes a collection of exports from composition and releases their resources if possible.
        /// </summary>
        public void Release()
        {
            try
            {
                // Release the exported composite services.
                _container.ReleaseExports <ICT, IMT>(CompositionContext);

                // Release the handle.
                CompositionContext = null;
            }
            catch { }
        }
Beispiel #3
0
 public void Dispose()
 {
     if (!_stopped)
     {
         _stopped = true;
         _service?.RemoveListener(this);
         _tcpListener?.Stop();
         if (null != _mefContainer)
         {
             if (null != _authenticators)
             {
                 _mefContainer.ReleaseExports(_authenticators);
             }
             _mefContainer.Dispose();
         }
         _authenticators = null;
     }
 }
Beispiel #4
0
        public TDSListener(TDSProxyService service, Configuration.ListenerElement configuration)
        {
            var insideAddresses = Dns.GetHostAddresses(configuration.ForwardToHost);

            if (0 == insideAddresses.Length)
            {
                log.ErrorFormat("Unable to resolve forwardToHost=\"{0}\" for listener {1}", configuration.ForwardToHost, configuration.Name);
                _stopped = true;
                return;
            }
            ForwardTo = new IPEndPoint(insideAddresses.First(), configuration.ForwardToPort);

            _service = service;

            var bindToEP = new IPEndPoint(configuration.BindToAddress ?? IPAddress.Any, configuration.ListenOnPort);

            try
            {
                var catalog = new AssemblyCatalog(configuration.AuthenticatorDll);
                _mefContainer = new CompositionContainer(catalog);
                var exports = _mefContainer.GetExports <IAuthenticator>().ToList();
                var export  = exports.FirstOrDefault(a => a.Value.GetType().FullName == configuration.AuthenticatorClass);
                if (null == export)
                {
                    log.ErrorFormat(
                        "Found dll {0} but not authenticator implementation {1} (DLL exported: {2})",
                        configuration.AuthenticatorDll,
                        configuration.AuthenticatorClass,
                        string.Join("; ", exports.Select(exp => exp.Value.GetType().FullName)));
                    Dispose();
                    return;
                }
                _export       = export;
                Authenticator = _export.Value;
                _mefContainer.ReleaseExports(exports.Where(e => e != _export));
            }
            catch (CompositionException ce)
            {
                log.Error(
                    "Failed to find an authenticator. Composition errors:\r\n\t" +
                    string.Join("\r\n\t", ce.Errors.Select(err => "Element: " + err.Element.DisplayName + ", Error: " + err.Description)),
                    ce);
                Dispose();
                return;
            }
            catch (Exception e)
            {
                log.Error("Failed to find an authenticator", e);
                Dispose();
                return;
            }

            try
            {
                log.DebugFormat("Opening SSL certificate store {0}.{1}", configuration.SslCertStoreLocation, configuration.SslCertStoreName);
                var store = new X509Store(configuration.SslCertStoreName, configuration.SslCertStoreLocation);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                var matching = store.Certificates.Find(X509FindType.FindByThumbprint, configuration.SslCertSubjectThumbprint, false);
                if (0 == matching.Count)
                {
                    log.ErrorFormat(
                        "Failed to find SSL certification with thumbprint '{0}' in location {1}, store {2}.",
                        configuration.SslCertSubjectThumbprint,
                        configuration.SslCertStoreLocation,
                        configuration.SslCertStoreName);
                    Dispose();
                    return;
                }
                Certificate = matching[0];
            }
            catch (Exception e)
            {
                log.Error("Failed to load SSL certificate", e);
                Dispose();
                return;
            }

            _tcpListener = new TcpListener(bindToEP);
            _tcpListener.Start();
            _tcpListener.BeginAcceptTcpClient(AcceptConnection, _tcpListener);

            _service.AddListener(this);

            log.InfoFormat(
                "Listening on {0} and forwarding to {1} (SSL cert DN {2}; serial {3}; authenticator {4})",
                bindToEP,
                ForwardTo,
                Certificate.Subject,
                Certificate.GetSerialNumberString(),
                Authenticator.GetType().FullName);
        }
 /// <summary>
 /// 從容器中釋放Export對象
 /// </summary>
 /// <param name="exports">要釋放的Export對象</param>
 public void ReleaseExports(IEnumerable <Export> exports)
 {
     _container.ReleaseExports(exports);
 }
Beispiel #6
0
 /// <summary>
 /// Releases many exports
 /// </summary>
 /// <typeparam name="T">Type to resolve</typeparam>
 /// <param name="exports">Object instances to release</param>
 public static void ReleaseMany <T>(IEnumerable <Lazy <T> > exports)
 {
     Container.ReleaseExports <T>(exports);
 }
Beispiel #7
0
        public TDSListener(TDSProxyService service, ListenerElement configuration)
        {
            var insideAddresses = Dns.GetHostAddresses(configuration.ForwardToHost);

            if (0 == insideAddresses.Length)
            {
                log.ErrorFormat("Unable to resolve forwardToHost=\"{0}\" for listener {1}", configuration.ForwardToHost, configuration.Name);
                _stopped = true;
                return;
            }
            ForwardTo = new IPEndPoint(insideAddresses.First(), configuration.ForwardToPort);

            _service = service;

            var bindToEP = new IPEndPoint(configuration.BindToAddress ?? IPAddress.Any, configuration.ListenOnPort);

            try
            {
                var catalog = new AggregateCatalog(from AuthenticatorElement a in configuration.Authenticators
                                                   select new AssemblyCatalog(a.Dll));
                _mefContainer = new CompositionContainer(catalog);

                var  allExports     = _mefContainer.GetExports <IAuthenticator>().ToDictionary(a => a.GetType().GetGenericArguments()[0].FullName);
                var  authenticators = new Lazy <IAuthenticator> [configuration.Authenticators.Count];
                bool die            = false;
                var  used           = new List <Lazy <IAuthenticator> >();
                for (int i = 0; i < configuration.Authenticators.Count; i++)
                {
                    var a = configuration.Authenticators[i];
                    if (!allExports.TryGetValue(a.Class, out var export))
                    {
                        log.ErrorFormat(
                            "For authenticator {0} found dll {1} but not class {2} (exports in catalog: {3})",
                            a.Name,
                            a.Dll,
                            a.Class,
                            string.Join("; ", allExports.Keys));
                        die = true;
                    }

                    used.Add(export);
                    authenticators[i] = export;
                }

                if (die)
                {
                    Dispose();
                    return;
                }

                _authenticators = authenticators;
                _mefContainer.ReleaseExports(allExports.Values.Except(used));
            }
            catch (CompositionException ce)
            {
                log.Error(
                    "Failed to find an authenticator. Composition errors:\r\n\t" +
                    string.Join("\r\n\t", ce.Errors.Select(err => "Element: " + err.Element.DisplayName + ", Error: " + err.Description)),
                    ce);
                Dispose();
                return;
            }
            catch (Exception e)
            {
                log.Error("Failed to find an authenticator", e);
                Dispose();
                return;
            }

            try
            {
                log.DebugFormat("Opening SSL certificate store {0}.{1}", configuration.SslCertStoreLocation, configuration.SslCertStoreName);
                var store = new X509Store(configuration.SslCertStoreName, configuration.SslCertStoreLocation);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                var matching = store.Certificates.Find(X509FindType.FindByThumbprint, configuration.SslCertSubjectThumbprint, false);
                if (0 == matching.Count)
                {
                    log.ErrorFormat(
                        "Failed to find SSL certification with thumbprint '{0}' in location {1}, store {2}.",
                        configuration.SslCertSubjectThumbprint,
                        configuration.SslCertStoreLocation,
                        configuration.SslCertStoreName);
                    Dispose();
                    return;
                }
                Certificate = matching[0];
            }
            catch (Exception e)
            {
                log.Error("Failed to load SSL certificate", e);
                Dispose();
                return;
            }

            _tcpListener = new TcpListener(bindToEP);
            _tcpListener.Start();
            _tcpListener.BeginAcceptTcpClient(AcceptConnection, _tcpListener);

            _service.AddListener(this);

            log.InfoFormat(
                "Listening on {0} and forwarding to {1} (SSL cert DN {2}; expires {5} serial {3}; authenticators {4})",
                bindToEP,
                ForwardTo,
                Certificate.Subject,
                Certificate.GetSerialNumberString(),
                string.Join(", ", from a in Authenticators select a.GetType().FullName),
                Certificate.GetExpirationDateString());
        }