C
C#2y ago
Foxtrek_64

✅ Multi-targeting NetFx and NetCore - Targeting wrong version?

History My last post was a bit of a mess so I've come here to help clean it up a bit. I have a project called Tafs.Activities.Finance. It multi-targets net461, net462, net6.0, and net7.0. In the project file, I have a few conditional includes. If we're working with NetFx, it includes a reference to Tafs.Activities.Results. If we're working with Net6/7, it instead includes a reference to Remora.Results. These two libraries provide the same types, though for NetFx and Net6.0+ respectively. In the Finance library, I have a type called SocialSecurityNumber which references a ValidationError. ValidationError exists in a third library: Tafs.Activities.Results.Extensions. The extensions library is set up very similarly to the finance library in its conditional includes. The ValidationError type implements ResultError, which is provided by either Tafs.Activities.Results or Remora.Results depending on the target.
#if NET6_OR_GREATER
using Remora.Results;
#endif

namespace Tafs.Activities.Results.Extensions.Errors;

public sealed record class ValidationError(string Message) : ResultError(Message);
#if NET6_OR_GREATER
using Remora.Results;
#endif

namespace Tafs.Activities.Results.Extensions.Errors;

public sealed record class ValidationError(string Message) : ResultError(Message);
Intellisense on ResultError does update properly between Tafs.Activities.Results and Remora.Results based on the selected build target. The Error When consuming ValidationError in my Finance project, I do so by assigning it to a Result type. The source of the Result type should likewise correspond to the selected build target, and Intellisense does confirm this. NetFx should only be aware of Tafs.Activities.Results and Net6/7 should only be aware of Remora.Results. This assignment operation relies on an implicit converter which handles anything of type ResultError:
// Result.cs
public static implicit operator Result(ResultError error)
=> new(error, defeault);

// SocialSecurityNumber.cs
public static Result Validate(SocialSecurityNumber ssn)
{
if (ssn.AreaNumber == Default.AreaNumber)
{
return new ValidationError("The area number cannot be '000'.");
}
// Rest of function omitted for brevity.
}
// Result.cs
public static implicit operator Result(ResultError error)
=> new(error, defeault);

// SocialSecurityNumber.cs
public static Result Validate(SocialSecurityNumber ssn)
{
if (ssn.AreaNumber == Default.AreaNumber)
{
return new ValidationError("The area number cannot be '000'.");
}
// Rest of function omitted for brevity.
}
I get two exceptions on the return statement when targeting net461 or net462:
"Cannot implicitly convert type 'Tafs.Activities.Results.Extensions.Errors.ValidationError' to 'Tafs.Activities.Results.Result'"

"The type 'ResultError' is defined in an assembly that is not referenced. You must add a reference to assembly 'Remora.Results, Version=7.2.3.0, Culture=neutral, PublicKeyToken=null'."
"Cannot implicitly convert type 'Tafs.Activities.Results.Extensions.Errors.ValidationError' to 'Tafs.Activities.Results.Result'"

"The type 'ResultError' is defined in an assembly that is not referenced. You must add a reference to assembly 'Remora.Results, Version=7.2.3.0, Culture=neutral, PublicKeyToken=null'."
Adding a reference to Remora.Results as suggested, even though NetFx can't use it, does resolve the second error, but I think that's a fake error anyways so I'm ignoring that. What I think is happening is that when the Finance library is referencing Tafs.Activities.Results.Extensions, it's somehow grabbing the Net6/Net7 target, even though the Finance library itself is targeting net461 or net462. If I change the target selector for the finance library to Net6 or Net7, the error goes away and the Result type changes to Remora.Results.Result. I have tried doing a project clean, then building the projects one at a time, but this does not have any effect on the issue. Does anyone have any ideas as to what might be happening here or how to fix it?
5 Replies
Foxtrek_64
Foxtrek_64OP2y ago
Tagging @Jax since your library is involved, though likely not complicit in the error. I think this is probably some weird language thing
ero
ero2y ago
Why are you using a different library when on netfx?
Foxtrek_64
Foxtrek_64OP2y ago
Remora.Results does not support net461 or net462 Wait, Jax is targeting NetStandard 2.0, which supports Net461 and 462 That solves my issues right there. No more multi-targeting!
ero
ero2y ago
yeah i was gonna say
Foxtrek_64
Foxtrek_64OP2y ago
Glad I found that

Did you find this page helpful?