OData / OData/AspNetCoreOData

$metadata endpoint does not work when creating the host from a Console Application

Open
#1,306 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug follow-up
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Assemblies affected
ASP.NET Core OData 9.0.0

Describe the bug
Creating a console application then adding a WebHost to it containing oData, makes the /$metadata endpoint to not be created, generating a 404 - NotFound.

Reproduce steps

  • Create Console Application (.net 8)
  • Add FrameworkReference and oData in the .csproj file:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
    <ItemGroup>
	    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    </ItemGroup>
    <ItemGroup>
      <PackageReference Include="Microsoft.AspNetCore.OData" Version="9.0.0" />
    </ItemGroup>

</Project>

  • Configure Program.cs:
using System.Net;
using Microsoft.AspNetCore.OData;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;

namespace TestePesquisaConsole;

internal class Program
{
    static void Main(string[] args)
    {
        Host.CreateDefaultBuilder()
            .ConfigureWebHostDefaults(
                webHost =>
                {
                    webHost.UseStartup<Startup>();
                    webHost.ConfigureKestrel(kestrel =>
                    {
                        kestrel.Listen(IPAddress.Any, 5900);
                    });
                })
            .Build()
            .Run();
    }

    private class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services
                .AddControllers()
                .AddOData(option =>
                {
                    option.Select().Filter().OrderBy().Expand().Count().SetMaxTop(null).AddRouteComponents("Pesquisa", GetEdmModel());
                    option.TimeZone = TimeZoneInfo.Utc;
                });

            static IEdmModel GetEdmModel()
            {
                ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
                builder.EntitySet<ClientePsqDto>(nameof(ClientePsqBo)).EntityType.Page(null, null);
                return builder.GetEdmModel();
            }
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();

            app.UseEndpoints(
                routeBuilder =>
                {
                    routeBuilder.MapControllers();
                });
        }
    }
}
  • Go to: http://localhost:5900/Pesquisa/$metadata and get's 404

Data Model

public class ClientePsqDto
{
    [Key]
    public long Identificador { get; set; }

    public string Nome { get; set; } = string.Empty;
}

EDM (CSDL) Model
404 - NotFound

Request/Response
http://localhost:5900/Pesquisa/$metadata
404 - NotFound

Expected behavior
Return the metadata

Additional context
In the project file if I change the Project Sdk it works:
Doesn't Work:
<Project Sdk="Microsoft.NET.Sdk">
Works:
<Project Sdk="Microsoft.NET.Sdk.Web">

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the supplied .csproj and Program.cs, comparing Microsoft.NET.Sdk with Microsoft.NET.Sdk.Web. Start by tracing how the console-hosted WebHost registers controllers and OData route components, then verify the /Pesquisa/$metadata request. Done means the endpoint returns metadata with the console SDK configuration, without requiring the Web SDK.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.