Grace: Exception with open generics

I’m trying to export a class accordingly:

c.ExportFactory(() => new SyncService(new JsonShipmentService(scope.Locate(typeof(IShipmentRepository<,>), withKey: "json") as Data.Json.Repositories.ShipmentRepository),
                                      new SqlShipmentService( scope.Locate(typeof(IShipmentRepository<,>), withKey: "sql")  as ShipmentRepository)))
 .Lifestyle.Singleton();

But am getting the following exception:

image

I’m not sure how to solve this…

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 33 (33 by maintainers)

Most upvoted comments

@ipjohnson Great initiative, really appreciate it, Ian.

Oh very good! Glad it all worked out. If you have any other questions submit an issue.

I’ll create a work item for myself to throw a better exception on registration to avoid this problem in the future.

This is actually something I should tighten up on the fluent registration and throw an exception on registration.

This evening I’ll open a defect and look to make it obvious upfront what the error is. The exception that’s being thrown currently is from dotnet because it’s trying to compile a function with an open interface.

I was actually surprised it worked as well but since the type isn’t trying to be closed and the container is a dictionary under the covers it looked up the type, found it and created an instance.

I can kinda get it to work by changing the configuration to


            container.Configure(c =>
            {
                c.Export<DummyService>().WithCtorParam(() => "dummy1").Named("dummy").AsKeyed(typeof(IDummyService<>), "dummy").Lifestyle.SingletonPerRequest();
                c.Export<DummyService2>().WithCtorParam<object>().Named("dummyService").LocateWithKey("dummy");
            });

That said I keep coming back to the fact that registering a closed implementation by an open interface, then locating by an open interface is not the right way to do this.

Normally when you register something with an open interface the type being export is an open type definition. What you’re doing is registering a closed implementation by and open interface and then locating it by hand, if you were to locate the type as a dependency it would fail because the exported type is not open.

Why not register the closed implementation by the closed interface and then locate using a closed interface? All your troubles come from the fact that you’re trying to use an open interface on a closed type.

One way or another I’m sure we can get this squared away. I was more just trying to get the lay of the land because usually when you register an open generic you then locate with a closed type.

Is your json service a generic implementation or closed implementation?