Skip to content

[Question]: Complex Pipeline Keys documentation example not working as expected #1926

@jwagon

Description

@jwagon

What are you wanting to achieve?

Hi,

I copied the code from from here:

https://www.pollydocs.org/advanced/dependency-injection.html#complex-pipeline-keys

Exactly into my project, but I kept getting "unable to find a resilience pipeline" exceptions.

What code or approach do you have so far?

When I looked through the code, specifically here:

if (_pipelines.TryGetValue(key, out pipeline))

public override bool TryGetPipeline(TKey key, [NotNullWhen(true)] out ResiliencePipeline? pipeline)
    {
        EnsureNotDisposed();

        if (_pipelines.TryGetValue(key, out pipeline))
        {
            return true;
        }

        if (_builders.TryGetValue(key, out var configure))
        {
            pipeline = GetOrAddPipeline(key, configure);
            return true;
        }

        pipeline = null;
        return false;
    }

It seems like if there is no pipeline defined with the key specified, like in your example:

 ResiliencePipeline instanceA = pipelineProvider.GetPipeline(new MyPipelineKey(ResiliencePipelines.NetSuite, "instance-A"));

It should go ahead and look in the _builders dictionary and build a new pipeline, with that new key, adding it to _pipelines, correct?

But I think (?) the problem is that in your example, PipelineNameComparer.GetHashCode takes the InstanceName into account:

public sealed class PipelineNameComparer : IEqualityComparer<MyPipelineKey>
{
    public bool Equals(MyPipelineKey x, MyPipelineKey y) => x.PipelineName == y.PipelineName;

    public int GetHashCode(MyPipelineKey obj) => (obj.PipelineName, obj.InstanceName).GetHashCode(); // <--- InstanceName is respected here, but maybe it shouldn't be?
}

I think (?) paying attention to InstanceName breaks your example, because when the key search in _builders (above) happens, it won't find any values.

Further, if I change that GetHashCode function to look like this, things work as I'd expect:

    public int GetHashCode(MyPipelineKey obj) => obj.PipelineName.GetHashCode();

I'm not sure what I might be breaking by making that change though, or if I'm just doing something else wrong somewhere.

Thoughts?

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions