Azure / azure-functions-dotnet-extensions

Azure Functions .NET extensions
MIT License
75 stars 42 forks source link

Error during startup when using SignalR.Client #64

Open rochapablo opened 3 years ago

rochapablo commented 3 years ago

I have this Function that it's working and now I need to install Microsoft.AspNetCore.SignalR.Client as dependency. The problem is that after install and do all the settings, I'm not able to run the Function.

This is how I'm running

CLI_DEBUG=1 func start --functions MyHttpTrigger --csharp --verbose

The error I'm getting

[2021-09-10T14:39:16.812Z] A host error has occurred during startup operation 'b567a982-7518-4be4-ac01-a800c7ca491e'.
[2021-09-10T14:39:16.813Z] System.MissingMethodException: Method not found: 'Microsoft.Extensions.Logging.ILoggingBuilder Microsoft.Extensions.Logging.ConsoleLoggerExtensions.AddConsole(Microsoft.Extensions.Logging.ILoggingBuilder)'.
[2021-09-10T14:39:16.813Z]    at Startup.<>c.<Configure>b__0_0(ILoggingBuilder options)
[2021-09-10T14:39:16.813Z]    at Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging(IServiceCollection services, Action`1 configure)
[2021-09-10T14:39:16.813Z]    at Startup.Configure(IFunctionsHostBuilder builder) in /Users/.../Startup.cs:line 29
[2021-09-10T14:39:16.813Z]    at Microsoft.Azure.Functions.Extensions.DependencyInjection.FunctionsStartup.Configure(IWebJobsBuilder builder)
[2021-09-10T14:39:16.813Z]    at Microsoft.Azure.WebJobs.WebJobsBuilderExtensions.ConfigureStartup(IWebJobsStartup startup, WebJobsBuilderContext context, IWebJobsBuilder builder) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Hosting\WebJobsBuilderExtensions.cs:line 168
[2021-09-10T14:39:16.813Z]    at Microsoft.Azure.WebJobs.WebJobsBuilderExtensions.ConfigureAndLogUserConfiguredServices(IWebJobsStartup startup, WebJobsBuilderContext context, IWebJobsBuilder builder, ILoggerFactory loggerFactory) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Hosting\WebJobsBuilderExtensions.cs:line 131
[2021-09-10T14:39:16.813Z]    at Microsoft.Azure.WebJobs.WebJobsBuilderExtensions.UseWebJobsStartup(IWebJobsBuilder builder, Type startupType, WebJobsBuilderContext context, ILoggerFactory loggerFactory) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Hosting\WebJobsBuilderExtensions.cs:line 118
[2021-09-10T14:39:16.813Z]    at Microsoft.Azure.WebJobs.WebJobsBuilderExtensions.UseExternalStartup(IWebJobsBuilder builder, IWebJobsStartupTypeLocator startupTypeLocator, WebJobsBuilderContext context, ILoggerFactory loggerFactory) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Hosting\WebJobsBuilderExtensions.cs:line 211
[2021-09-10T14:39:16.813Z]    at Microsoft.Azure.WebJobs.Script.ScriptHostBuilderExtensions.<>c__DisplayClass7_0.<AddScriptHostCore>b__1(HostBuilderContext context, IWebJobsBuilder webJobsBuilder) in D:\a\1\s\src\WebJobs.Script\ScriptHostBuilderExtensions.cs:line 207
[2021-09-10T14:39:16.813Z]    at Microsoft.Extensions.Hosting.WebJobsHostBuilderExtensions.<>c__DisplayClass5_0.<ConfigureWebJobs>b__1(HostBuilderContext context, IServiceCollection services) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Hosting\WebJobsHostBuilderExtensions.cs:line 56
[2021-09-10T14:39:16.813Z]    at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
[2021-09-10T14:39:16.813Z]    at Microsoft.Extensions.Hosting.HostBuilder.Build()
[2021-09-10T14:39:16.813Z]    at Microsoft.Azure.WebJobs.Script.WebHost.DefaultScriptHostBuilder.BuildHost(Boolean skipHostStartup, Boolean skipHostConfigurationParsing) in D:\a\1\s\src\WebJobs.Script.WebHost\DefaultScriptHostBuilder.cs:line 59
[2021-09-10T14:39:16.813Z]    at Microsoft.Azure.WebJobs.Script.WebHost.WebJobsScriptHostService.BuildHost(Boolean skipHostStartup, Boolean skipHostJsonConfiguration) in D:\a\1\s\src\WebJobs.Script.WebHost\WebJobsScriptHostService.cs:line 542
[2021-09-10T14:39:16.813Z]    at Microsoft.Azure.WebJobs.Script.WebHost.WebJobsScriptHostService.UnsynchronizedStartHostAsync(ScriptHostStartupOperation activeOperation, Int32 attemptCount, JobHostStartupMode startupMode) in D:\a\1\s\src\WebJobs.Script.WebHost\WebJobsScriptHostService.cs:line 244
System.ArgumentNullException: Value cannot be null. (Parameter 'provider')
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Azure.Functions.Cli.Actions.HostActions.StartHostAction.RunAsync() in D:\a\1\s\src\Azure.Functions.Cli\Actions\HostActions\StartHostAction.cs:line 325
   at Azure.Functions.Cli.ConsoleApp.RunAsync[T](String[] args, IContainer container) in D:\a\1\s\src\Azure.Functions.Cli\ConsoleApp.cs:line 66

Setup.cs

[assembly: FunctionsStartup(typeof(PremiumAds.Jobs.Functions.Startup))]

namespace PremiumAds.Jobs.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            string connection = Environment.GetEnvironmentVariable("SqlConnectionString");

            builder.Services.AddLogging(options => options.AddConsole());

            builder.Services.AddDbContext<BaseContext>(options => options.UseSqlServer(connection), ServiceLifetime.Transient);

            builder.Services.AddTransient((container) =>
                {
                    var context = container.GetService<BaseContext>();
                    var stack = container.GetRequiredService<ILogger<StackService>>();
                    return new StackService(stack, new StackConfiguration(context), null);
                });
        }
    }
}

My Function

[FunctionName("MyHttpTrigger")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log)
{
    await SignalRService.Publish("test title", "test message");
    return HttpEmptyResponse();
}

Service

public static class SignalRService
{
    public static async Task Publish(string title, string message)
    {
        var connection = new HubConnectionBuilder()
            .WithUrl("...")
            .Build();

        await connection.StartAsync();

        await connection.InvokeAsync("Test", title, message);
    }
}

.csproj

<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.10">
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.5.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.2.3" />

https://stackoverflow.com/questions/69161049

MikeBecatti commented 3 years ago

The following line adds console logging: builder.Services.AddLogging(options => options.AddConsole());

If you do not want console logging remove the AddConsole() option. If you do want console logging then you must add the following nuget package: "Microsoft.Extensions.Logging.Console"