GithubHelp home page GithubHelp logo

Comments (2)

tillig avatar tillig commented on August 16, 2024 1

I think we could definitely improve on the examples in the docs. Looks like it's pretty light in there.

For your immediate issue, you'll notice in the CombinationalWeakTypedAttributeScenarioTestFixture that the thing being resolved is an interface, not a class/attribute:

[Fact]
public void Validate_wireup_of_generic_attributes_to_strongly_typed_metadata_on_resolve()
{
    // arrange
    var builder = new ContainerBuilder();
    builder.RegisterMetadataRegistrationSources();

    builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
        .As<ICombinationalWeakTypedScenario>()
        .WithAttributedMetadata();

    // act
    var items = builder.Build().Resolve<IEnumerable<Lazy<ICombinationalWeakTypedScenario, ICombinationalWeakTypedScenarioMetadata>>>();

    // assert
    Assert.Single(items);
    Assert.Single(items.Where(p => p.Metadata.Name == "Hello"));
    Assert.Single(items.Where(p => p.Metadata.Age == 42));
}

...and that interface looks like...

public interface ICombinationalWeakTypedScenarioMetadata
{
    int Age { get; }

    string Name { get; }
}

The interface is pretty important because it means Autofac can use the magic of MEF to get a strongly typed view over the metadata.

If you use a class rather than an interface, the metadata registration sources won't handle it and it falls back to the default Autofac view provider which is where you're getting that exception. The default provider basically calls new on the type and tries to set properties, which is why adding the parameterless constructor fixes things up.

But, I agree, we could probably clean up the metadata docs a bit to bring it together better. There's nothing in there about metadata views and I think in general the doc could probably just be tightened up a bit to tie the concepts all together. Trying to pick up various bits from each section and put them all together is a challenge.

from documentation.

hawkerm avatar hawkerm commented on August 16, 2024

Thanks for the info @tillig, that's helpful to know. The error messages from autofac or elsewhere in the stack are sometimes a bit obtuse on the underlying issue or solution, so seeing it laid out in your example is helpful.

I was definitely trying to avoid having to create an interface for the metadata class in my scenario if possible, so that's probably leading me to some of the troubles. The parameterless constructor helped in my test scenario, but didn't seem to work in my practical example using a similar setup for some reason. Still wasn't able to figure that out, depending on tweaks I did it seemed to either look for the dictionary based constructor or throw some other exception outside of autofac I believe.

from documentation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.