Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • May 17 16:58
    hisuwh commented #338
  • May 17 16:43
    jeremydmiller commented #338
  • May 17 16:40
    hisuwh commented #338
  • May 17 16:34
    jeremydmiller commented #338
  • May 17 16:26
    hisuwh commented #338
  • May 10 22:21
    davidfowl commented #41
  • May 10 15:43
    zcacmjw opened #339
  • May 10 10:59
    jeremydmiller commented #41
  • May 10 07:46
    davidfowl commented #41
  • May 09 20:56

    github-actions[bot] on v6.1.6

    (compare)

  • May 09 20:44

    jeremydmiller on master

    tweaking the behavior of ValueT… (compare)

  • May 09 19:59

    github-actions[bot] on v6.1.5

    (compare)

  • May 09 19:52
    jeremydmiller closed #335
  • May 09 19:52
    jeremydmiller commented #335
  • May 09 19:50

    jeremydmiller on master

    support in the code generation … (compare)

  • May 09 19:38
    jeremydmiller closed #41
  • May 09 19:38
    jeremydmiller commented #41
  • May 09 19:37
    jeremydmiller commented #337
  • May 09 19:36
    jeremydmiller closed #338
  • May 09 19:36
    jeremydmiller commented #338
Michael Vastarelli
@mvastarelli
Ok. The container is pretty automagic though. It was picking up the class attributes (and even the registrations) without ever officially registering anything. That's a really nifty feature. I can't count the number of times CW crashed because somehow a dependency got missed during the bootstrap process.
Jeremy D. Miller
@jeremydmiller
Sorry, I misspoke. I never implemented anything for property level attributes. Thought I had. Nevermind, so do what you were saying:)
Michael Vastarelli
@mvastarelli
No worries. This solution is still very clean and easy to test.
Thanks again for your help!!
Jeremy D. Miller
@jeremydmiller
:thumbsup: Glad it worked out, thanks for the kind words
Ben Parsons
@9ParsonsB

Hi, I have a service with an optional parameter in the constructor like so:

public Service(IHostEnvironment environment) { ... }
public Service(IHostEnvironment environment, X509Certificate2? certificate = null) : this(environment) { ... }

I want Lamar to pick either the first constructor, or the second constructor with a null value for the second parameter. However, Lamar keeps trying to create an X509Certificate with null parameters which throws and it does not try auto-wiring in a different way.
How can I configure auto-wiring or force the use of a specific/preferred constructor?

Jeremy D. Miller
@jeremydmiller
@9ParsonsB My recommendation is to not do that if you can help that and only ever have one constructor so there's never any confusion about what the container is doing. That being said, here's the options to explicitly work around the constructor selection: https://jasperfx.github.io/lamar/guide/ioc/registration/constructor-selection.html
3 replies
Michael Vastarelli
@mvastarelli
Hey @jeremydmiller . Quick Lamar question for you. I'm working on writing a policy that can selectively do property injection for an object based on a convention. I know this can be accomplished using instance.AddInline(new LambdaIndstance(...)) but is there a simpler way to do this without having to manually call GetInstance() manually?
Michael Vastarelli
@mvastarelli
Is it possible to use factories without generics? For example. Instead of For<ISessionFactory>().Use( c => { ... }) write For(typeof(ISessionFactory)).Use(c => { ... }) ?
Mark Warpool
@CodingGorilla
@mvastarelli Yea, there's an overload that takes a Type as a parameter
Michael Vastarelli
@mvastarelli
@CodingGorilla I'm looking for something that can take a delegate though. I noticed the same thing for OnCreationForAll(). This method only appears to be available if I use the generic version of For<T>() but is absent in For(Type t).
Mark Warpool
@CodingGorilla
I always assumed they were just simple overloads, but looking at the code, that was a bad assumption. One is a InstanceExpression and one is a DescriptorExpression, the latter does not have the overload you're looking for. I'm not sure if there's reasoning behind that or not.
Michael Vastarelli
@mvastarelli
At first I thought I may have been missing a namespace import, but I was pretty surprised to see that the API for non-generics is pretty limited opposed to generics.
Maybe @jeremydmiller can shed some light on this.
Jeremy D. Miller
@jeremydmiller
@mvastarelli You can create a policy that adds an activator based on open generics if you want. Not a common thing to do, and the activator interceptor stuff was a recent addition to Lamar.
And this'd be a good pull request to add a simple syntax helper to the FI for that. That was part of StructureMap. If you're looking for an open generic type Lambda registration, that just doesn't make any sense. There is a way to do that that isn't well documented though.
chunty
@chunty
I'm trying to do some integration testing on a .net core 6 project and I need to override a service DI config. Because you're supposed to replace the use of ConfigureServices with ConfigureContainer(ServiceRegistry services) using builder.ConfigureTestServices() ala https://stackoverflow.com/a/53736814 doesn't work because it gets called BEFORE ConfigureContainer there is a ConfigureTestContainer which I was hoping to use to but its never triggered/called as far I can tell - any suggestions?
Jeremy D. Miller
@jeremydmiller
7 replies
chunty
@chunty
Wow thanks - I wish I'd asked 4 hours ago!
Any idea why I might get Could not load type 'Lamar.Microsoft.DependencyInjection.WebHostBuilderExtensions' from assembly 'Lamar.Microsoft.DependencyInjection, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
Jeremy D. Miller
@jeremydmiller
Mismatched assembly versions one way or another
Dustin Mikusko
@Dustin-Mikusko

