예제 #1
0
        public List <ChannelDocument> GetDocumentation(AppDomain domain)
        {
            Debug.Assert(_services.Get <IChannelLocator>() != null);

            List <Type> channels = _services.Get <IChannelLocator>().RegisteredChannels(domain);

            foreach (var channel in channels)
            {
                ChannelDocument  channelDoc         = new ChannelDocument();
                ChannelAttribute channelAttr        = channel.GetCustomAttribute <ChannelAttribute>();
                string           channelDescription = channelAttr.Description == null ? string.Empty : channelAttr.Description.ToString();
                string           channelName        = String.IsNullOrEmpty(channelAttr.Name) ? channel.Name : channelAttr.Name;
                string           channelRoute       = $"~/channels/{channelName}";
                MethodInfo[]     methods            = channel.GetMethods().Where(x => x.GetCustomAttribute <ChannelMethodAttribute>() != null).ToArray();
                ChannelMethodDocs = new List <ChannelMethodDocument>();
                foreach (var method in methods)
                {
                    ChannelMethodDocument  methodDocument         = new ChannelMethodDocument();
                    ChannelMethodInfo      description            = _services.Get <IChannelMethodDescriptor>().GetMethodDescription(method);
                    ChannelMethodAttribute ChannelMethodAttribute = method.GetCustomAttribute <ChannelMethodAttribute>();
                    ChannelHttpMethod      HttpMethod             = ChannelMethodAttribute.HttpMethod;
                    string[] names = description.Parameters.Select(x => x.Name).ToArray();
                    Type[]   types = description.Parameters.Select(x => x.Type).ToArray();
                    methodDocument.HttpMethod          = HttpMethod;
                    methodDocument.InputParameters     = names;
                    methodDocument.InputParameterTypes = types;
                    methodDocument.ReturnTypeName      = method.ReturnType.Name;
                    methodDocument.ReturnType          = method.ReturnType;
                    methodDocument.URL         = $"{channelRoute}/{method.Name}/";
                    methodDocument.AuthSchema  = ChannelMethodAttribute.Schema;
                    methodDocument.Description = ChannelMethodAttribute.Description; //null possibility

                    ChannelMethodDocs.Add(methodDocument);
                }
                channelDoc.Name               = channel.Name;
                channelDoc.Description        = channelDescription; //null possibility
                channelDoc.URL                = channelRoute;
                channelDoc.AvailableEndpoints = ChannelMethodDocs;

                ChannelDocs.Add(channelDoc);
            }


            return(ChannelDocs);
        }
예제 #2
0
        public ChannelConfigurationInfo Configure(HttpListener httpChannel, Type channel, MethodInfo method, string baseURL)
        {
            ChannelEndpoint  endpoint    = new ChannelEndpoint();
            ChannelAttribute channelAttr = channel.GetCustomAttribute <ChannelAttribute>();

            if (!String.IsNullOrEmpty(channelAttr.Name))
            {
                endpoint.URL = "/channels/" + channelAttr.Name + "/" + method.Name + "/";
            }
            else
            {
                endpoint.URL = "/channels/" + channel.Name + "/" + method.Name + "/";
            }

            endpoint.Name = channel.Name + "." + method.Name;

            ChannelMethodAttribute       channelMethod = method.GetCustomAttribute <ChannelMethodAttribute>();
            AuthorizeChannelAttribute    authAttr      = channel.GetCustomAttribute <AuthorizeChannelAttribute>();
            ChannelAuthenticationSchemes channelSchema = channelMethod.Schema;
            ChannelHttpMethod            httpMethod    = channelMethod.HttpMethod;

            string methodURL = string.Empty;

            if (baseURL == null)
            {
                methodURL = "http://localhost:4200" + endpoint.URL;
            }
            else
            {
                methodURL = baseURL + endpoint.URL;
            }

            bool httpAuthRequired = false;

            //Start hosting
            httpChannel.Prefixes.Add(methodURL);
            if (authAttr != null)
            {
                if (authAttr.Schema != ChannelAuthenticationSchemes.Anonymous)
                {
                    httpAuthRequired = true;
                }
            }
            //ChannelMethod can override ChannelAttribute Authentication Schemes
            if (channelSchema != ChannelAuthenticationSchemes.Anonymous)
            {
                httpAuthRequired = true;
            }
            else
            {
                if (authAttr != null)
                {
                    channelSchema = authAttr.Schema;
                }
            }

            return(new ChannelConfigurationInfo
            {
                ChannelAttribute = channelAttr,
                MethodAttribute = channelMethod,
                MethodUrl = methodURL,
                HttpMethod = httpMethod,
                AuthScheme = channelSchema,
                AuthorizeAttribute = authAttr,
                AuthenticationRequired = httpAuthRequired,
                Endpoint = endpoint
            });
        }