Skip to content

Add PactNet.Extensions.Grpc package for grpc plugin support - #548

Closed
max-lancaster wants to merge 13 commits into
pact-foundation:masterfrom
max-lancaster:master
Closed

max-lancaster wants to merge 13 commits into
pact-foundation:masterfrom
max-lancaster:master

Conversation

@max-lancaster

@max-lancaster max-lancaster commented Sep 4, 2025 •

Copy link
Copy Markdown

Stacked on #551. Everything except the last commit belongs to #551 and will drop out of this diff when that PR merges. The remaining commit adds the PactNet.Extensions.Grpc package, which is the reference example of a wrapper library built on the raw plugin API described in docs/plugin-interactions.md.

I have read through issue #492 thoroughly and examined the source code and feel that I have a decent understanding of what's involved in adding plugin support, specifically for Grpc. I was able to take @YOU54F's example and create this PR to get some sample Grpc tests working.

I have extracted a PactNet.Interop package as @adamrodger suggested in Issue #492 . This was not a breaking change since all the classes in this namespace were internal, PactNet now references this new package and none of the pre-existing interface methods have changed. Extracting this package made it possible to create bare-bones Grpc plugin tests and is effectively a very "raw" way of using any plugin. Perhaps a next step would be to wrap the bare-bones methods in a more fluent "raw" interface in a plugin package similar to implementations in other languages as @mefellows suggested.

Based on above "raw" tests I was able to refactor and extract shared functionality to create a new PactNet.Extensions.Grpc library that provides a fluent interface similar to the interfaces for http / rest and asynchronous messaging interactions.

An alternative approach here would be to just add <InternalsVisibleTo Include="PactNet.Extensions.Grpc" /> to the PactNet.csproj file but this allows integration of other plugins in repos outside this one so it depends on whether you want to explicitly allow that or not.

@max-lancaster max-lancaster changed the title Add Preliminary Grpc Sample Extract PactNet.Interop package with Grpc Sample Sep 4, 2025
@max-lancaster
max-lancaster force-pushed the master branch 2 times, most recently from c34af19 to 990939e Compare September 4, 2025 21:50
@max-lancaster
max-lancaster marked this pull request as ready for review September 4, 2025 21:57
@max-lancaster
max-lancaster marked this pull request as draft September 10, 2025 21:58
@max-lancaster
max-lancaster force-pushed the master branch 2 times, most recently from a827783 to 7cbeac4 Compare September 16, 2025 03:10
@max-lancaster max-lancaster changed the title Extract PactNet.Interop package with Grpc Sample Extract PactNet.Interop package and add Grpc Support Sep 18, 2025
@max-lancaster
max-lancaster marked this pull request as ready for review September 18, 2025 15:58
@max-lancaster
max-lancaster force-pushed the master branch 2 times, most recently from d4644b3 to 079935c Compare September 19, 2025 20:42
@adamrodger

Copy link
Copy Markdown
Contributor

Many thanks for the PR - I can see you've put a lot of work into this!

I think a good path forward would be to create some kind of framework for plugins to be able to hook into existing PactNet internals without making those internals themselves public. A few examples of where the approach in this PR isn't desireable:

  • The PactNet.Interop package has to have an InternalsVisibleTo("PactNet.Extensions.Grpc"). That means that developing a plugin wrapper effectively means that it has to be mainlined, whereas I'd like the option to allow other people to add plugin wrappers without mainlining
  • The GrpcPactBuilder has to duplicate a lot of the functionality that already exists internally, for example the pattern around ensuring the mock server is started and then displaying the log messages afterwards. Plugin wrapper authors shouldn't need to do this, and PactNet itself should be the single place where things like log formatting are done so that the entire ecosystem has a consistent presentation.
  • New classes have to be created, such as PactFileWriter, and these are either only accessible to mainlined plugin wrappers (undesireable as explained above) or need to be made public. If they're public then that creates a whole extra burden for us in ensuring they're totally safe to use at all times without corruping the FFI state, given we lose control of when they're being called.
  • You can update the Interop package without updating the others - this means we can't verify compatibility as and when FFI features change.