Howdy howdy! I'm looking for some help with hopefully a simple problem. I've got a simple console app in .NET 6, trying to set up a Container and configure service registration and keep getting this error:

System.InvalidOperationException: 'Detected some kind of bi-directional dependency while trying to discover and plan a missing service registration. Examining types: System.Text.Json.JsonSerializerOptions'

I've removed all references to System.Text through my app and it's still saying this. Any thoughts? Thanks in advance!

Jeremy D. Miller
@jeremydmiller
System.Text.Json. It should be telling you what registration is at the top of the bi-directional problem, that might help. Should also show you the full path of top to bottom dependencies until it hit that issue. Can you show more of the stacktrace?
Dustin Mikusko
@Dustin-Mikusko
   at Lamar.ServiceGraph.TryToCreateMissingFamily(Type serviceType)
   at Lamar.ServiceGraph.addMissingFamily(Type serviceType)
   at Lamar.ServiceGraph.ResolveFamily(Type serviceType)
   at Lamar.ServiceGraph.FindDefault(Type serviceType)
   at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass35_0.<couldBuild>b__0(ParameterInfo p)
   at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
   at Lamar.IoC.Instances.ConstructorInstance.couldBuild(ConstructorInfo ctor, ServiceGraph services)
   at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass36_0.<DetermineConstructor>b__1(ConstructorInfo x)
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at Lamar.IoC.Instances.ConstructorInstance.DetermineConstructor(ServiceGraph services, String& message)
   at Lamar.ServiceGraph.CouldBuild(Type concreteType, String& message)
   at Lamar.ConcreteFamilyPolicy.Build(Type type, ServiceGraph serviceGraph)
   at Lamar.ServiceGraph.<>c__DisplayClass66_0.<TryToCreateMissingFamily>b__1(IFamilyPolicy x)
   at LamarCodeGeneration.Util.EnumerableExtensions.FirstValue[TItem,TReturn](IEnumerable`1 enumerable, Func`2 func)
   at Lamar.ServiceGraph.TryToCreateMissingFamily(Type serviceType)
   at Lamar.ServiceGraph.addMissingFamily(Type serviceType)
   at Lamar.ServiceGraph.ResolveFamily(Type serviceType)
   at Lamar.ServiceGraph.FindDefault(Type serviceType)
   at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass35_0.<couldBuild>b__0(ParameterInfo p)
   at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
   at Lamar.IoC.Instances.ConstructorInstance.couldBuild(ConstructorInfo ctor, ServiceGraph services)
   at Lamar.IoC.Instances.ConstructorInstance.<>c__DisplayClass36_0.<DetermineConstructor>b__1(ConstructorInfo x)
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at Lamar.IoC.Instances.ConstructorInstance.DetermineConstructor(ServiceGraph services, String& message)
   at Lamar.IoC.Instances.ConstructorInstance.createPlan(ServiceGraph services)
   at Lamar.IoC.Instances.Instance.CreatePlan(ServiceGraph services)
   at Lamar.ServiceGraph.planResolutionStrategies()
   at Lamar.ServiceGraph.buildOutMissingResolvers()
   at LamarCodeGeneration.Util.PerfTimer.Record(String text, Action action)
   at Lamar.ServiceGraph.Initialize(PerfTimer timer)
   at Lamar.IoC.Scope..ctor(IServiceCollection services, PerfTimer timer)
   at Lamar.Container..ctor(IServiceCollection services)
   at Lamar.Container..ctor(Action`1 configuration)
Jeremy D. Miller
@jeremydmiller
Okay, I meant the part of the exception where it shows you the dependency trail:)
Dustin Mikusko
@Dustin-Mikusko
image.png
Oops! My apologies. I'm still new to all of this. Could you guide me on where to find that please?
Jeremy D. Miller
@jeremydmiller
Should be in the exception message. Lamar should be showing you a numbered list that describes the dependency path from one type back to itself
Bailey Ammons
@bammons
Is it possible to add an interceptor to all types in a registry via a registry convention?
Jeremy D. Miller
@jeremydmiller
You could use a registry convention to programmatically add interceptors to ConstructorInstance objects you're building on the fly. There's also IInstancePolicy that would be evaluated at container activation time you could also use. What are you wanting to do?
Bailey Ammons
@bammons
We're rearchitecting and upgrading right now and a part of that is going from structure map to lamar. It looks like we're attempting to auto validate with fluent on our business service method inputs. I know it seems unnecessary, but I'm trying not to rock the boat too hard until after we've upgraded.
Jeremy D. Miller
@jeremydmiller
How does StructureMap in your current usage "know" to apply the decorators/activators today?
Lamar has much less runtime flexibility than SM did, but actually has many more convention capabilities at config time. Just trying to figure out which would be most helpful for you here
Bailey Ammons
@bammons
Started to describe it, but I figure it'd be easier to just put the code in here
public override void ScanTypes(TypeSet types, Registry registry)
        {
            base.ScanTypes(types, registry);

            foreach (var type in types.FindTypes(TypeClassification.Concretes).Where(t => t.Implements(typeof(IApplicationService))))
            {
                var pluginType = FindPluginType(type);

                var delegateType = typeof(Func<,>).MakeGenericType(pluginType, pluginType);

                // Create FuncInterceptor class with generic argument +
                var d1 = typeof(FuncInterceptor<>);

                Type[] typeArgs = { pluginType };

                var interceptorType = d1.MakeGenericType(typeArgs);
                // -

                // Create lambda expression for passing it to the FuncInterceptor constructor +
                var arg = Expression.Parameter(pluginType, "x");
                var method = GetType().GetMethod("GetProxy").MakeGenericMethod(pluginType);

                // Crate method calling expression
                var methodCall = Expression.Call(method, arg);

                // Create the lambda expression
                var lambda = Expression.Lambda(delegateType, methodCall, arg);
                // -

                // Create instance of the FuncInterceptor
                var interceptor = Activator.CreateInstance(interceptorType, lambda, "");

                registry.For(pluginType).Use(type).InterceptWith(interceptor as IInterceptor);
            }
        }

        public static T GetProxy<T>(object service)
        {
            var validationInterceptor = new ValidationInterceptor();
            IoC.BuildUp(validationInterceptor);
            //disabled for now
            //var result = ProxyGen.CreateInterfaceProxyWithTarget(typeof(T), service, validationInterceptor);
            var result = ProxyGen.CreateInterfaceProxyWithTarget(typeof(T), service, new MiniProfilerInterceptor());
            return (T)result;
        }
So we're trying to add the interceptor to every plugin type in the registry and the majority of those are brought in through scans.
Bailey Ammons
@bammons
Apologies for just pasting some shit code at you lol. We ended up refactoring all of that into a StructureMap.DynamicProxyInterceptor. I noticed that there was a lamar implementation for that as well, but it hasn't been maintained. Is there an alternative way to do something similar?
Jeremy D. Miller
@jeremydmiller
Nothing in the box. It's not something I use myself, and hasn't been a priority. I take pull requests though!
Bailey Ammons
@bammons
I'll check your contrib docs and start the conversation
Jeremy D. Miller
@jeremydmiller
Okay
Bailey Ammons
@bammons
Is it possible to get a stack overflow if there is too much to build up off a single injection?
Jeremy D. Miller
@jeremydmiller
You’d fail in the “planning” phase before it even attempted to build, and in that case you’d get a bi-directional dependency problem w/o doing a StackOverflowException. Why?
Bailey Ammons
@bammons
So we had a .NET 4.8 Topshelf app that used structuremap and we've upgraded it to .NET 6 with worker services and lamar. In the older version we're able to inject all of the same dependencies fine through structure map, but doing the same thing in lamar is creating a stack overflow and I see InstanceConstructorFrame.WriteExpressions() getting called repeatedly until it blows up.
image.png
Jeremy D. Miller
@jeremydmiller
Okay, by itself, that isn't terribly helpful. Any way you can see what the argument type it is in the stack that's doing that? This has popped up a couple time, but I don't remember the exact cause. It's some kind of ASP.Net Core related type that's a composite type with itself as a detected constructor dependency
Bailey Ammons
@bammons
Sorry about that. I ended up setting the default lifetime to scoped and that resolved my issue. Also, I didn't see any contributing docs anywhere. It cool if I just submit a PR? It'll be a bit before it happens. I need to clean everything up and write tests.
Jeremy D. Miller
@jeremydmiller
What do you mean by setting the default lifetime to scoped? And yes to PRs, but nothing gets in without tests is all. PR for what though?
Bailey Ammons
@bammons
within scan.WithDefaultConventions(ServiceLifetime.Scoped);
And to make a dynamic interceptor policy. We have all of our business service interfaces inheriting from IApplicationService, but InterceptAll doesn't go deep enough to just attach to it and trigger the intercept. We would like to be able to do so and pass in intercept behaviors.
Jeremy D. Miller
@jeremydmiller
Okay, sounds good. Can you also add some documentation to the website for that? It's all markdown files in the source code. YOu can even get to it from the "suggest changes" links in the web site
Bailey Ammons
@bammons
For sure