https://pact.io logo
#pact-net
Title
# pact-net
l

Lewis Prescott

04/19/2022, 2:16 PM
Anyone using PactNet with .Net 6 yet? Interested in understanding how you are dealing with the absence of Startup Class
f

Fábio Rodrigues

04/19/2022, 3:31 PM
I'm using it with no problem? Maybe i didn't understood the issue?
l

Lewis Prescott

04/19/2022, 3:37 PM
.net 6 doesn't have startup class by default. Does the service come with or without a startup class?
f

Fábio Rodrigues

04/19/2022, 3:37 PM
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

Lewis Prescott

04/19/2022, 3:39 PM
Yes our service doesn't have a startup class. Looking for examples like this
f

Fábio Rodrigues

04/19/2022, 3:40 PM
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

Lewis Prescott

04/19/2022, 3:41 PM
Okay I'll give this a try! Thanks
b

Bret McClory

04/21/2022, 5:06 PM
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

Eddie Stanley

05/22/2022, 11:27 PM
@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

Bret McClory

05/23/2022, 2:10 AM
"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

Lewis Prescott

05/23/2022, 6:02 AM
In the same situation as Bret, haven't moved yet. Just working out whether it's possible
🙏 1
b

Bret McClory

05/23/2022, 3:22 PM
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

Yousaf Nabi (pactflow.io)

05/23/2022, 5:13 PM
@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

Lewis Prescott

05/23/2022, 5:49 PM
I referred Matt to this channel. Need to spend some time investigating the proposals in this thread
👍 1
j

Jake Howden

07/18/2022, 4:00 PM
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

Lewis Prescott

07/18/2022, 4:02 PM
Unfortunately not. Bit blocked by internal politics
j

Jake Howden

07/18/2022, 5:27 PM
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

Lewis Prescott

07/19/2022, 8:53 AM
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

Matt (pactflow.io / pact-js / pact-go)

07/19/2022, 1:30 PM
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

Yousaf Nabi (pactflow.io)

07/19/2022, 3:36 PM
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
6 Views