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.

Put models folder into a new project named DataAccessSample from SwaggerODataSample solution

See original GitHub issue

Hi,

I have a bug in my demo when I use SwaggerODataSample(aspnetcore),and I do not know how to do with the DI of my new project DataAccessSample into startup.cs.

### The log is: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.AspNet.OData.TypeExtensions.<>c__DisplayClass23_0.<GetStructuredType>b__0(IEdmStructuredType t) in C:\Users\Fusionshen\Downloads\aspnet-api-versioning\src\Common.OData.ApiExplorer\AspNet.OData\TypeExtensions.cs:line 137 at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable1 source, Func2 predicate, Boolean& found) at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate) at Microsoft.AspNet.OData.TypeExtensions.GetStructuredType(Type type, IEdmModel model, IEnumerable1 assemblies) in C:\Users\Fusionshen\Downloads\aspnet-api-versioning\src\Common.OData.ApiExplorer\AspNet.OData\TypeExtensions.cs:line 137 at Microsoft.AspNet.OData.TypeExtensions.SubstituteIfNecessary(Type type, IServiceProvider serviceProvider, IEnumerable1 assemblies, ModelTypeBuilder modelTypeBuilder) in C:\Users\Fusionshen\Downloads\aspnet-api-versioning\src\Common.OData.ApiExplorer\AspNet.OData\TypeExtensions.cs:line 53 at Microsoft.AspNetCore.Mvc.ApiExplorer.ODataApiDescriptionProvider.GetApiResponseTypes(IReadOnlyList1 responseMetadataAttributes, Type responseType, IServiceProvider serviceProvider) in C:\Users\Fusionshen\Downloads\aspnet-api-versioning\src\Microsoft.AspNetCore.OData.Versioning.ApiExplorer\AspNetCore.Mvc.ApiExplorer\ODataApiDescriptionProvider.cs:line 556 at Microsoft.AspNetCore.Mvc.ApiExplorer.ODataApiDescriptionProvider.<NewODataApiDescriptions>d__33.MoveNext() in C:\Users\Fusionshen\Downloads\aspnet-api-versioning\src\Microsoft.AspNetCore.OData.Versioning.ApiExplorer\AspNetCore.Mvc.ApiExplorer\ODataApiDescriptionProvider.cs:line 324 at Microsoft.AspNetCore.Mvc.ApiExplorer.ODataApiDescriptionProvider.OnProvidersExecuted(ApiDescriptionProviderContext context) in C:\Users\Fusionshen\Downloads\aspnet-api-versioning\src\Microsoft.AspNetCore.OData.Versioning.ApiExplorer\AspNetCore.Mvc.ApiExplorer\ODataApiDescriptionProvider.cs:line 172 at Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescriptionGroupCollectionProvider.GetCollection(ActionDescriptorCollection actionDescriptors) at Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescriptionGroupCollectionProvider.get_ApiDescriptionGroups() at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath, String[] schemes) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.<Invoke>d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame1.<ProcessRequestsAsync>d__2.MoveNext() ### The bug location: default

It seems that the DI of my new project DataAccessSample is not ok.

what can I do?

Regards

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

1reaction
commonsensesoftwarecommented, Oct 20, 2018

I’ve been able to confirm this is actually a bug that occurs in two parts:

  1. When the model type is a different assembly it may not be automatically added to the AssemblyPartManager, which is how assemblies are discovered by default
  2. It is [falsely] assumed that a model type should be mapped to a CLR type from the available assemblies. This may not be true when part 1 is true, making the CLR type undiscoverable, and can actually result in a NullReferenceException.

I should have both of these issues fixed shortly and published in the next release.

In the meantime, you can work around the issue as @spasarto suggested or simply change line 27 in your example to:

services.AddMvc().AddApplicationPart(typeof(Order).Assembly);
0reactions
commonsensesoftwarecommented, Oct 17, 2018

Good point @spasarto. That could be why it’s not being automatically detected. If that’s truly the issue, there are two ways to deal with it. Option 1 is exactly as @spasarto suggests. Option 2 is that you can add to the model builder before you build the models. Dependency Injection is a convenience and not a requirement for things to work.

Either of the following are valid:

public void Configure( IApplicationBuilder app )
{
  var modelBuilder = new VersionedODataModelBuilder()
  {
      ModelConfigurations =
      {
          new PersonModelConfiguration()
      }
  };
  var models = modelBuilder.GetEdmModels();
  app.UseMvc( routes => routes.MapVersionedODataRoutes( "odata", default, models ) );
}

Figure 1: Imperatively create a model builder without any Dependency Injection

public void Configure( IApplicationBuilder app, VersionedODataModelBuilder modelBuilder )
{
  modelBuilder.ModelConfigurations.Add( new PersonModelConfiguration() );
  var models = modelBuilder.GetEdmModels();
  app.UseMvc( routes => routes.MapVersionedODataRoutes( "odata", default, models ) );
}

Figure 2: Add to a model builder supplied via Dependency Injection

Read more comments on GitHub >

github_iconTop Results From Across the Web

Put models folder into a new project named ...
I have a bug in my demo when I use SwaggerODataSample(aspnetcore),and I do not know how to do with the DI of my...
Read more >
ASP.NET CORE - Adding Models Folder and Models Class ...
To add a class in models folder; Right click on Models Add Class. Add New Item Windows will be open. Select Code in...
Read more >
How do I add an existing directory tree to a project in Visual ...
You will then need to select this directory, right click, and choose "Include in Project." enter image description here. Share.
Read more >
Import Models
You can import models into a project or into a folder. ... The model name is updated only when a new name is...
Read more >
Organizing and testing projects with the .NET CLI
To organize the project, create a new folder and name it Models to hold the type files. Place the type files into the...
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