Missing a way to register runtime types

Issue #77 duplicate
Michael Buen created an issue

Looping each WCF service contract (interface) and registering the wcf proxies, is not possible through RegisterDelegate<TService> as the TService is determined at compile-time

container.Register<System.ServiceModel.BasicHttpBinding>(DryIoc.Reuse.Singleton, x => x.GetConstructor(new Type[] { }));

foreach(var contractType in typeof(TheServiceContracts.IProductService).Assembly.GetTypes())
{
    var binding = new System.ServiceModel.BasicHttpBinding();
    var endpointAddress = new System.ServiceModel.EndpointAddress("http://localhost:1337/" + contractType.SubString(1) + ".svc"); // removes the first letter I
    _container.RegisterDelegate<contractType>(r => 
        {
            var binding = r.Resolve<System.ServiceModel.BasicHttpBinding>();

            var svcAddress = "http://localhost:1337/" + contractType.Name.Substring(1) + ".svc";
            var address = new System.ServiceModel.EndpointAddress(svcAddress);

            return (contractType) ServiceModelHelper.CreateService(contractType, binding, endpointAddress)
        }, Reuse.InResolutionScope);
}

I've made a solution:

_container.Register<System.ServiceModel.BasicHttpBinding>(DryIoc.Reuse.Singleton, x => x.GetConstructor(new Type[] { }));

foreach (var contractType in typeof(TheServiceContracts.IMemberService).Assembly.GetTypes())
{
    _container.RegisterDelegate(contractType,
        r =>
        {
            var binding = r.Resolve<System.ServiceModel.BasicHttpBinding>();

            var svcAddress = "http://localhost:1337/" + contractType.Name.Substring(1) + ".svc";
            var endpointAddress = new System.ServiceModel.EndpointAddress(svcAddress);


            return ServiceModelHelper.CreateService(contractType, new System.ServiceModel.BasicHttpBinding(), endpointAddress);
        }, Reuse.InResolutionScope);

}   

I detailed it here: http://www.ienablemuch.com/2014/10/registering-runtime-types-on-dryioc.html

Comments (5)

  1. Maksim Volkau repo owner

    Hi Michael,

    The API for registering delegates and instances with runtime type is available in upcoming DryIoc v2.0 (currently in prerelease NuGet packages or dev source branch). It was added in scope of: #28: Add RegisterInstance method that accepts a System.Type parameter.

    So you can write:

    container.RegisterDelegate(contractType, r => ServiceModelHelper.CreateService(...), Reuse.Singleton);
    

    If you need to provide custom parameter value, e.g. endpointAddress, there is other way in v2.0:

    container.Register(contractType, concreteServiceType, setup: Setup.With(
        parameters: Parameters.All.And("endpointAddress", r => GetEndpointAddressFromContractType(r.ServiceType))));
    

    Check, how "r" (Request) parameter is used to get access to context.

    The API with parameters is not so elegant at the moment, so I appreciate any suggestions. But it is tested and can be used as of now.

    Hope it helps, dadhi

  2. Michael Buen reporter

    Thanks Maksim! didn't see it's resolved now in 2.0 :-)

    I think I can't use this on WCF services registration:

    container.Register(contractType, concreteServiceType, setup: Setup.With(
        parameters: Parameters.All.And("endpointAddress", r => GetEndpointAddressFromContractType(r.ServiceType))));
    

    concreteServiceType paramater can't be determined. During registration, there's no concrete type yet for the interfaces, as concrete types are determined only when the delegate executes. Or perhaps the interfaces are pre-assigned with concrete objects during IoC registration with that? I'll check that functionality as soon I got to use 2.0

    By any chance, is interception functionality on the roadmap?

  3. Log in to comment