Azure functions Azure函数中的StackExchange.Redis不工作
我创建了一个超级简单的Azure functions Azure函数中的StackExchange.Redis不工作,azure-functions,stackexchange.redis,Azure Functions,Stackexchange.redis,我创建了一个超级简单的Azure函数,它使用依赖项注入来利用我的共享库 该函数在我的开发人员计算机上本地运行正常,但当我将其发布到Azure时,我收到一个错误,该错误表示StackExchange.Redis没有指定端点--请参见以下内容: 我在某处读到,这可能是由于ConnectionMultiplexerr不是静态的。以下是共享库中我的RedisCache客户端的代码: public class RedisCache { private static IDatabase _cache
Azure函数
,它使用依赖项注入
来利用我的共享库
该函数在我的开发人员计算机上本地运行正常,但当我将其发布到Azure时,我收到一个错误,该错误表示StackExchange.Redis
没有指定端点--请参见以下内容:
我在某处读到,这可能是由于ConnectionMultiplexerr
不是静态的。以下是共享库中我的RedisCache
客户端的代码:
public class RedisCache
{
private static IDatabase _cache;
private static ConnectionMultiplexer connection;
private Dictionary<string, SemaphoreSlim> _locks;
public RedisCache(IConfiguration configuration)
{
var host = configuration["redisHost"];
var key = configuration["redisKey"];
if (_cache == null)
{
connection = ConnectionMultiplexer.Connect($"{host},abortConnect=false,ssl=true,password={key}");
_cache = connection.GetDatabase();
}
_locks = new Dictionary<string, SemaphoreSlim>();
}
public async Task<T> GetObjectAsync<T>(string key)
{
var serializedCachedData = await _cache.StringGetAsync(key);
if (!serializedCachedData.HasValue)
return default(T);
return JsonUtils.Deserialize<T>(serializedCachedData.ToString());
}
}
知道是什么导致了这个问题吗?我认为您的appsettings.json
没有正确部署。您是否可以从Azure门户检查应用程序设置,以验证您的设置是否已上载。另一个更好的方法是使用ExecutionContext
。下面是Eg代码
使用执行上下文
此外,您的ConfigurationBuilder
上缺少.AddEnvironmentVariables()
。应该这样做
var config = new ConfigurationBuilder()
.SetBasePath(currentDirectory )
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
你所有的建议都是正确的,现在它运行良好。非常感谢。真棒@Sam很乐意帮忙
var executioncontextoptions = builder.Services.BuildServiceProvider()
.GetService<IOptions<ExecutionContextOptions>>().Value;
var currentDirectory = executioncontextoptions.AppDirectory;
var binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var currentDirectory = Path.GetFullPath(Path.Combine(binDirectory, ".."));
var config = new ConfigurationBuilder()
.SetBasePath(currentDirectory )
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();