Anyone using PactNet with .Net 6 yet? Interested i...
# pact-net
l
Anyone using PactNet with .Net 6 yet? Interested in understanding how you are dealing with the absence of Startup Class
f
I'm using it with no problem? Maybe i didn't understood the issue?
l
.net 6 doesn't have startup class by default. Does the service come with or without a startup class?
f
we run it with a dotnet test and a add a startup class indeed
but for the service onlu
and we have a test startup class
a very simple one
l
Yes our service doesn't have a startup class. Looking for examples like this
f
just create a test startup class and in the provider test class use it
you create a startup class that maps the controlers only
and then in the provider you add a webhost that uses it
l
Okay I'll give this a try! Thanks
b
You might also get some inspiration from ASP.NET's guidance for integration tests for applications that don't use Startup: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory
👍 2
e
@Bret McClory
You might also get some inspiration from ASP.NET's guidance for integration tests for applications that don't use Startup:
FYI - suggesting to look at the ASP.NET guidance for integration tests is a bit of a bum steer, as the HTTP client returned by WebApplicationFactory can't be used by the Pact Verifier - see this issue: https://github.com/pact-foundation/pact-net/issues/283 @Matt (pactflow.io / pact-js / pact-go)
☝️ 1
@Lewis Prescott what did you end up doing?
b
"inspiration" != literally use the same web application factory -- which replaces Kestrel with an in-memory TestServer and returns an HTTP client that shoots requests directly into that server. Those pieces certainly won't work with a Pact verifier that runs out-of-process. But you could examine Microsoft's source to see how they solved the problem of resolving a SUT's host when it uses WebApplication API for building a host. There may be public bits you can borrow from that, e.g. the HostFactoryResolver they're using in there. https://github.com/dotnet/aspnetcore/blob/a5bb3e8761dcfc21d3a31f3c85d4d31779ef4f4f/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs#L390 I'm sure there's a solution somewhere there; We haven't moved to .NET 6 yet so I haven't personally had to solve this yet. Good luck!
l
In the same situation as Bret, haven't moved yet. Just working out whether it's possible
🙏 1
b
Also worth pointing out: You aren't required to use the WebApplication API for ASP.NET Core 6.0 projects. You continue to use Generic Host (Builder) APIs directly. That's probably what we'll be doing as we upgrade projects to .NET 6. For projects with any complexity, I personally like the separation of concerns having Host and Application configuration in different places. I also like how it makes things consistent with Worker (non- web) applications. YMMV.
y
@Matt Lund This may be worth a read if you haven’t already seen it. I assume yourself and @Lewis Prescott have been chatting about this
l
I referred Matt to this channel. Need to spend some time investigating the proposals in this thread
👍 1
j
Hi, @Lewis Prescott I am running into the same issue. Did you manage to solve this with a test startup which maps your provider controllers?
l
Unfortunately not. Bit blocked by internal politics
j
Ah sorry to hear that, thank you for the response.
Hi @Fábio Rodrigues, are you able to share a code snippet of your Test Startup please? I'm having trouble replicating it from the your earlier comments :)
For those looking for a Test Startup so they can run their Pact tests while having their actual Web API project using the .net 6 minimal hosting model, here is my test startup which works. Where Controller is the controller you want to Pact test:
Copy code
public class TestStartup
{
        public TestStartup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        
        public void ConfigureServices(IServiceCollection services)
        {
            var controllerAssembly = Assembly.GetAssembly(typeof(Controller))!;

              services.AddMvc().AddApplicationPart(controllerAssembly)
                               .AddControllersAsServices();
        }
        
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();
            
            app.UseEndpoints(e => e.MapControllers());
        }
}
🎉 2
l
Thanks @Jake Howden for sharing. @Yousaf Nabi (pactflow.io) @Matt (pactflow.io / pact-js / pact-go) could we get this in the examples please?
👍 1
m
Thanks folks! @Adam Rodger (pact-net) what do you think about the above suggestion? Perhaps we could update the Pactflow examples to use the 4.x.x code base (it’s still on 3.x.x) and use this strategy? I’m not a .NET expert so will take guidance. cc: @Yousaf Nabi (pactflow.io)
y
Sounds like a sensible idea, thanks @Jake Howden for sharing, we have a few example/sample repos for .NET and there was some work tracked for be performed on these so this is a good opportunity, also to help bring in more contributors. Related .NET pact workshop issue tracking the upgrade to use the 4.x.x codebase https://github.com/DiUS/pact-workshop-dotnet-core-v3/issues/6 pact-foundation workshop https://github.com/pact-foundation/pact-workshop-dotnet-core-v1 pactflow .net example consumer https://github.com/pactflow/example-consumer-dotnet pactflow .net example provider https://github.com/pactflow/example-provider-dotnet
👍 1