bbsimonbb
bbsimonbb

Reputation: 29022

Dependency resolution with .net web api and tinyioc

I have TinyIoc running happily in a .net web api. I just tried to add a service class that uses System.Web.Http.HttpClient. I explicitly register my new class with the container, but, all of a sudden, I have a stack of runtime errors on application start - tiny can't find a bunch of System.Web.Http classes. Here is the first such error...

TinyIoC.TinyIoCResolutionException
HResult=0x80131500
Message=Unable to resolve type: System.Web.Http.Metadata.ModelMetadataProvider
Source=HubHacienda
StackTrace:
at TinyIoC.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options) in C:\Workspaces\HubHacienda_DEV\HubHacienda\TinyIoC.cs:line 3558
>   HubHacienda.dll!TinyIoC.TinyIoCContainer.ResolveInternal(TinyIoC.TinyIoCContainer.TypeRegistration registration, TinyIoC.NamedParameterOverloads parameters, TinyIoC.ResolveOptions options) Line 3557  C#
    HubHacienda.dll!TinyIoC.TinyIoCContainer.Resolve(System.Type resolveType) Line 1566 C#
    HubHacienda.dll!HubHacienda.TinyIoCDependencyResolver.GetService(System.Type serviceType) Line 36   C#
    [External Code] 
    HubHacienda.dll!HubHacienda.WebApiApplication.Application_Start() Line 1517:13 12/09/201817:13 12/09/201817:14 12/09/2018   C#
    [External Code] 

Here it the Application_Start() method in global.asax

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

Here is the Register() method from my WebApiConfig class...

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services

    // Web API routes
    //config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional },
        constraints: new { id = @"\d*" }
    );
    // sby
    // http://www.rbwestmoreland.com/posts/inversion-of-control-in-asp-net-web-api/
    var container = new TinyIoCContainer();
    // Repositorys
    container.Register<IJustifRepo, JustifRepo>();
    container.Register<IReferringAppsRepo, ReferringAppsRepo>();
    // then many more like this...

    config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data"));

    config.DependencyResolver = new TinyIoCDependencyResolver(container);


}

Lastly, my TinyIocDependencyResolver is here...

public class TinyIoCDependencyResolver : IDependencyResolver
{
    private TinyIoCContainer _container;

    public TinyIoCDependencyResolver(TinyIoCContainer container)
    {
        if (container == null)
            throw new ArgumentNullException("container");

        _container = container;
    }

    public IDependencyScope BeginScope()
    {
        if (_disposed)
            throw new ObjectDisposedException("this", "This scope has already been disposed.");

        return new TinyIoCDependencyResolver(_container.GetChildContainer());
    }

    public object GetService(Type serviceType)
    {
        if (_disposed)
            throw new ObjectDisposedException("this", "This scope has already been disposed.");

        try
        {
            return _container.Resolve(serviceType);
        }
        catch (TinyIoCResolutionException)
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        if (_disposed)
            throw new ObjectDisposedException("this", "This scope has already been disposed.");

        try
        {
            return _container.ResolveAll(serviceType);
        }
        catch (TinyIoCResolutionException)
        {
            return Enumerable.Empty<object>();
        }
    }

    #region IDisposable

    bool _disposed;

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed)
            return;

        if (disposing)
            _container.Dispose();

        _disposed = true;
    }

    #endregion IDisposable
}

Does this mean anything to anyone? HttpClient, the likely addition that is causing my pain, is instantiated explicitly, not by DI. Why should I be getting DI errors for classes that aren't mine? Target framework for all projects is .net 4.5.

Upvotes: 0

Views: 409

Answers (0)

Related Questions