Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot add ValueProviderFactory to Web API

I'm working with MVC 4 and I have this simple dummy ValueProvider:

class DummyValueProviderFactory : ValueProviderFactory
{
    public override IValueProvider GetValueProvider(ControllerContext controllerContext)
    {
        return new DummyValueProvider();
    }

    private class DummyValueProvider : IValueProvider
    {
        public DummyValueProvider()
        {
        }

        public bool ContainsPrefix(string prefix)
        {
            return true;
        }

        public ValueProviderResult GetValue(string key)
        {
            return null;
        }
    }
}

And my problem resides when I try to register it's factory in the Web API:

config.Services.Add(typeof(ValueProviderFactory), new DummyValueProviderFactory());

It compiles OK, but when the server starts, I get an ArgumentException telling me The service type ValueProviderFactory is not supported.

I've read this tutorial and this other one and both are telling me it should work fine. What am I doing wrong?

like image 598
Lucio Paiva Avatar asked Oct 04 '22 12:10

Lucio Paiva


1 Answers

Make sure you are referencing the ValueProviderFactory in the ASP.NET Web API namespace, and not the one the MVC namespace. They both have the same class name, but they are in different namespaces.

like image 84
Pablo Cibraro Avatar answered Oct 07 '22 17:10

Pablo Cibraro