I'm trying to run a TimeTrigger trigger from azure functions , where I need to access my repository layer where it contains the data access methods in which I get the following error when executing the project:
A ScriptHost error has occurred
[01/10/2018 19:07:39] System.Private.CoreLib: Could not load type 'Microsoft.Azure.WebJobs.Hosting.IWebJobsStartup' from assembly 'Microsoft.Azure.WebJobs.Host, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = null '.
functioncodeI'mtryingtorun:
usingSystem;usingSystem.Threading.Tasks;usingDomain.Interfaces.Repository;usingMicrosoft.Azure.WebJobs;usingMicrosoft.Azure.WebJobs.Host;usingWebFunctions.DependencySetup.Injection;namespaceWebFunctions{publicstaticclassExpirarOfertasFunction{[FunctionName("functionName")]
public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log, [Inject]INegociacaoRepository negociacaoRepository, [Inject] IUnitOfWork uow)
{
var teste = await negociacaoRepository.GetByIdAsync(Guid.NewGuid());
negociacaoRepository.Update(teste);
uow.Commit();
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
}
Code where I set the dependency injection
using WebFunctions.DependencySetup.Injection;
using Microsoft.Extensions.DependencyInjection;
using Domain.Interfaces.Repository;
using Infraestrutura.Repository;
using Domain.Interfaces.Infra;
using Infraestrutura.UoW;
namespace WebFunctions.DependencySetup
{
public class DependencyConfiguration : IDependencyConfiguration
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<INegociacaoRepository, NegociacaoRepository>();
services.AddSingleton<IUnitOfWork, UnitOfWork>();
}
}
}
DI initialization code
using System;
using System.Linq;
using WebFunctions.DependencySetup.Injection.Internal;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Extensions.DependencyInjection;
namespace WebFunctions.DependencySetup.Injection
{
internal class InjectWebJobsExtension : IExtensionConfigProvider
{
public void Initialize(ExtensionConfigContext context)
{
var rule = context.AddBindingRule<InjectAttribute>();
rule.BindToInput<Anonymous>(attribute => null);
var dependencyConfig = InitializeContainer(context);
if (dependencyConfig == null) return;
var serviceCollection = new ServiceCollection();
dependencyConfig.ConfigureServices(serviceCollection);
var container = serviceCollection.BuildServiceProvider();
rule.AddOpenConverter<Anonymous, OpenType>(typeof(InjectConverter<>), container);
}
private static IDependencyConfiguration InitializeContainer(ExtensionConfigContext context)
{
var configType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.FirstOrDefault(x =>
typeof(IDependencyConfiguration).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
IDependencyConfiguration configuration = null;
if (configType == null) return configuration;
var configInstance = Activator.CreateInstance(configType);
configuration = (IDependencyConfiguration) configInstance;
return configuration;
}
}
}
Initialization
using WebFunctions.DependencySetup.Injection;
using Microsoft.Azure.WebJobs;
namespace WebFunctions.DependencySetup.ConfigurationHost
{
public static class DependencyInjectionWebJobsBuilderExtensions
{
public static IWebJobsBuilder AddDependencyInjection(this IWebJobsBuilder builder)
{
builder.AddExtension(new InjectWebJobsExtension());
return builder;
}
}
}
SDK Version 1.0.21 Architecture and project details
Ifyouhaveanotherwaytoinjectdependency,orifitisnotpossibleintheprojectversionthatI'musing".NET STANDART 2.0", I accept suggestions for better ways to implement this function, I need to change some 00:00 records every day .
I'm following this project example in this github link: link