Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Molinware opened this issue Aug 29, 2024 · 5 comments
Labels
bug Something isn't working follow-up

Comments

@Molinware
Copy link

Molinware commented Aug 29, 2024

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();
                });
        }
    }
}

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">

@Molinware Molinware added the bug Something isn't working label Aug 29, 2024
@julealgon
Copy link
Contributor

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">

This is very interesting.... I personally had no idea the SDK had that kind of influence in the application side.

Out of curiosity, does it work if you use the Worker SDK? Microsoft.NET.Sdk.Worker?

@Molinware
Copy link
Author

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">

This is very interesting.... I personally had no idea the SDK had that kind of influence in the application side.

Out of curiosity, does it work if you use the Worker SDK? Microsoft.NET.Sdk.Worker?

Tested! Doesn't work too...

@xuzhg
Copy link
Member

xuzhg commented Sep 3, 2024

FYI: https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview

The difference between "Microsoft.NET.Sdk" and "Microsoft.NET.Sdk.Web":

image

@corranrogue9
Copy link
Contributor

Shouldn't the nuget package automatically update the project's SDK? Or if not, include a .props and .targets file in the package to import the SDK like here

@julealgon
Copy link
Contributor

@corranrogue9

Shouldn't the nuget package automatically update the project's SDK?

I don't think that is a thing.

Or if not, include a .props and .targets file in the package to import the SDK like here

But the question is "what is missing exactly"? Why does the metadata controller only work on the Web SDK?

I would be careful with any attempted solution before we fully understand that bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working follow-up
Projects
None yet
Development

No branches or pull requests

5 participants