Skip to content

MessagePipe.Interprocess - Scope issue on running in none production environment | with scope validation (ValidateScopes) #149

@gszdev

Description

@gszdev

Hi,

I'm evaluating the MessagePipe.Interprocess using the NamedPipe.
I'm using the IDistributedSubscriber within a scoped Service and get the following exception if running inside none production environment (DOTNET_ENVIRONMENT != "Production") like development or staging.
Running the same code in a production enviornment works, but running it in an devleopment environment it fails.
This is because scope validation (ValidateScopes) not active and not running in production enviornments.

Exception details:

System.AggregateException
  HResult=0x80131500
  Message=Some services are not able to be constructed
  Source=Microsoft.Extensions.DependencyInjection
  StackTrace:
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Demo.IpcTests.dotNetFxConsoleApp.Program.<Main>d__0.MoveNext()

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidOperationException: 
Error while validating the service descriptor 'ServiceType: Demo.IpcTests.Services.IServerService Lifetime: Scoped ImplementationType: Demo.IpcTests.Services.ServerService': 
Cannot consume scoped service 'MessagePipe.Interprocess.Workers.NamedPipeWorker' from singleton 'MessagePipe.IDistributedSubscriber`2[System.String,Demo.IpcTests.Data.SampleData]'.

Inner Exception 2:
InvalidOperationException: Cannot consume scoped service 'MessagePipe.Interprocess.Workers.NamedPipeWorker' from singleton 'MessagePipe.IDistributedSubscriber`2[System.String,Demo.IpcTests.Data.SampleData]'.

I assume the following is causing the issue. Inside NamedPipeDistributedPublisherSubscribercs (lifetime: singleton) a NamePipeWoker (lifetime: scoped) is injected:

public NamedPipeDistributedSubscriber(NamedPipeWorker worker, MessagePipeInterprocessNamedPipeOptions options, IAsyncSubscriber<IInterprocessKey, IInterprocessValue> subscriberCore, FilterAttachedMessageHandlerFactory syncHandlerFactory, FilterAttachedAsyncMessageHandlerFactory asyncHandlerFactory)

--
Changing the InstanceLifetime from Scoped to Singleton for NamedPipeInterprocess is workaround, maybe:
.AddNamedPipeInterprocess(Demo.IpcTests.Services.Constants.PipeNames.DotNetPipe, namedPipeOptions => {namedPipeOptions.InstanceLifetime = InstanceLifetime.Singleton;
})

--

Does someone know how to solve this issue? Or I'm doing some wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions