Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ASP.NET Core – Error CS0266 Cannot implicitly convert type StackExchange.Redis.Extensions.Core.Configuration.RedisConfiguration

In my ASP.NET Core-6 Web API, I am implementing Redis for In-Memory Cache. I installed this extensions:

<PackageReference Include="StackExchange.Redis.Extensions.AspNetCore" Version="9.1.0" />
<PackageReference Include="StackExchange.Redis.Extensions.Newtonsoft" Version="9.1.0" />

Then I have this code for the dependency injection:

public static class DIServices
{
    public static void AddDI(this IServiceCollection services)
    {
        services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>( options =>
        {
            return new RedisConfiguration();
        });
    }
}

And in Program.cs:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

builder.Services.AddDI();

However, I got this error:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0266  Cannot implicitly convert type 'StackExchange.Redis.Extensions.Core.Configuration.RedisConfiguration' to 'System.Collections.Generic.IEnumerable<StackExchange.Redis.Extensions.Core.Configuration.RedisConfiguration>'. An explicit conversion exists (are you missing a cast?)

Also:

return new RedisConfiguration()

is highlighted.

How do I resolve this?

>Solution :

The error is telling you that the function is returning a RedisConfiguration but it’s expecting to return an IEnumerable<RedisConfiguration>. An apple is not a basket of apples.

Return a collection instead:

return new List<RedisConfiguration> { new RedisConfiguration() };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading