Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. .NetCore dependency injection and optional params on assembly scanning

.NetCore dependency injection and optional params on assembly scanning

Scheduled Pinned Locked Moved C#
csharpdockerhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14874720
    wrote on last edited by
    #1

    With autofac I'm to add a parameter to the constructor and even more, the parameter can be optional

    //autofac
    Builder
    .RegisterAssemblyTypes(assemblies)
    .Where(t => typeof(T).IsAssignableFrom(t))
    .SingleInstance()
    .AsSelf()
    .WithParameter(new ResolvedParameter(
    (pi, ctx) => pi.ParameterType == typeof(IMyService),
    (pi, ctx) => ctx.ResolveOptional() ));

    Could you help do this with the default MS dependency container and Scrutor? What should I add here:

    //MS Dependency Container

    Builder
    .Scan(s =>
    s.FromAssemblies(assemblies)
    .AddClasses(c => c.AssignableTo(typeof(T)))
    .AsSelf()
    .WithTransientLifetime() );

    Thank you in advance for help and answer

    Richard DeemingR 1 Reply Last reply
    0
    • M Member_14874720

      With autofac I'm to add a parameter to the constructor and even more, the parameter can be optional

      //autofac
      Builder
      .RegisterAssemblyTypes(assemblies)
      .Where(t => typeof(T).IsAssignableFrom(t))
      .SingleInstance()
      .AsSelf()
      .WithParameter(new ResolvedParameter(
      (pi, ctx) => pi.ParameterType == typeof(IMyService),
      (pi, ctx) => ctx.ResolveOptional() ));

      Could you help do this with the default MS dependency container and Scrutor? What should I add here:

      //MS Dependency Container

      Builder
      .Scan(s =>
      s.FromAssemblies(assemblies)
      .AddClasses(c => c.AssignableTo(typeof(T)))
      .AsSelf()
      .WithTransientLifetime() );

      Thank you in advance for help and answer

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      If the constructor parameter is optional, it should just work:

      public class Foo
      {
      public IBar Bar { get; }
      public IBaz Baz { get; }
      public Foo(IBar bar = default, IBaz baz = default) (Bar, Baz) = (bar, baz);
      }

      ...

      IServiceCollection services = new ServiceCollection();
      services.AddTransient<IBaz, Baz>();
      services.AddTransient<Foo>();

      IServiceProvider provider = services.BuildServiceProvider();
      Foo foo = provider.GetRequiredService<Foo>();

      Debug.Assert(foo != null);
      Debug.Assert(foo.Baz != null);
      Debug.Assert(foo.Bar == null);

      It doesn't matter whether you register the services manually or use Scrutor.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups