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

Is not work for CreateApplicationBuilder #1

Open
AndresGMD opened this issue Mar 5, 2024 · 1 comment
Open

Is not work for CreateApplicationBuilder #1

AndresGMD opened this issue Mar 5, 2024 · 1 comment

Comments

@AndresGMD
Copy link

AndresGMD commented Mar 5, 2024

Hi.

Currently I'm using var builder = Host.CreateApplicationBuilder(args); in my initial configuration to work with Workerservice

This is my Appfactory for testing

public class WorkerAppFactory : WorkerApplicationFactory<Program>
{
    private IHost host;
    protected override void ConfigureHost(IHostBuilder builder)
    {
        var configuration = TestFixtures.GetContigurationTest(TestFixtures.Settings!);

        builder.UseEnvironment("Local");
        builder.ConfigureAppConfiguration((context, config) =>
            {
                // Add test configuration
                config.AddConfiguration(configuration);
            });
        builder.ConfigureServices(services =>
        {
            services.AddTestMongoDb();
        });
    }

Bu when I exceuted using StarAsync I get the following Error:

System.AggregateException : One or more errors occurred. (Method 'CreateHostBuilder' was not found in class 'Program' (Assembly: TripArc.Excursion.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null))
---- System.Exception : Method 'CreateHostBuilder' was not found in class 'Program' (Assembly: TripArc.Excursion.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at TripArc.Excursion.Api.Unit.Tests.BackgroundServiceTest.UpdateMetatronDescriptionWorkerTest..ctor(WorkerAppFactory workerFactory) in C:\Users\AndrésGustavoMUÑOZ\Projects\Trip-Arc\DMX\TripArc.Excursions.Api\TripArc.Excursion.Api.Unit.Tests\BackgroundServiceTest\UpdateMetatronDescriptionWorkerTest.cs:line 34
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span1 copyOfArgs, BindingFlags invokeAttr) ----- Inner Stack Trace ----- at WorkerService.Testing.HostFactoryResolver.ResolveFactory[T](Assembly assembly, String name) at WorkerService.Testing.HostFactoryResolver.ResolveHostBuilderFactory(Assembly assembly) at WorkerService.Testing.WorkerApplicationFactory1.CreateHostBuilder()

How can I use with

var builder = Host.CreateApplicationBuilder(args); ?

@gao-artur
Copy link
Owner

The WorkerApplicationFactory, similar to the older WebApplicationFactory, expects your entry point class to have a CreateHostBuilder method by convention. So, your Program.cs should look similar to this:

public static class Program
{
    public static void Main(string[] args)
    {
        reateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices(services =>
            {
                services.AddHostedService<Worker>();
            });
}

If you are using the newer VS templates, it probably looks like this

public class Program
{
    public static void Main(string[] args)
    {
        IHost host = Host.CreateDefaultBuilder(args)
            .ConfigureServices(services =>
            {
                services.AddHostedService<Worker>();
            })
            .Build();

        host.Run();
    }
}

Sorry, I haven't updated this code since .NET Core 3, so it won't work with newer templates. Maybe try this solution instead (I didn't try it)

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

No branches or pull requests

2 participants