user4266661
user4266661

Reputation:

Can't Find Tuple Method

I have a Net 4.6.2 console app which uses a Net Standard 2.0 library I've written. One of the methods in the library returns a tuple:

public (ScanStatEntry scanEntry, DateTime nextScan) Run()
{

The routine is called from within the console app as follows:

(ScanStatEntry scanEntry, DateTime nextScan) = _scanner.Run();

This all worked fine for quite some time...until suddenly I started getting the following exception when the console app ran:

System.MissingMethodException: Method not found: 'System.ValueTuple`2<Olbert.CommunityScanner.Data.Entities.ScanStatEntry,System.DateTime> Olbert.CommunityScanner.Scanner.Run()'. at Olbert.CommunityScanner.ScannerService.RunAndWait() at Olbert.CommunityScanner.ScannerService.Start() in C:\Programming\CommunityScanner\CommunityScannerTopShelf\ScannerService.cs:line 70 at Olbert.CommunityScanner.Program.<>c.b__0_2(ScannerService tc) in C:\Programming\CommunityScanner\CommunityScannerTopShelf\Program.cs:line 20 at Topshelf.ServiceConfiguratorExtensions.<>c__DisplayClass2_0`1.<WhenStarted>b__0(T service, HostControl control) at Topshelf.Builders.DelegateServiceBuilder`1.DelegateServiceHandle.Start(HostControl hostControl) at Topshelf.Hosts.ConsoleRunHost.Run()

I don't know what I did to cause this problem to appear. Also, everything compiles fine, with no errors; it just crashes as soon as execution tries to enter the method within which the _scanner.Run() call is located (e.g., the containing class' constructor executes just fine).

In researching this, I realized that I had never installed the System.ValueTuple NuGet package for the console app...which I thought was a requirement when using tuples in Net 4.6.2, although, if so, I don't know why the app used to run fine.

So I tried adding the package. No joy; same compile okay but crash on execution claiming the method can't be found.

Calling another method in that same Net Standard 2.0 library works fine. It's just the method that returns a tuple that causes the problem. I could work around this by returning an object rather than a tuple, but I'm curious as to what's causing the problem, and why it appeared after everything was working fine for so long.

Upvotes: 1

Views: 1583

Answers (2)

Rafael Gurski
Rafael Gurski

Reputation: 1

I know it's a late answer, but if someone still have this problem and come by this thread, we solved it by adding multiple framework targets to the csproj file of the .net-standard project.

That way the compiler compiles a version for each framework and links the correct one.

<PropertyGroup>
    <TargetFrameworks>net472;net5.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>

PS: note that it's TargetFrameworkS, plural.

Upvotes: 0

user4266661
user4266661

Reputation:

Several more hours researching this problem online led me into discussions about problems associated with automatic binding redirects. Apparently, those can arise when you are mixing net standard and net traditional assemblies, which themselves depend upon other assemblies that may depend on different versions of other assemblies.

In my net traditional console app automatic redirect was set in the csproj file (old style format, first PropertyGroup):

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{B64EE74D-F171-4438-8E7A-ACAE7B40C6C8}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>Olbert.CommunityScanner</RootNamespace>
    <AssemblyName>CommunityScannerTopShelf</AssemblyName>
    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>

Setting AutoGeneratedBindingRedirects to false solved the problem of the application crashing, and let me step into the Net Standard class library method that was returning a tuple.

Upvotes: 2

Related Questions