Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to register on Azure Functions since there is no extension for IFunctionsHostBuilder #183

Closed
joaoantunes opened this issue Sep 3, 2019 · 2 comments
Labels
question Further information is requested

Comments

@joaoantunes
Copy link

Azure Functions V2 now supports .net dependency injection
In order to achieve that you need to do the following code:

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
namespace MyNamespace
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();
            builder.Services.AddSingleton((s) => {
                return new CosmosClient(Environment.GetEnvironmentVariable("COSMOSDB_CONNECTIONSTRING"));
            });
            builder.Services.AddSingleton<ILoggerProvider, MyLoggerProvider>();
        }
    }
}

I want to change the default container to use Lamar

O the documentation there is an example for a WebHost:

var builder = new WebHostBuilder();
builder
    // Replaces the built in DI container
    // with Lamar
    .UseLamar()
    
    // Normal ASP.Net Core bootstrapping
    .UseUrls("http://localhost:5002")
    .UseKestrel()
    .UseStartup<Startup>();

builder.Start();

But I'm not able to change IFunctionsHostBuilder to use "UseLamar()" extension. Since this extends IWebHostBuilder.
The only ways I was able to intercept the initialization of azure functions was Either with FunctionsStartup that configures IFunctionsHostBuilder or IWebJobsStartup that configures IWebJobsBuilder, but I don't find extensions for those kinds of builds on Lamar.

I've tried to check the existing extension to create a similar code but is not working because probably I need to create more stuff:

[assembly: FunctionsStartup(typeof(FunctionAppPrototype.Startup))]
namespace FunctionAppPrototype
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var container = new Container(x =>
            {
                x.AddTransient<IMyService, MyService>();
            });

            builder.Services.AddSingleton<IServiceProviderFactory<IServiceCollection>, LamarServiceProviderFactory>();
            builder.Services.AddSingleton<IServiceProviderFactory<ServiceRegistry>, LamarServiceProviderFactory>();
        }
    }
}
@jeremydmiller
Copy link
Member

@joaoantunes I would accept a pull request for adding a new Nuget that supports the Azure FunctionsBuilder.

Regardless of a pull request, you should be able to pretty well copy/paste the little bit of code for IHostBuilder support in the AspNetCore adapter

@jeremydmiller jeremydmiller added the question Further information is requested label Sep 15, 2019
@jeremydmiller
Copy link
Member

No recent activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants