Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Last active October 7, 2022 07:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/c20b518aeef74789469c764360076e38 to your computer and use it in GitHub Desktop.
Save justinyoo/c20b518aeef74789469c764360076e38 to your computer and use it in GitHub Desktop.
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))
...
;
}
}
@Buyani
Copy link

Buyani commented Jun 19, 2022

Where can I locate an implemented code for this in azure functions App?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment