0

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
3
  • It seems to be using the DI framework from .net core. That method is an extension method. You are either missing the reference or the namespace or both. Commented Mar 6, 2018 at 15:42
  • Check to make sure Microsoft.Extensions.DependencyInjection is referenced Commented Mar 6, 2018 at 15:44
  • The reference exists in both the Unit Test project and My ODataAPI project Commented Mar 6, 2018 at 15:59

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.