Ejemplo n.º 1
0
        public Cache CreateCache()
        {
            ISerializer serializer = null;
            switch (this._serializerType)
            {
                case SerializerType.ProtoBufNet:
                    serializer = new ProtoBufSerializer();
                    break;
                case SerializerType.Json:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            ILocalCacheAdapter firstLevelCache = null;

            switch (this._firstLevelCacheType)
            {
                case FirstLevelCacheType.None:
                    break;
                case FirstLevelCacheType.ConcurrentDictionary:
                    firstLevelCache = new DictionaryCacheAdapter();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }

            IDistributedCacheAdapter secondLevelCache = null;
            IMessenger messenger = null;
            switch (this._secondLevelCacheType)
            {
                case SecondLevelCacheType.None:
                    break;
                case SecondLevelCacheType.Redis:
                    if (this._redisInstances == null || !this._redisInstances.Any())
                        throw new ArgumentNullException("Missing redis instances configurations");
                    var redis = new RedisCacheAdapter(this._redisInstances, this._redisDatabase, serializer);
                    secondLevelCache = redis;
                    messenger = redis;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return new Cache(firstLevelCache, secondLevelCache, messenger);
        }
Ejemplo n.º 2
0
        void ListenerCallback(IAsyncResult asyncRequest)
        {
            HttpListenerContext context  = m_httpListener.EndGetContext(asyncRequest);
            RestRequest         request  = new RestRequest(context.Request);
            RestResponse        response = new RestResponse(context.Response);
            Type   restModuleType        = null;
            string result = "";

            bool        isNewSession = false;
            RestSession session      = m_sessionManager.GetSession(request, out isNewSession);

            m_logger.LogInfo("Server received: " + request.ToString());

            if (m_restModuleTypes.TryGetValue(request.RESTModuleName, out restModuleType))
            {
                // Find the method on the module by name
                MethodInfo method = restModuleType.GetMethod(request.RESTMethodName);

                if (method != null)
                {
                    // Attempt to convert convert all string arguments over to the corresponding native types
                    try
                    {
                        ParameterInfo[] methodParameters = method.GetParameters();
                        object[]        parameters       = new object[methodParameters.Length];

                        for (int parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
                        {
                            Type   parameterType   = methodParameters[parameterIndex].ParameterType;
                            string parameterName   = methodParameters[parameterIndex].Name;
                            string stringParameter = request.RESTMethodParameters[parameterName];

                            parameters[parameterIndex] = Convert.ChangeType(stringParameter, parameterType);
                        }

                        // Attempt to call the REST method
                        try
                        {
                            ICacheAdapter    applicationCacheAdapter = new DictionaryCacheAdapter(m_applicationCache);
                            ISessionAdapter  sessionAdapter          = session;
                            IResponseAdapter responseAdapter         = response;

                            RestModule restModule =
                                (RestModule)Activator.CreateInstance(
                                    restModuleType,
                                    applicationCacheAdapter,
                                    sessionAdapter,
                                    responseAdapter);

                            object methodResult = method.Invoke(restModule, parameters);

                            result = methodResult.ToString();
                        }
                        catch (System.Exception ex)
                        {
                            result = "Failed to invoke REST Method: " + ex.Message;
                            m_logger.LogError(result);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        result = "Malformed REST Parameters: " + ex.Message;
                        m_logger.LogError(result);
                    }
                }
                else
                {
                    result = "Unknown REST Method: " + request.RESTMethodName;
                    m_logger.LogError(result);
                }
            }
            else
            {
                result = "Unknown REST Module: " + request.RESTModuleName;
                m_logger.LogError(result);
            }

            // If the module wants the session to be abandoned free it from the session manager
            if (session.IsAbandoned)
            {
                m_sessionManager.FreeSession(session);
            }
            // If this is a new session, give the client the session cookie
            else if (isNewSession)
            {
                response.AppendSessionIdCookie(session);
            }

            // If there was any result string from the rest method, assign it to the result
            if (result.Length > 0)
            {
                response.SetBody(result);
            }

            response.Close();
        }