Skip to content

Instantly share code, notes, and snippets.

@jessebarocio
Last active July 17, 2022 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessebarocio/e526223cb37f27cb0993a6b26a2a2ee7 to your computer and use it in GitHub Desktop.
Save jessebarocio/e526223cb37f27cb0993a6b26a2a2ee7 to your computer and use it in GitHub Desktop.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace DIExample
{
public class HttpTrigger
{
private readonly IMyService _myService;
public HttpTrigger(IMyService myService)
{
_myService = myService;
}
[FunctionName("MyHttpTrigger")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
ILogger log)
{
// Call the service that the dependency injection system gave us
await _myService.DoSomethingAsync();
return new NoContentResult();
}
}
}
dotnet add package Microsoft.Extensions.DependencyInjection
dotnet add package Microsoft.Azure.Functions.Extensions
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(DIExample.Startup))]
namespace DIExample
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient<IMyService, MyService>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment