Git Product home page Git Product logo

dotnetdevops.extensions.azurefunctions's Introduction


Extensions for Azure Functions to run AspNetCore applications

If you run into any inconsistencies, bugs or incompatibilities, kindly let us know and we'll do our best to address them.

Status

Branch Status myget
Master Master
Dev Build Status

Usage

Create your function project and use the following boilerplate for a catchall route that delegates to the aspnet app using a custom binding.

    public class ServerlessApi 
    { 
        [FunctionName("AspNetCoreHost")]
        public Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, Route = "{*all}")]HttpRequest req,
            [AspNetCoreRunner(Startup = typeof(Startup))] IAspNetCoreRunner aspNetCoreRunner,
            ExecutionContext executionContext)
        {

            return aspNetCoreRunner.RunAsync(executionContext);
        }
    }

if you want to customize the WebHostBuilder for the application, you may do so using the following example. Using the WebJobStartup AspNetCoreWebHostStartUp<TWebBuilder,TStartup>, it will auto registere it with DI and fire it up in your function.

    [assembly: WebJobsStartup(typeof(AspNetCoreWebHostStartUp<pksorensen.web.FunctionHost.WebBuilder, pksorensen.web.Startup>))]

    public class WebBuilder : IWebHostBuilderExtension<Startup>
    {
        private readonly IHostingEnvironment environment;

        public WebBuilder(IHostingEnvironment environment)
        {
            this.environment = environment;
        }
        public void ConfigureAppConfiguration(WebHostBuilderContext context, IConfigurationBuilder builder)
        {

        }
        private void Logging(ILoggingBuilder b)
        {
            //b.AddProvider(new SerilogLoggerProvider(
            //            new LoggerConfiguration()
            //               .MinimumLevel.Verbose()
            //               .MinimumLevel.Override("Microsoft", LogEventLevel.Verbose)
            //               .Enrich.FromLogContext()
            //                .WriteTo.File($"apptrace.log", buffered: true, flushToDiskInterval: TimeSpan.FromSeconds(30), rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 32, rollingInterval: RollingInterval.Hour)
            //               .CreateLogger()));
        }

        public void ConfigureWebHostBuilder(ExecutionContext executionContext, WebHostBuilder builder)
        {
            builder.ConfigureAppConfiguration(ConfigureAppConfiguration);
            builder.ConfigureLogging(Logging);

            if (environment.IsDevelopment())
            {
                builder.UseContentRoot(Path.Combine(Directory.GetCurrentDirectory(), "../../../../../apps/pksorensen.web"));
            }
        }
    }

Razor Pages and MVC apps

The following steps should allow you to setup a function host for your MVC/Razor application.

  1. Open your solution with existing razor/mvc app, or create a new project with this and create your site.
  2. Create new AzureFunction with http trigger. Project.Web.FunctionHost ect
  3. Add a nuget.config file, for using prerelease packages (until its on nuget)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="DotNetDevOps" value="https://www.myget.org/F/dotnet-devops" />
    <add key="NuGet" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>
  1. Update your dependencies of the FunctionHost app, and also aspnet core 2.2 <TargetFramework>netcoreapp2.2</TargetFramework>
    <PackageReference Include="DotNETDevOps.Extensions.AzureFunctions" Version="1.0.0-pre-2019050410" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.RazorPages" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="2.2.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.27" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
  1. Update the function to the code snippet above and update the startup class name to yours.
  2. Update host.json to remove api prefix on routes and add "ASPNETCORE_ENVIRONMENT": "Development" to local.settings.json
{
  "version": "2.0",
  "extensions": {
    "http": {
      "routePrefix": ""
    }
  }
}
  1. Add [assembly: WebJobsStartup(typeof(AspNetCoreWebHostStartUp))] to your function.cs
  2. To get the razor views compiled and outputed to your function bin folder you most update your FunctionHost.csproj file with post build events
xcopy /y "$(TargetDir)*.Views.dll" "$(TargetDir)bin\"
xcopy /y "$(TargetDir)*.Views.pdb" "$(TargetDir)bin\"

or copy paste

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="xcopy /y &quot;$(TargetDir)*.Views.dll&quot; &quot;$(TargetDir)bin\&quot;&#xD;&#xA;xcopy /y &quot;$(TargetDir)*.Views.pdb&quot; &quot;$(TargetDir)bin\&quot;" />
  </Target>
  1. and aspnet core project.csproj with
   <RazorCompileOnBuild>True</RazorCompileOnBuild>
   <RazorCompileOnPublish>True</RazorCompileOnPublish>
   <RazorEmbeddedResource>True</RazorEmbeddedResource>
   <PreserveCompilationContext>true</PreserveCompilationContext>
   <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>

dotnetdevops.extensions.azurefunctions's People

Contributors

espray avatar pksorensen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.