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.

Null Reference Exception in generated Data Service class

See original GitHub issue

Hi, the last update has introduced an issue in the generated Data Service class. The following generated code throws a null reference exception when the result from the data operation is null.

        public static Task<Tenant> GetByHostNameAsync(string? hostName)
        {
            return DataSvcInvoker.Default.InvokeAsync(typeof(TenantDataSvc), async () => 
            {
                var __result = await Factory.Create<ITenantData>().GetByHostNameAsync(hostName).ConfigureAwait(false);
                ExecutionContext.Current.CacheSet(__result.UniqueKey, __result);
                if (_getByHostNameOnAfterAsync != null) await _getByHostNameOnAfterAsync(__result, hostName).ConfigureAwait(false);
                return __result;
            });
        }

This is generated using:

  <Entity Name="Tenant" Text="Tenant configuration" Implements="IETag, IChangeLog, ITenant" Collection="true" WebApiRoutePrefix="api/v1/tenants" CollectionResult="true" 
          AutoImplement="Database" DatabaseSchema="dbo">
    ...
    <Operation Name="GetByHostName" OperationType="Custom" ReturnType="Tenant" WebApiRoute="hostName/{hostName}" WebApiMethod="HttpGet">
      <Parameter Name="HostName" Type="string" />
    </Operation>
  </Entity>

I think this is due to the change to line 481 - https://github.com/Avanade/Beef/blame/master/tools/Beef.CodeGen.Core/Templates/EntityDataSvc_cs.xml#L481

ExecutionContext.Current.CacheSet<{{Operation.ReturnType}}>(__result?.UniqueKey ?? UniqueKey.Empty, __result);

has been changed to:

ExecutionContext.Current.CacheSet(__result.UniqueKey, __result);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
chullybuncommented, May 11, 2020

Thanks @edjo23 - I have merged in the PR and published an updated NuGet package for Beef.CodeGen.Core v3.1.7.

0reactions
edjo23commented, May 11, 2020

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What is a NullReferenceException, and how do I fix it?
This means the reference is null , and you cannot access members (such as methods) through a null reference. The simplest case: string...
Read more >
Object Reference Not Set to an Instance of an Object
This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a...
Read more >
NullReferenceException Class (System)
The exception that is thrown when there is an attempt to dereference a null object reference.
Read more >
c# - Determining the object that caused a null reference ...
Frequently in applications we encounter situations that could throw a NullReferenceException ; for example, assuming the following method's ...
Read more >
Getting Null Reference Exception when trying to connect to ...
At runtime I am getting a Null Reference Exception that seems to be coming from the System.ServiceModel class. This null reference exception is ......
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