Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StructureMap and Orleans #4442

Closed
misha-fm opened this issue Apr 10, 2018 · 11 comments
Closed

StructureMap and Orleans #4442

misha-fm opened this issue Apr 10, 2018 · 11 comments
Assignees
Milestone

Comments

@misha-fm
Copy link

misha-fm commented Apr 10, 2018

Hi! I try to integrate the StructureMap in Orleans (v 2.0.0).

I use the following code:

var builder = new SiloHostBuilder()
    .UseServiceProviderFactory(new StructureMapServiceProviderFactory(new MyRegistry());

StructureMapServiceProviderFactory implements a method:

public Registry CreateBuilder(IServiceCollection services)
{
    var registry = Registry ?? new Registry();
    registry.Populate(services);
    return registry;
}

But call to registry.Populate(services) throws an exception:
"Specified argument was out of the range of valid values.
Parameter name: Orleans.Runtime.StatelessWorkerPlacement must have at least one public constructor to be plugged in by StructureMap".

So, what is the correct way to inject StructureMap into Orleans DI framework?

@sergeybykov sergeybykov added this to the Triage milestone Apr 10, 2018
@xiazen
Copy link
Contributor

xiazen commented Apr 11, 2018

which structure map nugget are you using?

I found this https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection on the internet show me install StructureMap.Microsoft.DependencyInjection and then it has a sample showing how to plug StructureMap into ASP.NET DI abstraction, which is the abstraction Orleans integrated with. This seems to be the suggested way if user want to plug StructureMap into ASP.NET DI , according to aspnet/DependencyInjection#442. abstractions. is this what you are using? your code doesn't look quite like the sample .

@misha-fm
Copy link
Author

Hi, @xiazen! Thank you for your reply!
Yes, I'm using that Nuget (StructureMap.Microsoft.DependencyInjection), and the code for method CreateBuilder I took from there. According to aspnet/DependencyInjection#442, all implementations are populating Orleans services into the container: registry.Populate(services);. But as I wrote in my first post, it throws the exception saying that Orleans.Runtime.StatelessWorkerPlacement must have a public constructor in order to be plugged into the StructureMap container.

Tried to use Autofac in a similar manner:

var autofacSpFactory = new AutofacServiceProviderFactory();
var builder = new SiloHostBuilder()
	.UseLocalhostClustering()
	.UseServiceProviderFactory(autofacSpFactory);

It throws the similar exception: "No accessible constructors were found for the type 'Orleans.Runtime.StatelessWorkerPlacement'."

StackTrace:

   at Autofac.Core.Activators.Reflection.DefaultConstructorFinder.GetDefaultPublicConstructors(Type type)
   at Autofac.Core.Activators.Reflection.ReflectionActivator..ctor(Type implementationType, IConstructorFinder constructorFinder, IConstructorSelector constructorSelector, IEnumerable`1 configuredParameters, IEnumerable`1 configuredProperties)
   at Autofac.Builder.ConcreteReflectionActivatorData.get_Activator()
   at Autofac.Builder.RegistrationBuilder.CreateRegistration[TLimit,TActivatorData,TSingleRegistrationStyle](IRegistrationBuilder`3 builder)
   at Autofac.Builder.RegistrationBuilder.RegisterSingleComponent[TLimit,TActivatorData,TSingleRegistrationStyle](IComponentRegistry cr, IRegistrationBuilder`3 builder)
   at Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry, Boolean excludeDefaultModules)
   at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)
   at Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory.CreateServiceProvider(ContainerBuilder containerBuilder)
   at Orleans.Hosting.ServiceProviderFactoryAdapter`1.BuildServiceProvider(HostBuilderContext context, IServiceCollection services)
   at Orleans.Hosting.ServiceProviderBuilder.BuildServiceProvider(HostBuilderContext context)
   at Orleans.Hosting.SiloHostBuilder.Build()
   at CoreLedger.Orleans.Host.CoreLedgerSilo.<StartSilo>d__8.MoveNext() in C:\Users\Mikhail PC\source\Workspaces\Coreledger Core\Dev\OrleansSilo\src\CoreLedger.Orleans.Host\CoreLedgerSilo.cs:line 183

@misha-fm
Copy link
Author

misha-fm commented Apr 11, 2018

Another option to inject a StructureMap is to provide a factory-lambda in UseServiceProviderFactory method:

var builder = new SiloHostBuilder()
	.UseServiceProviderFactory(services =>
	{
		var ctr = new Container();
		ctr.Populate(services);
		return ctr.GetInstance<IServiceProvider>();
	})

But it throws the same exception after calling to populate method.

@xiazen
Copy link
Contributor

xiazen commented Apr 11, 2018

my wild guess is StructureMap requires everything injected into DI has a public constructor, which StatelessWorkerPlacement doesn't... it wasn't a problem with the native ASP.NET DI solution we uses, so we never run into this issue before. you are more familair with StructureMap than I'm, is this the case? if yes, Orleans may need to made code change for third party DI container like StructureMap to be integrated with Orleans.

@xiazen
Copy link
Contributor

xiazen commented Apr 11, 2018

at the same time I will look into StructureMap. Just you are more familair with it supposeblely, so I figured I can just ask.

@misha-fm
Copy link
Author

yeah, you're right - StructureMap does require that all services it tries to instantiate need public constructors. when I have some spare time, I'll also try to investigate the way how Orleans could support this behaviour

@xiazen
Copy link
Contributor

xiazen commented Apr 12, 2018

submitted pr #4453 which seems to fix the issue.

@misha-fm
Copy link
Author

Great, thank you! Hope this feel fix the issue. Waiting for a new release!

@sergeybykov
Copy link
Contributor

Resolved via #4453.

@stephenlautier
Copy link

@sergeybykov was this released in 2.0.3? From the changes, this seems to be in Microsoft.Orleans.Core.Abstractions, however, there is no 2.0.3 for the specified package; furthermore, I had updated to 2.0.3 but its still not working

@ReubenBond
Copy link
Member

@stephenlautier yes, this should be fixed in 2.0.3 - we made a change to fix it without having to revise the Abstractions package

@ghost ghost locked as resolved and limited conversation to collaborators Sep 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants