orleans icon indicating copy to clipboard operation
orleans copied to clipboard

StructureMap and Orleans

Open misha-fm opened this issue 8 years ago • 11 comments

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?

misha-fm avatar Apr 10 '18 12:04 misha-fm

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 https://github.com/aspnet/DependencyInjection/issues/442. abstractions. is this what you are using? your code doesn't look quite like the sample .

xiazen avatar Apr 11 '18 01:04 xiazen

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 https://github.com/aspnet/DependencyInjection/issues/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 avatar Apr 11 '18 08:04 misha-fm

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.

misha-fm avatar Apr 11 '18 09:04 misha-fm

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 avatar Apr 11 '18 21:04 xiazen

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

xiazen avatar Apr 11 '18 21:04 xiazen

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

misha-fm avatar Apr 11 '18 23:04 misha-fm

submitted pr #4453 which seems to fix the issue.

xiazen avatar Apr 12 '18 00:04 xiazen

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

misha-fm avatar Apr 12 '18 07:04 misha-fm

Resolved via #4453.

sergeybykov avatar Apr 16 '18 17:04 sergeybykov

@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

stephenlautier avatar May 16 '18 13:05 stephenlautier

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

ReubenBond avatar May 17 '18 01:05 ReubenBond