Instantly share code, notes, and snippets.

Embed
What would you like to do?
AutoMapper DI into Azure Functions
public class AppModule : Module
{
public override void Load(IServiceCollection services)
{
...
services.AddAutoMapper(Assembly.GetAssembly(this.GetType()));
...
}
}
public class GetSecretFunction : FunctionBase<ILogger>, IGetSecretFunction
{
...
private readonly IMapper _mapper;
public GetSecretFunction(AppSettings settings, IMapper mapper, IKeyVaultClient kv)
{
...
this._mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
public override async Task<TOutput> InvokeAsync<TInput, TOutput>(TInput input, FunctionOptionsBase options = null)
{
...
var mapped = this._mapper.Map<SecretModel>(secret);
...
}
}
public static class SecretsHttpTrigger
{
// Register dependencies through the fucntion factory.
public static IFunctionFactory Factory = new FunctionFactory<AppModule>();
[FunctionName(nameof(GetSecret))]
public static async Task<IActionResult> GetSecret(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "secrets/{name}")] HttpRequest req,
string name,
ILogger log)
{
IActionResult result;
try
{
...
// Invokes function containing all business logics
result = await Factory.Create<IGetSecretFunction, ILogger>(log)
.InvokeAsync<HttpRequest, IActionResult>(req, options)
.ConfigureAwait(false);
...
}
return result;
}
}
public class SecretProfile : Profile
{
public SecretProfile()
{
this.CreateMap<SecretBundle, SecretModel>()
.ForMember(d => d.Id, o => o.MapFrom(s => s.Id))
...
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment