question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ArgumentNullException when performing AspNetCore Integration Tests using `Microsoft.AspNetCore.TestHost.TestServer`

See original GitHub issue

Describe the bug ArgumentNullException when performing AspNetCore Integration Tests using Microsoft.AspNetCore.TestHost.TestServer

Value cannot be null.
Parameter name: operatingAssembly (See inner exception for details.) ---> 
System.ArgumentNullException: Value cannot be null.
Parameter name: operatingAssembly
   
at RazorLight.Compilation.RoslynCompilationService..ctor(IMetadataReferenceManager referenceManager, Assembly operatingAssembly)
   
at RazorLight.RazorLightEngineBuilder.Build()
   
at Services.Contract.IoC.ContractDomainModule.<>c.<Load>b__0_0(IComponentContext x)
   

To reproduce: Run a test class library targeting .NET Framework 4.7.1. but running a .NETCore api. Inject the RazorlightEngine in a controller, query the controller end point and wait for it to throw.

Information (please complete the following information):

  • OS: Windows 10
  • Platform .NET Framework 4.x
  • RazorLight version: 2.0.0-beta1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:9

github_iconTop GitHub Comments

3reactions
talanccommented, Aug 15, 2018

I’m getting the same exception in a .NET Framework 4.6.1 MSTest project – but not when I’m running the ASP.NET Core project…

operatingAssembly must be null from a test project?

2reactions
BarsikVcommented, Apr 2, 2020

I have a .netframework project and I had a similar issue. You need to call “SetOperatingAssembly” and pass the assembly where you actually use the razor engine. To make this work I created a factory that is easily injectable and it can create the razor engine wherever and whenever I need it. Example of the factory:

public class RazorEngineFactory : IRazorEngineFactory
{
    public IRazorLightEngine Create()
    {
        return new RazorLightEngineBuilder()
            .SetOperatingAssembly(Assembly.GetCallingAssembly())
            .UseFileSystemProject(Directory.GetCurrentDirectory())
            .UseMemoryCachingProvider()
            .Build();
    }
}

Just inject this factory and use it to create the razor engine instead of directly injecting the engine. By the way, i am using the filesystem resource type, just change it to embedded as you need.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core TestHost Integration Testing 'System. ...
System.ArgumentNullException : String reference not set to an instance of a String. Parameter name: s. Stack Trace: at System.Text.Encoding.
Read more >
The curious case of ASP.NET Core integration test deadlock
One of the common approaches to testing ASP.NET Core applications is to use the integration testing available via the Microsoft.AspNetCore.
Read more >
Integration tests in ASP.NET Core
ASP.NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server.
Read more >
Integration Testing - ASP.NET Core Documentation
AspNetCore.TestHost package is included in the project, you will be able to create and configure a TestServer in your tests. The following test...
Read more >
ASP.NET Core Integration Tests With TestServer - How Did I ...
The test project references the Web API project. And I'm adding a few NuGet packages: Microsoft.AspNetCore.TestHost v3.1.21; Newtonsoft.Json ( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found