For those reasons and more, I'd prefer to see some kind of framework created, probably as part of PactNet or maybe PactNet.Abstractions whereby plugin authors can register their plugins and add interactions to pacts (like the UsePlugin and NewSyncMessageInteraction methods you've added, which are a good addition) but cope with other internal details that plugin wrapper authors shouldn't need to worry about - writing the pact, starting the mock server, checking the response codes, displaying mismatches, freeing resources, etc.

To me, success would look like:

  • It's possible to have a totally separate repository which can add a plugin wrapper, without any changes to PactNet itself. This repository would also contain the documentation and samples for how to get it working.
  • It's possible to use a more "raw" plugin system in case a wrapper project doesn't exist, so that emerging new plugins don't need to wait until a nice wrapper is mainlined.
  • We'd have some kind of documentation/guidance/workshop to show how a plugin wrapper can be written.

I think this is a great step to achieving that, and congratulations to you for this being the first time that we've been able to run plugins in PactNet. It's been a long time coming 😁

@adamrodger adamrodger mentioned this pull request Sep 20, 2025
@max-lancaster max-lancaster changed the title Extract PactNet.Interop package and add Grpc Support Add PactNet.Extensions.Grpc package for grpc plugin support Oct 8, 2025
@max-lancaster

max-lancaster commented Oct 8, 2025 •

Copy link
Copy Markdown
Author
  • It's possible to have a totally separate repository which can add a plugin wrapper, without any changes to PactNet itself. This repository would also contain the documentation and samples for how to get it working.

@adamrodger I rebased this PR on top of PR #551 and made appropriate changes here to demonstrate that this is possible now. With those changes we can now create the new PactNet.Extensions.Grpc library in this repo or another repo as you prefer.

The only changes in this PR that are not in PR #551 are in aba5d3d which only adds the new library and associated tests.

  • It's possible to use a more "raw" plugin system in case a wrapper project doesn't exist, so that emerging new plugins don't need to wait until a nice wrapper is mainlined.

This was achieved in PR #551 for synchronous plugin interactions, I was thinking we could add asynchronous interactions in a separate PR.

  • We'd have some kind of documentation/guidance/workshop to show how a plugin wrapper can be written.

I'll leave it up to you how you want to go about doing this but would be willing to chip in if you want to provide some guidance.

@timo-reymann

Copy link
Copy Markdown

@max-lancaster thanks for the effort already put into this. It would really unblock my work integrating Pact with our internal gRPC services. Happy to provide early feedback on usage etc. if needed - feel free to ping me.

@jorgalad

Copy link
Copy Markdown

@max-lancaster thank you for working on this, from my side also happy to provide early testing

@mefellows

Copy link
Copy Markdown
Member

@adamrodger what's required to move forward with this? It looks like a great and substantial change for the ecosystem!

@adamrodger

Copy link
Copy Markdown
Contributor

