microsoftarchive / redis

Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes
http://redis.io
Other
20.78k stars 5.37k forks source link

StackExchange.Redis can't write to or read from local Redis Server when I deploy my asp.Net Mvc application to IIS 8.5 on Windows Server 2012 R2 #563

Open aulusoy opened 6 years ago

aulusoy commented 6 years ago

I have an asp.net mvc application which works on .Net 4.6.2 framework. This app has Dependency Injection with Inversion of Control technics using SimpleInjector and Aspect Oriented Programming technics using PostSharp.

StackExchange.Redis library working fine on my local machine when I start to debug my solution in Visual Studio 2015 Ent. on Windows 10 Pro. I can write to and read from redis server on my local also my app can write to and read from redis server on my local when I deploy/publish my app IIS server on my remote server.

But I can't write the redis server on remote server. I check the ports and firewalls but it can't write or read in any way. Also when I trace my app it can successfully connect to redis server on same server also can send commands to it but when look up to redis monitor it does not show that commands.

What could be cause to this?

Code Samples are below

Redis Cache Manager

using System;
using System.Collections.Generic;
using System.Configuration;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace Cits.Portal.Core.CrossCuttingConcern.Caching.Redis
{
    public class RedisCacheManager : ICacheManager
    {
        private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
            var configurationOptions = new ConfigurationOptions();

            #if DEBUG
            configurationOptions.EndPoints.Add("localhost", 6379);
            #else
            configurationOptions.EndPoints.Add("141.11.11.212", 6379);
            #endif

            configurationOptions.AllowAdmin = true;
            configurationOptions.AbortOnConnectFail = false;

            return ConnectionMultiplexer.Connect(configurationOptions);
        });

        public static ConnectionMultiplexer Connection => LazyConnection.Value;

        public static IDatabase RedisCache => Connection.GetDatabase();

        public void Add(string key, object data, int cacheTime)
        {
            if (data == null || IsAdd(key))
                return;

            var value = TimeSpan.FromMinutes(cacheTime);

            RedisCache.StringSet(key, Serialize(data), value);
        }

        public T Get<T>(string key)
        {
            var value = RedisCache.StringGet(key);

            if (!value.HasValue)
                return default(T);

            return Deserialize<T>(value);
        }

        public bool IsAdd(string key)
        {
            return RedisCache.KeyExists(key);
        }

        public void Remove(string key)
        {
            RedisCache.KeyDelete(key);
        }

        public void RemoveByPattern(string pattern)
        {
            var endPoints = Connection.GetEndPoints();

            foreach (var endpoint in endPoints)
            {
                var server = Connection.GetServer(endpoint);
                var enumerable = server.Keys(RedisCache.Database, pattern);
                foreach (var current in enumerable)
                    Remove(current);
            }
        }

        public void Clear()
        {
            var endPoints = Connection.GetEndPoints();

            foreach (var endpoint in endPoints)
            {
                var server = Connection.GetServer(endpoint);

                var enumerable = server.Keys(RedisCache.Database);

                foreach (var current in enumerable)
                    Remove(current);
            }
        }

        public List<string> GetKeyList()
        {
            var list = new List<string>();

            var endPoints = Connection.GetEndPoints();

            foreach (var endpoint in endPoints)
            {
                var server = Connection.GetServer(endpoint);

                var enumerable = server.Keys(RedisCache.Database);

                foreach (var redisKey in enumerable)
                    list.Add(redisKey);
            }

            return list;
        }

        protected virtual string Serialize(object serializableObject)
        {
            var jsonSerializerSettings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            };

            return JsonConvert.SerializeObject(serializableObject, jsonSerializerSettings);
        }

        protected virtual T Deserialize<T>(string serializedObject)
        {
            if (serializedObject == null)
                return default(T);

            var jsonSerializerSettings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            };

            return JsonConvert.DeserializeObject<T>(serializedObject, jsonSerializerSettings);
        }
    }
}

Redis Cache Aspect

using System;
using System.Linq;
using Cits.Portal.Core.CrossCuttingConcern.Caching.Redis;
using PostSharp.Aspects;

namespace Cits.Portal.Core.Aspects.Caching
{
    [Serializable]
    public class CacheAspectAttribute : MethodInterceptionAspect
    {
        private readonly int _cacheTimeOut;

        public CacheAspectAttribute(int cacheTimeOut = 540)
        {
            _cacheTimeOut = cacheTimeOut;
        }

        public override void OnInvoke(MethodInterceptionArgs args)
        {
            var cacheManager = new RedisCacheManager();

            if (args.Method.ReflectedType != null)
            {
                var methodFullName = $"{args.Method.ReflectedType.Namespace}.{args.Method.ReflectedType.Name}.{args.Method.Name}";

                var arguments = args.Arguments.ToList();

                var key = $"{methodFullName}({string.Join(",", arguments.Select(x => x?.ToString() ?? "<null>"))})";

                if (cacheManager.IsAdd(key))
                {
                    args.ReturnValue = cacheManager.Get<object>(key);
                    return;
                }

                base.OnInvoke(args);

                cacheManager.Add(key, args.ReturnValue, _cacheTimeOut);
            }
        }
    }
}

Our Module List Method which is cached

[CacheAspect]
        public List<ModuleViewModel> GetListAsList()
        {
            var rowLogQuery = _rowLogService.GetListQueryable("Module");

            var moduleQuery =
                _moduleDal.GetQueryable(p => p.RowStateId != _rowState)
                    .Select(p => new ModuleViewModel
                    {
                        Id = p.Id,
                        Code = p.Code,
                        Name = p.Name,
                        IsActive = p.IsActive,
                        RowLogViewModel = rowLogQuery.FirstOrDefault(q => q.RowId.Equals(p.Id)),
                        RowStateId = p.RowStateId
                    }).ToList();

            return moduleQuery;
        }

Also These are my redis.windows.configs

bind 127.0.0.1
bind 141.11.11.212
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
databases 16

Also These are my redis.windows.service.configs

bind 127.0.0.1
bind 141.11.11.212
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
databases 16

Also I don't have redis auth password. I'm testing my app on remote server using remote servers browser but result the same.

Can you give me any suggestions about steps that I can find the issue/s?

And this the redis.server.log

[2252] 04 Aug 15:05:42.664 # Creating Server TCP listening socket 141.11.11.212:6379: bind: No error
[7504] 07 Aug 10:03:01.666 * Redis 3.2.100 (00000000/0) 64 bit, standalone mode, port 6379, pid 7504 ready to start.
[7504] 07 Aug 10:03:01.666 # Server started, Redis version 3.2.100
[7504] 07 Aug 10:03:01.666 * DB loaded from disk: 0.002 seconds
[7504] 07 Aug 10:03:01.666 * The server is now ready to accept connections on port 6379