Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type must not be ByRef (Parameter 'type') exception when constructor contains in parameters #1226

Closed
cd21h opened this issue Nov 2, 2020 · 3 comments

Comments

@cd21h
Copy link

cd21h commented Nov 2, 2020

Describe the Bug

System.ArgumentException with message Type must not be ByRef (Parameter 'type') is thrown when building container if
type constructor contains in parameter.

Same code did not throw any exception.

Steps to Reproduce

    public readonly struct Input
    {
        public Input(string foo)
        {
            Foo = foo;
        }
        public string Foo { get; }
    }

    public class Service
    {
        public Service(in Input input)
        { }
    }

    public class UnitTest1
    {
        [Fact]
        public void CanBuildContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<Service>();
            builder.Build();
        }
    }

Expected Behavior

Looks like this is limitation of lambda. In that case, detailed error message with reference to type being processed (Service in this case) should be thrown.

Exception with Stack Trace


System.ArgumentException
Type must not be ByRef (Parameter 'type')
   at System.Dynamic.Utils.TypeUtils.ValidateType(Type type, String paramName, Boolean allowByRef, Boolean allowPointer)
   at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
   at Autofac.Core.Activators.Reflection.ConstructorBinder.GetConstructorInvoker(ConstructorInfo constructorInfo)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Autofac.Core.Activators.Reflection.ConstructorBinder..ctor(ConstructorInfo constructorInfo)
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ConfigurePipeline(IComponentRegistryServices componentRegistryServices, IResolvePipelineBuilder pipelineBuilder)
   at Autofac.Core.Registration.ComponentRegistration.BuildResolvePipeline(IComponentRegistryServices registryServices, IResolvePipelineBuilder pipelineBuilder)
   at Autofac.Core.Registration.ComponentRegistration.BuildResolvePipeline(IComponentRegistryServices registryServices)
   at Autofac.Core.Registration.ComponentRegistryBuilder.Build()
   at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)
   at AutofacInParameter.UnitTest1.CanBuildContainer()

Dependency Versions

Autofac: 6.0.0
Target Framework: netcoreapp3.1

Additional Info

In the real application that class was registered by mistake using assembly scanning, so it was never resolved, but it took me a while to find root cause, because exception thrown had no reference registration,

@alistairjevans
Copy link
Member

It appears as though this is an oversight in the ConstructorBinder.DetectIllegalParameter method.

We check for pointers, but not in parameters.

Would you be willing to raise a PR for the relatively simple addition of checking for 'in' parameters, plus an associated test?

@alsami
Copy link
Member

alsami commented Nov 4, 2020

Would you be willing to raise a PR for the relatively simple addition of checking for 'in' parameters, plus an associated test?

I've sent a PR your way, including tests.

@tillig
Copy link
Member

tillig commented Nov 4, 2020

Resolved by #1228

@tillig tillig closed this as completed Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants