Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Member 'EndPoints' cannot be initialized. It is not a field or property. #2238

Closed
vxsaurabh-s opened this issue Sep 1, 2022 · 7 comments
Closed

Comments

@vxsaurabh-s
Copy link

vxsaurabh-s commented Sep 1, 2022

Hi
Server - AWS Elastic Cache Cluster mode with 2 nodes
Engine - 6.2.5
Client - StackExchange.Redis 2.1.58

This is our code which had been working until recently.

using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace RedisHelper.Client
{
    public class RedisHelperClient
    {

        //  private static readonly Lazy<ConnectionMultiplexer> LazyConnection;

      private static ConfigurationOptions _configurationOptions;
     

        public RedisHelperClient(string argStrEndPoints, string argStrPassword, bool argSsl = false)
        {
            _configurationOptions = new ConfigurationOptions
            {
                EndPoints = { argStrEndPoints },
                Password = argStrPassword,
                AbortOnConnectFail = false,
                KeepAlive = 30,
                ConnectTimeout = 2 * 60 * 1000, // in millisond defulat 5 sec , currently changed to 2 minute
                SyncTimeout = 2 * 60 * 1000, // in millisond defulat 5 sec , currently changed to 2 minute
                Ssl = argSsl

            };
            _configurationOptions.CertificateValidation += (sender, cert, chain, errors) =>
            {
                //Console.WriteLine("errors: " + errors);
                return true;
            };
        }

    

        private static IDatabase RedisCache
        {
            get
            {
                return Connection.GetDatabase();
            }
        }

        private static readonly Lazy<ConnectionMultiplexer> LazyConnection
            = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(_configurationOptions));

        public static ConnectionMultiplexer Connection
        {
            get
            {
                return LazyConnection.Value;
            }
        }


        #region Check Keys


      public async Task<bool> IsRedisKeyExistAsync(string argKeyName)
        {
            bool result = false;
            try
            {

                result = await RedisCache.KeyExistsAsync(argKeyName);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;

        }

 }
}

But since we updated our StackExchange.Redis package to version 2.6.48, we are getting this error message :Member 'EndPoints' cannot be initialized. It is not a field or property.
Is there any logic change to the way we connect to redis, or is there is any alternative to this?

@mgravell
Copy link
Collaborator

mgravell commented Sep 1, 2022

Hmm; I suspect you're using a pretty old C# compiler - it looks like the addition of init here (/cc @NickCraver) is making it very unhappy.

Does it work if you do:

            _configurationOptions = new ConfigurationOptions
            {
                Password = argStrPassword,
                AbortOnConnectFail = false,
                KeepAlive = 30,
                ConnectTimeout = 2 * 60 * 1000, // in millisond defulat 5 sec , currently changed to 2 minute
                SyncTimeout = 2 * 60 * 1000, // in millisond defulat 5 sec , currently changed to 2 minute
                Ssl = argSsl

            };
            _configurationOptions.EndPoints.Add(argStrEndPoints);

?

As side notes:

  1. a static backing field from an instance constructor looks like a recipe for problems - did you perhaps mean readonly instead of static?
  2. the empty try/catch/throw in IsRedisKeyExistsAsync is redundant; can be simply public Task<bool> IsRedisKeyExistAsync(string argKeyName) => RedisCache.KeyExistsAsync(argKeyName); (may need a {return ...} depending on C# version )

@vxsaurabh-s
Copy link
Author

Hello @mgravell ,
Thanks for your quick guidance.
We are running Visual Studio 2019, with .Net Standard Framework 2.1.
MicrosoftTeams-image (2)

Now we have moved our library class to .Net 6.0(VS 2022) and as a result, we are able to work with StackExchange.Redis v2.6.48.

Regarding your other suggestions/side notes, if we change "static" to "readonly", we are not allowed to assign any value.

I have shared my connection code above, can you let me know the correct way to connect with redis, or point me in right direction/reference documents so that I may avoid any future issues?

Thanks
Saurabh

@mgravell
Copy link
Collaborator

mgravell commented Sep 2, 2022

There is no single definition of correct - everything in programming is contextual. However: a static field assigned in an instance constructor would usually be wrong - that means if you create 42 objects, they're all sharing the same field

@vxsaurabh-s
Copy link
Author

Hi @mgravell,

Thank you for the suggestions.

There is another thing we have noticed after we updated our StackExchange.Redis package to version 2.6.48, the first call to redis always takes a lot of time(approx 2min 15sec) when the redis we are trying to connect to has "SSL=True" and all the subsequent calls are fast,
But if I am using redis server where "SSL=False" then both StackExchange.Redis(old and new versions) libraries work fast.

This was not the case with older version of StackExchange.Redis(v 2.1.58).

Is there anything else we should be aware of?

Thanks,
Saurabh

@Archanial
Copy link

Hello,
I've upgraded StackExchange from 2.2.xx to (the newest at the time) 2.6.48. The issue with EndPoints is that old version (pre version 8 if memory servers me right) do not have support for init only setters and during runtime it was trying to call the wrong proc.
I've personally solved it by moving to the .net 6 with newest language version and it is working fine.

About the other issue, we've noticed the first connection to redis sometimes takes very long. As @vxsaurabh-s mentioned it can be in the ranges of 2 to even 4 minutes, which causes some of our systems to timeout.
Moving to the newest version of the library didn't fix the issue.

@NickCraver
Copy link
Collaborator

On the connection issue, version 2.6.70 is on MyGet which hopefully resolves the connection duration - if you're able to confirm that fixes your other issue I'd hugely appreciate it.

@NickCraver
Copy link
Collaborator

Alrighty, since the first issue was compiler and second is now on NuGet with the connection fix (2.6.70) going to tidy up here. If that library update doesn't solve the second issue of using the full timeout please comment and we'll definitely reopen to poke!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants