Last active
December 29, 2020 16:38
-
-
Save SimonLuckenuik/2f35167e1df1d9ee82c17e01893e8dbc to your computer and use it in GitHub Desktop.
Concept example: Hosting an AspNetCore WebAPI inside an Azure Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
using System.IO; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.TestHost; | |
using Microsoft.Azure.WebJobs.Host; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Newtonsoft.Json; | |
namespace FunctionApp1 | |
{ | |
public static class HttpRequests | |
{ | |
private static TestServer InternalTestServer; | |
static HttpRequests() | |
{ | |
var functionPath = new FileInfo(typeof(HttpRequests).Assembly.Location).Directory.FullName; | |
Directory.SetCurrentDirectory(functionPath); | |
InternalTestServer = CreateServer(); | |
} | |
private static TestServer CreateServer() | |
{ | |
var functionPath = new FileInfo(typeof(HttpRequests).Assembly.Location).Directory.FullName; | |
return new TestServer(Microsoft.AspNetCore.WebHost | |
.CreateDefaultBuilder() | |
.ConfigureAppConfiguration((builderContext, config) => | |
{ | |
var builder = config | |
.SetBasePath(functionPath) | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | |
.AddJsonFile($"appsettings.{builderContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true) | |
.AddEnvironmentVariables(); | |
}) | |
.UseStartup<Startup>() | |
.UseContentRoot(functionPath)); | |
} | |
[FunctionName(nameof(HttpRequests))] | |
public static async Task<HttpResponseMessage> Run(CancellationToken ct, | |
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "patch", "options", Route = "{*x:regex(^(?!admin|debug|monitoring).*$)}")]HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info("C# HTTP trigger function processed a request."); | |
var client = InternalTestServer.CreateClient(); | |
var responseMessage = await client.SendAsync(req, ct); | |
return null; | |
} | |
} | |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
} | |
public void Configure(IApplicationBuilder app) | |
{ | |
app.UseMvc(); | |
} | |
} | |
[Route("api/[controller]")] | |
public class TodoController : Controller | |
{ | |
private readonly IConfiguration _configuration; | |
public TodoController(IConfiguration configuration) | |
{ | |
_configuration = configuration; | |
} | |
[HttpGet("{id}", Name = "GetTodo")] | |
public IActionResult GetById(long id) | |
{ | |
return new ObjectResult(id + _configuration["Message"]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<TargetFramework>netstandard2.0</TargetFramework> | |
<AzureFunctionsVersion>v2</AzureFunctionsVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" /> | |
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" /> | |
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.0.2" /> | |
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.1" /> | |
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.6" /> | |
</ItemGroup> | |
<ItemGroup> | |
<None Update="host.json"> | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
</None> | |
<None Update="local.settings.json"> | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
<CopyToPublishDirectory>Never</CopyToPublishDirectory> | |
</None> | |
</ItemGroup> | |
</Project> |
Also worth checking out https://github.com/tntwist/NL.Serverless.AspNetCore which is a framework which achieves the same thing and seems to work well with azure functions v3.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please note that the goal of this example is to show a concept where a WebAPI app would be hosted inside and Azure Functions. This example was not tested for performance and rely on test related classes which were not meant to be used this way. Also note that while this code compile, you get an assembly loading runtime error when executing the application.