System.BadImageFormatException when running Unit Tests

Hello,

I’m developing a CodeActivity wrapper for a UriBuilder. I created a new .NET Framework 4.5.2 library and added the System.Activities and System.ComponentModel references as specified in the guide.

Now that I have a functional class, which I named UrlBuilder so as to avoid a namespace collision, I created an xUnit test library and created a simple test as follows:

[Theory]
[InlineData("www.google.com")]
[InlineData("www.duckduckgo.com")]
public void NoOverridesTest(string website)
{
    var activity = new UriBuilderActivity();

    var input = new Dictionary<string, object>
    {
        { nameof(UriBuilderActivity.Url), website }
    };

    var output = WorkflowInvoker.Invoke(activity, input);

    Assert.Equal(website, output.Host);
    Assert.Equal("/", output.PathAndQuery);
}

When executing this unit test, it throws the following exception:

Test Name:	UrlBuilder.Tests.UriBuilderActivityTests.NoOverridesTest(website: "www.duckduckgo.com")
Test FullName:	UrlBuilder.Tests.UriBuilderActivityTests.NoOverridesTest
Test Source:	C:\Users\me\source\repos\UrlBuilder.Tests\UriBuilderActivityTests.cs : line 14
Test Outcome:	Failed
Test Duration:	0:00:00.001

Result StackTrace:	
at UrlBuilder.Tests.UriBuilderActivityTests.NoOverridesTest(String website)
----- Inner Stack Trace -----
Result Message:	
System.BadImageFormatException : Could not load file or assembly 'System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
---- System.BadImageFormatException : Cannot load a reference assembly for execution.

As you can see, it’s not a very useful stacktrace. Line 14 is merely the method declaration for my unit test, so it’s failing fairly early or else the exception isn’t bubbling up properly. I’d initially taken this to a chat group where I’d discussed my issues with a Microsoft developer who was surprised anyone was using WWF. He was unsure how to resolve my issue besides potentially changing .NET versions. I tried 4.6.1 but this saw no change.

Where can I go from here? StackOverflow is devoid of similar issues and there is one similar issue here but it’s unanswered and deals with a different part of the workflow.

Any ideas would be appreciated.

Thank you,

Devin

Amendment: since I can’t edit my original post and I feel I left this out of the original post, I feel this is appropriate to add.

The signature for my UriBuilderActivity looks like so:

public class UriBuilderActivity : CodeActivity<Uri>

If you would like to see the full class, you can see it here:
https://paste.mod.gg/wecetilido.cpp

Do note that the class hasn’t been cleaned yet. There’s still some code from when I assumed I could update the UiPath Studio Properties pane from code behind, and even then it probably wasn’t the best way to do it. I was also playing with the UriScheme class at the bottom which I’d hoped to use to make a list of the UriSchemes appear as a drop-down list in the properties pane but I was unsure of how to do that.

Found the issue…

A default xUnit test project relies on Microsoft.NETCore.App and runs against DotNetCore 2.1 (at least in an environment without 3.0 installed).

xUnit has both framework and core runners and either cross-compiles or compiles for standard, so this should be supported. Thus, loading a framework into a .net core process should normally work, but because you’re then targeting both framework and core, WWF can’t properly be targeted.

The solution was to create a new .NET Framework 4.5.2 class library and install the xunit and xunit.runner.visualstudio NuGet packages as per the xUnit Documentation. Running this new, hand-crafted unit test functions properly as you’re not dual-targeting two separate frameworks.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.