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.

Exception netstandard 1.4 PCL

See original GitHub issue

I’m trying to to validate a ViewModel in a netstandard 1.4 PCL. The code below crashes in the constructor of RegisterCmdValidator with an ArgumentException: “Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.”

This happens with 6.2.1 and the latest beta (6.4.0-beta9 at the time of writing).

Callstack:

at System.Reflection.RuntimeMethodInfo.CreateDelegateInternal(Type delegateType, Object firstArgument, DelegateBindingFlags bindingFlags, StackCrawlMark& stackMark) at System.Reflection.RuntimeMethodInfo.CreateDelegate(Type delegateType) at FluentValidation.Resources.StaticResourceAccessorBuilder.GetResourceAccessor(Type resourceType, String resourceName) in C:\projects\FluentValidation\src\FluentValidation\Resources\IResourceAccessorBuilder.cs:line 44 at FluentValidation.Resources.LocalizedStringSource…ctor(Type resourceType, String resourceName, IResourceAccessorBuilder resourceAccessorBuilder) in C:\projects\FluentValidation\src\FluentValidation\Resources\LocalizedStringSource.cs:line 40 at FluentValidation.Resources.LocalizedStringSource.CreateFromExpression(Expression1 expression, IResourceAccessorBuilder resourceProviderSelectionStrategy) in C:\projects\FluentValidation\src\FluentValidation\Resources\LocalizedStringSource.cs:line 68 at FluentValidation.DefaultValidatorOptions.<>c__DisplayClass10_02.<WithLocalizedMessage>b__0(PropertyRule config) in C:\projects\FluentValidation\src\FluentValidation\DefaultValidatorOptions.cs:line 185 at FluentValidation.Internal.RuleBuilder2.FluentValidation.Internal.IConfigurable<FluentValidation.Internal.PropertyRule,FluentValidation.IRuleBuilderOptions<T,TProperty>>.Configure(Action1 configurator) in C:\projects\FluentValidation\src\FluentValidation\Internal\RuleBuilder.cs:line 80 at FluentValidation.DefaultValidatorOptions.WithLocalizedMessage[T,TProperty](IRuleBuilderOptions2 rule, Expression1 resourceSelector, IResourceAccessorBuilder resourceAccessorBuilder) in C:\projects\FluentValidation\src\FluentValidation\DefaultValidatorOptions.cs:line 184 at FluentValidation.DefaultValidatorOptions.WithLocalizedMessage[T,TProperty](IRuleBuilderOptions2 rule, Expression1 resourceSelector) in C:\projects\FluentValidation\src\FluentValidation\DefaultValidatorOptions.cs:line 141 at ViewModels.RegistrationViewModel.RegisterCmdValidator…ctor(ICoreStrings coreStrings)

public class RegistrationViewModel
{
	...

    private readonly ICoreStrings coreStrings;

    class RegisterCmdValidator : AbstractValidator<RegistrationViewModel>
    {
        public RegisterCmdValidator(ICoreStrings coreStrings)
        {
            RuleFor(vm => vm.Password).NotEmpty().WithLocalizedMessage(() => FluentValidation.Resources.Messages.notempty_error);
            RuleFor(vm => vm.Password).Length(4, 15).WithLocalizedMessage(() => FluentValidation.Resources.Messages.length_error);
            RuleFor(vm => vm.ConfirmPassword).NotEmpty().WithLocalizedMessage(() => FluentValidation.Resources.Messages.notempty_error);

            // Crash
            RuleFor(vm => vm.ConfirmPassword).Must((vm, password) => vm.Password == password).WithLocalizedMessage(() => coreStrings.ValidationPasswordConfirmMismatch);

            // Works
            RuleFor(vm => vm.ConfirmPassword).Must((vm, password) => vm.Password == password).WithMessage(coreStrings.ValidationPasswordConfirmMismatch);
        }
    }

    readonly RegisterCmdValidator registerCmdValidator;

    private string email;

    public string Email
    {
        get { return email; }
        set { this.RaiseAndSetIfChanged(ref email, value); }
    }

    private string password;

    public string Password
    {
        get { return password; }
        set { this.RaiseAndSetIfChanged(ref password, value); }
    }

    private string confirmPassword;

    public string ConfirmPassword
    {
        get { return confirmPassword; }
        set { this.RaiseAndSetIfChanged(ref confirmPassword, value); }
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
oliverwcommented, Jan 30, 2017

Yeah it’s not really obvious right now that the lambda has some hidden requirements. I think your proposed changes are spot on.

1reaction
JeremySkinnercommented, Jan 30, 2017

This has caused confusion a few times…I guess it comes from using a lambda for something unusual which isn’t expected.

I’m thinking of changing this behaviour for the next version:

  1. Add new WithLocalizedMessage(Type resourceType, string resourceName)
  2. Deprecate WithLocalizedMessage(Expression<Func<string>>), with the above method being the recommended replacement
  3. Add a new WithLocalizedMessage(Func<T, string>) that would work as you expect, and also provide access to the container model in the delegate

What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NET Standard
With a few exceptions, it includes only technologies that work cross-platform. For .NET 5 code, net5.0 replaces both netcoreapp and netstandard ...
Read more >
Upgraded to .Net Standard 2, now project says reference ...
I just upgraded a .Net Standard 1.4 PCL dll to .Net Standard 2.0. I am trying to write a method that returns a...
Read more >
Resources File in .NET Standard (1.4) with New csproj #1984
I'm developing an application targeting .NET Standard 1.4, using the new csproj style (not project.json), and trying to get a localizable ...
Read more >
Understanding the .NET ecosystem: The introduction of . ...
In this article, previously part of my new book, we look at .NET Standard, look at why it was created, and discuss its...
Read more >
How to Convert a PCL Library to .NET Standard and Keep ...
It is time to convert those old PCL libraries to new .NET Standard libraries! It is so easy to do and takes only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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