unit-testingdependency-injectionasp.net-web-api2odatamissingmethodexception

Unit Testing ODataQueryOptions Gives MissingMethodException DependencyInjection


So here is my problem, I have an OData Web Api service that uses ODataQueryOptions to filter data from our sql server and I am trying to setup a .Net Framework Unit Test project to test the controllers with different query options. I have been searching for several days now and found many examples but most of them use an older version of OData. This example is the best one I have found so far, the only problem is that calling config.EnableDependencyInjection(); gives me the following exception:

Method not found: 'System.IServiceProvider Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(Microsoft.Extensions.DependencyInjection.IServiceCollection)'.

Here is an example of my code:

using System.Collections.Generic; 
using System.Web.Http.Results;
using System.Web.OData;
using System.Web.OData.Query;
using System.Net.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using University.API.OData.Controllers;
using University.API.OData.Models;
using System.Web.OData.Routing;
using System.Web.Http;
using System.Web.OData.Extensions;

[TestClass]
public class SalesforceUnitTest
{
    private HttpRequestMessage request;
    private ODataQueryOptions<Product> _options;

    [TestInitialize]
    public void TestInitialize()
    {
        request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/odata/product?$top=5");
        var model = WebApiConfig.GetModel();

        HttpConfiguration config = new HttpConfiguration();
        config.EnableDependencyInjection(); //Throws Missing Method Exception
        WebApiConfig.Register(config);
        config.EnsureInitialized();
        request.SetConfiguration(config);

        ODataQueryContext context = new ODataQueryContext(
            model,
            typeof(Product),
           new ODataPath(
               new Microsoft.OData.UriParser.EntitySetSegment(
                    model.EntityContainer.FindEntitySet("product"))
            )
        );

        _options = new ODataQueryOptions<Product>(context, request);
    }

    [TestMethod]
    public void ProductTest()
    {
        var controller = new ProductController();
        controller.Request = request;
        var response = controller.Get(_options);
        var contentResult = response as OkNegotiatedContentResult<List<Product>>;

        Assert.IsNotNull(contentResult);
        Assert.IsNotNull(contentResult.Content);
    }
}

Let me know if there is any other information you may need. Thank you for any help you can provide.

EDIT: Here what is referenced in the unit test project:

EntityFramework
EntityFramework.SqlServer
Microsoft.Data.Edm
Microsoft.Data.OData
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.DependencyInjection.Abstractions
Microsoft.OData.Core
Microsoft.Odata.Edb
Microsoft.Spatial
Microsoft.Threading.Tasks
Microsoft.Threading.Tasks.Extensions
Microsoft.Threading.Tasks.Extensions.Desktop
Microsoft.VisualStudio.TestPlatform.TestFramework
Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions
System
System.ComponentModel.DataAnnotations
System.Net
System.Net.Http
System.Net.Http.Extensions
System.Net.Http.Primitives
System.Net.Http.WebRequest.
System.Spatial
System.Web
System.Web.Http
System.Web.OData
ODataAPI

Solution

  • I figured it out after some more digging. It seems that my Unit Test project was using a different version than my ODataApi project. This for some weird reason was causing the MissingMethodException instead of a VersionMismatchException. Hopefully this helps someone else who is looking into why Dependency Injection isnt working for your Unit Test project.