Exemplo n.º 1
0
        /// <summary>
        /// Parses the urls of the instance
        /// </summary>
        private void ParseInterface()
        {
            String WebAttribute = "";
            String WebMethod    = "";

            #region Find the correct interface

            var _AllInterfaces = from _Interface
                                 in _Instance.GetType().GetInterfaces()
                                 where _Interface.GetCustomAttributes(typeof(ServiceContractAttribute), false).IsNotNullOrEmpty()
                                 select _Interface;

            var _Count = _AllInterfaces.Count();

            if (_Count < 1)
            {
                throw new Exception("Could not find any valid interface having the ServiceContract attribute!");
            }

            if (_Count > 1)
            {
                throw new Exception("Multiple interfaces having the ServiceContract attribute!");
            }

            var _CurrentInterface = _AllInterfaces.FirstOrDefault();

            if (_CurrentInterface == null)
            {
                throw new Exception("Could not find any valid interface having the ServiceContract attribute!");
            }

            #endregion

            #region Check global Force-/NoAuthenticationAttribute

            var _GlobalNeedsExplicitAuthentication = false;

            if (_CurrentInterface.GetCustomAttributes(typeof(NoAuthenticationAttribute), false).IsNotNullOrEmpty())
            {
                _GlobalNeedsExplicitAuthentication = false;
            }

            if (_CurrentInterface.GetCustomAttributes(typeof(ForceAuthenticationAttribute), false).IsNotNullOrEmpty())
            {
                _GlobalNeedsExplicitAuthentication = true;
            }

            #endregion

            Boolean?NeedsExplicitAuthentication = null;

            foreach (var Method in _CurrentInterface.GetMethods())
            {
                WebAttribute = "";
                NeedsExplicitAuthentication = _GlobalNeedsExplicitAuthentication;

                foreach (var _Attribute in Method.GetCustomAttributes(true))
                {
                    #region WebGet

                    var _WebAttribute = _Attribute as WebGetAttribute;
                    if (_WebAttribute != null)
                    {
                        WebAttribute = _WebAttribute.UriTemplate.ToLower();
                        WebMethod    = "GET";
                        continue;
                    }

                    #endregion

                    #region WebInvoke

                    var _WebInvokeAttribute = _Attribute as WebInvokeAttribute;
                    if (_WebInvokeAttribute != null)
                    {
                        WebAttribute = _WebInvokeAttribute.UriTemplate.ToLower();
                        WebMethod    = _WebInvokeAttribute.Method;
                        continue;
                    }

                    #endregion

                    #region NoAuthentication

                    var _NoAuthenticationAttribute = _Attribute as NoAuthenticationAttribute;
                    if (_NoAuthenticationAttribute != null)
                    {
                        NeedsExplicitAuthentication = false;
                        continue;
                    }

                    #endregion

                    #region ForceAuthentication

                    var _ForceAuthenticationAttribute = _Attribute as ForceAuthenticationAttribute;
                    if (_ForceAuthenticationAttribute != null)
                    {
                        NeedsExplicitAuthentication = true;
                        continue;
                    }

                    #endregion

                    #region NeedsAuthentication

                    var _NeedsAuthenticationAttribute = _Attribute as NeedsAuthenticationAttribute;
                    if (_NeedsAuthenticationAttribute != null)
                    {
                        NeedsExplicitAuthentication = _NeedsAuthenticationAttribute.NeedsAuthentication;
                        continue;
                    }

                    #endregion
                }

                _Parser.AddUrl(WebAttribute, Method, WebMethod, NeedsExplicitAuthentication);
            }
        }