News for hackers. Stuff that matters.
Re:.NET Myths Dispelled by luuio on Sunday March 13, 2022 @03:04AM (#30658718)
Huh? I don't think you read what I'm saying.

Here's a simple challenge for you. Create a command line app (your csproj file should say Sdk="Microsoft.NET.Sdk"), ask the user to input a port number at runtime. Use this port number to start an ASP.NET service.

I have not had any luck to pull all the right nuget dependencies to do that so far. The only way to run asp.net app I could find is switching your whole project to be Sdk="Microsoft.NET.Sdk.Web"

Re:.NET Myths Dispelled by kumarvvr on Sunday March 13, 2022 @03:21AM (#30658798)
You can run multiple asp.net core services on different ports on same machine. You could create an over-arching console application to automate the same, or you can do it with a simple script.

https://docs.microsoft.com/en-us/troubleshoot/developer/weba...

Re:.NET Myths Dispelled by luuio on Sunday March 13, 2022 @03:25AM (#30658828)
Nope. Not the same thing. Again, I want them to be in the same binary. Like the example I gave you, have the user input a port number *at runtime*, then use that port to start an asp.net service.

That's just an example. The point is mixing a regular console app with an asp.net service.

Re:.NET Myths Dispelled by useerup on Sunday March 13, 2022 @05:45AM (#30659662)
1. Create a new web project (it is actually a console app)

2. In program.cs add the following lines at the top:

    Console.Write("Port number:");
    var port = int.Parse(Console.ReadLine());
3. Change the bottom line App.Run()` to

    app.Run($"https://localhost:{port}");
You now have an application which asks for a port and launches your empty website on that port.

A `Microsoft.NET.Sdk.Web` project is still a console app. The "web" variant has some other default packages and (crucially) it has some other defaults for how to compile files such as cshtml.

Re:.NET Myths Dispelled by luuio on Sunday March 13, 2022 @11:22AM (#30661998)
I can confidently tell you that what you put here won't work. Try it.
Re:.NET Myths Dispelled by useerup on Sunday March 13, 2022 @12:24PM (#30662747)
I did. It works. The above code lines are cut directly from a test project that I made.

Several people have pointed out how a web app in .NET 5/6 is really a console app which only starts a web server configured with an application and waits for it to complete (i.e. shut down).

It really is that simple.

Re:.NET Myths Dispelled by luuio on Sunday March 13, 2022 @01:39PM (#30663702)
The whole point here is that the project does not target the Sdk.Web thing. Yours only works because the project is targeting Sdk.Web (instead of Sdk).

When I said console app, I literally meant using the project that you would create if you do `dotnet new console`.

To copy from my other comment:

That is exactly the thing. I do not want to have the Sdk=...Web/Worker. Imagine this scenario, you started a new project with the Sdk targeting Worker. Then you need that binary to also target web. What do you do?

- If you switch the project to Sdk=...Web, you won't have the dependencies to build the worker services.

- If you keep it as Sdk=...Worker, you won't have the dependencies to build asp.net

Re:.NET Myths Dispelled by useerup on Sunday March 13, 2022 @02:41PM (#30664417)
The "Sdk.Web thing" is the way the project is built.

As I and several people in this thread has pointed out to you, the application the resulting application is not a "web" application. It is a console app that launches a webserver.

The Sdk (without web) is not designed to compile cshtml and/or razor source files. It has nothing to do with package dependencies.

If you do want to start of with a console app sdk, then look at "minimal API". You simply add package references to Microsoft.AspNetCore and Microsoft.AspNetCore.App to your console app, and then you can start using the minimal API samples. They will also launch a website.

The example I showed you above was literally adding two lines and changing one line; and then you had a console app which asked the port and launched a webserver on that port.

If you start from a console app, you will not have build targets configured for cdhtml and razor files. However, to prove that you can launch a webserver the same way you can do this (yes I have tried it and it works):

1) Add dependencies to your "console sdk" project:

    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />
2) Replace program.cs with this (this is the entire app):

    using Microsoft.AspNetCore.Builder;
    var builder = WebApplication.CreateBuilder(args);
    var app = builder.Build();
    app.MapGet("/", () => "Hello World!");
    app.Run();
Voila. A web server.
Re:.NET Myths Dispelled by luuio on Sunday March 13, 2022 @08:04PM (#30667063)
Can you build with just these package references? because I did exactly did a while ago and I could not.
Re:.NET Myths Dispelled by davidfowl on Sunday March 13, 2022 @10:04PM (#30667778)
This is almost the correct answer, https://news.ycombinator.com/item?id=30667145 is more accurate.
Re:.NET Myths Dispelled by JamesNK on Sunday March 13, 2022 @08:17PM (#30667145)
Here you go:

  <Project Sdk="Microsoft.NET.Sdk">
  
    <PropertyGroup>
      <TargetFramework>net6.0</TargetFramework>
      <Nullable>enable</Nullable>
      <ImplicitUsings>enable</ImplicitUsings>
      <OutputType>exe</OutputType>
    </PropertyGroup>
  
    <ItemGroup>
      <FrameworkReference Include="Microsoft.AspNetCore.App" />
    </ItemGroup>
  
  </Project>
Just create an empty ASP.NET Core project, change the SDK, add a FrameworkReference and a OutputType.

Read the console args and configure the web app with a port as desired.

Re:.NET Myths Dispelled by luuio on Sunday March 13, 2022 @08:31PM (#30667236)
Thanks! This is what I was looking for (not anymore, sorry switched to a different language/ecosystem for this project).

This still begs the question, or my original point still stands—why framework reference vs. nuget reference? I'm sure there's a perfectly rationale explanation, but this in itself, having at least two "official" ways of managing dependencies (framework reference vs. package reference on nuget), it's kind of a "code smell" imo.

Re:.NET Myths Dispelled by JamesNK on Sunday March 13, 2022 @09:30PM (#30667569)
Because ASP.NET Core is a framework that is installed with .NET. It isn’t a set of Nuget packages.

Why? .NET Core 1.x tried making everything a Nuget package and it was a disaster. ASP.NET is big which meant a huge number of dependencies, easy to create conflicts, long restore and build times.

It was much better to just include ASP.NET Core in the box with .NET and simplify it down to a single reference. And most people use the web SDK which does it for you so they don't even need that.

Re:.NET Myths Dispelled by luuio on Sunday March 13, 2022 @11:25PM (#30668223)
What about the option to just make everything asp.net one single nuget package? And how come other ecosystems/languages don't have this issue? What's the difference?

The other question is, why isn't what you posted the default, but defaulting to this <Project Sdk=...>? To me, it's a lot more intuitive, signaling that each project can have multiple framework references, vs. just one SDK attribute.

Re:.NET Myths Dispelled by dahauns on Monday March 14, 2022 @07:48AM (#30671270)
>What about the option to just make everything asp.net one single nuget package? And how come other ecosystems/languages don't have this issue? What's the difference?

It's no different to e.g. create-react-app vs npm install react.

Re:.NET Myths Dispelled by JamesNK on Monday March 14, 2022 @02:49AM (#30669216)
You're searching for ways to make things more difficult than they need to be.

Make the simple easy. Make the complex possible.

Re:.NET Myths Dispelled by luuio on Monday March 14, 2022 @11:08AM (#30673326)
Disagree here. This is where I would quote the principles of two other language.

From Python: have one way of doing things

From Go: explicit is bette than implicit.

Is Sdk=... is just a short cut to pull in a handful of framework references? Or are there actually more things happening under the hood?

https://news.ycombinator.com/item?id=30673710

Re:.NET Myths Dispelled by dahauns on Monday March 14, 2022 @02:12PM (#30675889)
sigh I can't help it...

Python really isn't a great choice for making your case. "Have one obvious way of doing things" has become an ironic meme since forever, and toolchaining and dependency management has traditionally been a hot mess (yes, even with pip and virtualenv, just not the total clusterfudge it was before. And don't get me started on pipenv.), and only gotten better recently with 3rd party tools like poetry, or even conda.

Re:.NET Myths Dispelled by merb on Sunday March 13, 2022 @04:57AM (#30659374)
> I have not had any luck to pull all the right nuget dependencies to do that so far.

thats because they are not directly on nuget. at least not in the included one, you need a framework reference, since for packaging reasons they have a runtime and a "web runtime" (kinda like that)

https://docs.microsoft.com/de-de/aspnet/core/fundamentals/ta...

Re:.NET Myths Dispelled by Ducki on Sunday March 13, 2022 @04:12AM (#30659078)
You can set the ports via the kestrel options: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/se...