@max-lancaster @mefellows the first stage is getting the other mentioned PR merged so that we can create a generic plugin interface, and then adding a gRPC plugin would be the first to adopt it (and prove that we'd made it appropriately generic hopefully also).

I'm having a look at that other PR now though.

@jorgalad

Copy link
Copy Markdown

Hi @mefellows and @adamrodger, what's the current startus of this? We're currently trying to plan our involvement and how we'd like to continue with gRPC contract tests, would love to get an update if possible

@mefellows

Copy link
Copy Markdown
Member

It looks like #551 is in decent shape now, which I think is the crucial change required to support gRPC (and other plugins). Let's see what Adam comes back with.

This enables creating a nicer API to expose publicly for use by plugin
wrappers.

See: pact-foundation#492
max-lancaster and others added 12 commits September 14, 2026 11:48
…ilder

Setting the plugin contents completes a synchronous plugin interaction, so
WithContent now returns void instead of the same builder. The plugin splits
one contents document into the request and its responses itself: a second
call would replace the request but append another response, which is why the
builder no longer allows it. Provider states stay chainable via Given.

Adds unit tests for the plugin request and pact builders and a docs page for
the raw plugin API, linked from the README.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@YOU54F YOU54F left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been testing this out in a local fork against a few projects, and it is a nice DSL.

I agree that we should split it out into a separate repo, especially with the newly rewritten [avro plugin](https://github.com/hominux/pact-avro-plugin, and others, with the ability to add them into pact-net, hopefully they will grow in numbers.

If we do retain in the same repo, we will have to sort the release workflow out to ensure we can release the packages independently.

I would be happy splitting out into a new repo, my only question is whether we wanted one exclusively per plugin, or if a mono repo with multiple plugin wrappers would allow us to support the community building new plugins and avoid repo sprawl.

Couple of outstanding points to get CI working in the stacked PR.

Would you like to create a repo and then transfer it into Pact-Foundation, or would you like me to create one and invite you in?

/// <returns>Pact builder</returns>
public static IGrpcPactBuilderV4 WithGrpcInteractions(this IPactV4 pact, int? port = null, IPAddress host = IPAddress.Loopback)
{
var pluginBuilder = pact.WithSynchronousPluginInteractions("protobuf", "0.4.0", transport: "grpc", port, host);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend not hard coding the plugin version here.

If it is null, the framework will use the latest available version from the repository index

https://github.com/pact-foundation/pact-plugins/blob/main/repository/repository.index

I would allow for the user to be able able to optionally set it.

That way we don't need a new release for a new version of the plugin

@max-lancaster

Copy link
Copy Markdown
Author

Would you like to create a repo and then transfer it into Pact-Foundation, or would you like me to create one and invite you in?

I think it makes sense for you to create one and invite me if it's long term home is going to be pact foundation @YOU54F

@YOU54F

YOU54F commented Oct 9, 2026

Copy link
Copy Markdown
Member

I’ve created https://github.com/pact-foundation/pact-net-extensions

let me know how you want to proceed, I am happy to scaff out the repo, and you can fork and contribute the first plugin wrapper, or I can invite you directly as a maintainer. i am easy here, i have some time
i can dedicate today

Following pact-nets structure as a basis, with the ability to support releases of multiple wrappers would probably be the route i would go down but open to ideas

@max-lancaster

Copy link
Copy Markdown
Author

Following pact-nets structure as a basis, with the ability to support releases of multiple wrappers

Yes I think a scaffolded monorepo makes sense for these simple wrapper libraries - that setup will make it easier for future contributors to add new libraries.

If you can do that I'll contribute the first library as a PR. I don't really see the need to make me a full maintainer at this time.

@YOU54F

YOU54F commented Oct 9, 2026

Copy link
Copy Markdown
Member

no worries chap! I knocked up something together earlier today and did an avro wrapper, the pattern you set worked nicely for enabling plugins for http and async too.

I'll get it all pushed up along with changes for pact-net on Monday. i was able to pull along all your commit attribution for the wrapper library so that's all sorted. thanks Max.

Happy to rebase from any updated commits, or pick up last tasks on the other pr.

@YOU54F

YOU54F commented Oct 16, 2026

Copy link
Copy Markdown
Member

Going to close this off now, as the contributions are now in a sister repository

See this #551 (comment) for more detail, on your associated plugin interface pull request

Sample grpc project commit: pact-foundation/pact-net-extensions@cd94567
PactNet.Extensions.Grpc package commit: pact-foundation/pact-net-extensions@a6e8311

Really appreciate this contribution, and hope we can get #551 landed soon, as once that is in place, we can get the extension library released, and follow up with http and async plugin interfaces

@YOU54F YOU54F closed this Oct 16, 2026
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

Successfully merging this pull request may close these issues.

6 participants