我有一个简单的注入器DI容器,该容器有助于实例化我的函数所需的各种依赖关系。我知道静态变量在Azure Functions中具有特殊待遇,并且我想确保自己做得对。这是我的下面的代码。这是正确的吗?
public static class DeviceDataFunction
{
public static Container Container = SimpleInjectionInitializer.Initialize();
[FunctionName("DeviceDataFunction")]
public static void Run([ServiceBusTrigger("devicedatatopic", "devicedatasubscription", AccessRights.Manage, Connection = @"ConnectionStringKey")]string data, Microsoft.Extensions.Logging.ILogger log)
{
var logger = new ILoggerAdapter(log);
logger.Info($"DeviceDataFunction ServiceBus topic trigger function processed message: {data}");
try
{
var DeviceDataService = new DeviceDataHelper(logger,
Container.GetInstance<IDeviceDataProvider>(),
Container.GetInstance<IDeviceProvider>(),
Container.GetInstance<IDeviceAlarmProvider>(),
Container.GetInstance<IAlarmTopicHandler>(),
Container.GetInstance<ICacheClient>());
DeviceDataService.Process(data);
}
catch(Exception ex)
{
var message = (ex.InnerException != null) ? ex.Message + "\n" + ex.InnerException.Message : ex.Message;
message += (ex.InnerException != null) ? ex.StackTrace + "\n" + ex.InnerException.StackTrace : ex.StackTrace;
logger.Error(message);
throw;
}
}
}
答案 0 :(得分:0)
从技术上看,它是正确的。
Azure函数对静态函数没有任何特殊处理,只是因为函数始终是静态的,所以必须使用静态初始化器。