Пример #1
0
        /// <summary>
        /// Creates what might be considered a good "default" server factory, if possible,
        /// or <c>null</c> if no good default was possible. A <c>null</c> value could be returned,
        /// for example, if a user opted to remove all implementations of <see cref="IServer"/> and
        /// the associated <see cref="IServerFactory"/> for security reasons.
        /// </summary>
        public static IServerFactory CreateDefaultServerFactoryOrNull(IExceptionContext ectx)
        {
            Contracts.CheckValue(ectx, nameof(ectx));
            // REVIEW: There should be a better way. There currently isn't,
            // but there should be. This is pretty horrifying, but it is preferable to
            // the alternative of having core components depend on an actual server
            // implementation, since we want those to be removable because of security
            // concerns in certain environments (since not everyone will be wild about
            // web servers popping up everywhere).
            var cat = ModuleCatalog.CreateInstance(ectx);

            ModuleCatalog.ComponentInfo component;
            if (!cat.TryFindComponent(typeof(IServerFactory), "mini", out component))
            {
                return(null);
            }
            IServerFactory factory = (IServerFactory)Activator.CreateInstance(component.ArgumentType);
            var            field   = factory.GetType().GetField("Port");

            if (field?.FieldType != typeof(int))
            {
                return(null);
            }
            field.SetValue(factory, 12345);
            return(factory);
        }