1

I've recently upgraded the .NET MongoDB Drivers from the Legacy ones to the newer ones. Lots of the queries don't work any more, this one has me stumped.

       var dataSource = permissions.Select(x => new PermissionSetPermissionGridViewModel
        {
            Id = x.Id,
            PermissionGroupInfo = new KeyValuePair<Guid?, string>(x.PermissionGroupId, TitleHelper.GetPermissionGroupTitle(x.PermissionGroupId)),
            IsEnabled = x.IsEnabled,
            ObjectType = ObjectTypeIds.GetEnumValue(x.ObjectTypeId.Value)
        }).ToList()

It doesn't seem to support the Static function TitleHelper.GetPermissionGroup() producing the following error.

    [NotSupportedException]: GetPermissionGroupTitle of type Saturn.WebUI.Core.Helpers.TitleHelper is not supported in the expression tree GetPermissionGroupTitle({document}{PermissionGroupId}).
   at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateMethodCall(MethodCallExpression node)
   at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateMapping(ProjectionMapping mapping)
   at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateMapping(ProjectionMapping mapping)
   at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node)
   at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateProjectValue(Expression selector)
   at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateSelect(SelectExpression node)
   at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node)
   at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)
   at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression)
   at MongoDB.Driver.Linq.MongoQueryableImpl`2.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Saturn.WebUI.Areas.Security.Controllers.PermissionSetRecordController.GetStaticPermissions(DataSourceRequest request, Guid permissionSetId) in C:\work\saturn-develop\saturn\src\Saturn.WebUI\Areas\Security\Controllers\PermissionSetRecordController.cs:line 87

How can I refactor this query so that It is supported by the newer drivers?

3
  • Use a sniffer like wireshark or fiddler and check the response status to see if you are getting 200 OK. Does original code still work? If so compare the original headers in first request with non working. You MAY have a TLS issue. TLS 1.0/1.1 was discontinued in June this year. Microsoft recently pushed a new security update on servers that disabled TLS 1.0/1.1 and you must use TLS 1.2 or newer. The new driver you installed may of updated your clients to TLS 1.2. There are too may combinations to say right now exactly what is causing issue. Sniffer results will help. Commented Aug 24, 2020 at 13:46
  • 1
    I'm pretty sure this comment was meant for another question Commented Aug 24, 2020 at 14:15
  • You have a runtime issue since you said queries do not run. Not compiler errors. So the issues are with newer driver during runtime. So why do you not think my response is meant for another question? Commented Aug 24, 2020 at 15:42

1 Answer 1

1

Your GetPermissionGroupTitle(...) method cannot be translated to meaningful Mongo expression (it has no equivalent).

What you can do is to get all the needed data in memory and then subsequently invoke that static method. Basically, you should call .ToList() and then another .Select(...) in which to call your static method. Another way to do this is to use AutoMapper - we are calling such helper extension method when mapping between different models. Something like that should be the configuration:

CreateMap<Permission, PermissionSetPermissionGridViewModel>()
    .ForMember(
        dest => dest.PermissionGroupInfo,
        cfg => cfg.MapFrom(src => new KeyValuePair<Guid?, string>( TitleHelper.GetPermissionGroupTitle(src.PermissionGroupId, src.PermissionGroupId)))) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.