-1

I used to make REST APIs using the "ASP.NET Core Web Application" template in Visual Studio 2019, but I don't want to have to install .NET Core to use my server application, so I used the .NET Framework template.

However, the former always created an executable file for me to run even outside the IDE. The server would run in a console window. The .NET Framework template however only generates the assembly as a DLL. There is no use in changing to "Console Application" in project settings since the template has no Main() function.

How would I make my server run outside the IDE? I'd love to know how to get the server started from a Main() function. I could then implement a ServiceBase class to turn my program into a service, which is ultimately what I want.

I know that there is a way to "deploy" the server to run with issexpress, but I'd rather have it run itself from an executable.

Andy
  • 12,859
  • 5
  • 41
  • 56
nc404
  • 135
  • 2
  • 8
  • There isn't much difference between an exe and a dll. The exe just has an entry point main and a different extension. So either you can create a new console application that just calls the dll, or add an entry point main do your dll which will execute by double clicking the dll. See https://learn.microsoft.com/en-us/cpp/build/reference/entry-entry-point-symbol?view=msvc-160 or https://learn.microsoft.com/en-us/dotnet/framework/interop/specifying-an-entry-point – jdweng May 21 '21 at 15:34
  • 1
    You just run the DLL it produces with `dotnet run` command. More information [here](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-run). – Andy May 21 '21 at 15:44
  • @jdweng I understand but what should I call in my Main() function to get the server started? I currently have no clue. Seems like Global.asax currently holds the "main class" of the program, derived from System.Web.HttpApplication, but there don't seem to be any useful "Start()" methods in it. – nc404 May 21 '21 at 15:51
  • "The .NET Framework template" <- are we talking .NET Core or .NET Framework? You tagged the first. – JHBonarius May 21 '21 at 15:51
  • It's a little bit ambiguous when you say "...but I want to have to install .NET Core to use my server application...". Is it possible your interest is just the opposite? – V.Lorz May 21 '21 at 15:56
  • @Andy Called from the project's directory? For me it just gives me an error. Maybe it should take some more arguments. – nc404 May 21 '21 at 15:56
  • @V.Lorz Oh god, my bad. I meant DON'T. – nc404 May 21 '21 at 15:56
  • You already have a good answer to your question [here](https://stackoverflow.com/a/67640259/15086628). Give it a try. – V.Lorz May 21 '21 at 15:58
  • @V.Lorz The answer is about .NET Core, my question is about the .NET Framework template. Sorry if my small earlier mistake caused some confusion. – nc404 May 21 '21 at 16:01
  • You also tagged the question wrong then! Please correct... You will need to run something to host. For instance Microsoft IIS. No, an ASP.NET Framework app is not complete nor stand-alone. You need a server. – JHBonarius May 21 '21 at 16:05
  • First off: this is completely false: *... but I don't want to have to install .NET Core to use my server application, so I used the .NET Framework template...* If you publish .NET Core right, it's self contained and you don't need the runtime installed on the server. – Andy May 21 '21 at 16:07
  • Why is it false? – V.Lorz May 21 '21 at 16:08
  • @Andy What do you mean? It's true I used that template before. – nc404 May 21 '21 at 16:09
  • I'm saying: the reason you aren't using .NET Core is absolute BS. You say in your question "I won't use .NET Core because I don't want to have to install the runtimes on the server" . Since when was it a requirement the runtimes be installed to run a .NET Core application? – Andy May 21 '21 at 16:15
  • @Andy Well of course you need the framework. I tried running a Web API server before and I couldn't run it until I installed the .NET Core framework with ASP.NET web api or something like that, from the microsoft website. – nc404 May 21 '21 at 16:19
  • Because you published it wrong. Try doing this: `dotnet publish MyProgram.sln -c Release -r win-x64` and run it. [This guy](https://stackoverflow.com/questions/32037695/do-net-core-apps-require-the-net-runtime-installed-on-the-target-machine) explains it well. – Andy May 21 '21 at 16:20
  • 1
    Being or not a requirement depends on how you want to deploy your application and your target operating system. As you point out, if you deploy as single executable you don't need the runtime to be installed, you have a copy of it with each application you deploy. But this is not always convenient. IMO, for every runtime update that becomes available you'll need to update and recompile+publish your application. – V.Lorz May 21 '21 at 16:27
  • I've been doing it that way for over 4 years and it's far from inconvenient. – Andy May 21 '21 at 16:27
  • There was an answer for this question [here](https://stackoverflow.com/a/67640259/15086628) but it seems it was deleted. – V.Lorz May 21 '21 at 16:28
  • That was *my* answer and I deleted it because he said he wanted to use .NET Framework, not .NET Core. This entire question is screwy. – Andy May 21 '21 at 16:29
  • Well, you crafted a very nice and good answer, yet the OP says he/she wants to use the framework. Maybe the OP would reconsider moving away from .Net Core. – V.Lorz May 21 '21 at 16:31
  • @V.Lorz -- that's what I have been trying to do lol.. but you're kinda fighting me on it. – Andy May 21 '21 at 16:31
  • @Andy Thank you so much. I'll try it out, will probably switch back to .NET Core if it works. If what JHBonarius is saying is true I think it's much more convenient. There is also the problem that I can't load System.Data.Linq.dll in a .NET Core program, tho, but that is another problem. – nc404 May 21 '21 at 16:33
  • 1
    I'll undelete my original answer if you want to try .NET Core again. – Andy May 21 '21 at 16:33
  • An exe file (or dll) is designed to run in windows. There is a specification on how the application interfaces with the operating system. Part of the specification is the entry point and when you run the c# compiler it create an executable that meets the windows requirements. When you specify to the c# compiler an entry point the compiler create the exe (or dll) so the entry point is the first location in the code to run. – jdweng May 21 '21 at 18:38
  • specific asp.net web application wants to be run here - that needs IIS, or IIS express. For some strange reason, people build a web site in Visual Studio - it just seems to work, and the concept of requiring a whole web server is often lost in the narrative. The user NEEDS to be running IIS - and no simple exe going to achieve this goal. Talk of a simple .dll, or single .exe simply does not apply at all here - it is a turn down the wrong garden path based on not realizing that IIS is required here. – Albert D. Kallal May 21 '21 at 22:36

1 Answers1

2

dotnet run

.NET Core produces a DLL that can be run using dotnet run. For example:

dotnet run MyProgram.dll

Please note: You need the .NET Core runtimes installed if you use this method.

dotnet publish

You can get an EXE stub that will do this all for you by publishing your project. For example:

dotnet publish MyProgram.sln -c Release -r win-x64

That will create a release-mode Windows x64 compilation that contains a file named MyProgram.exe that you can execute normally.

Please note: You do not need the .NET Core runtimes installed if you use this method.

single-file publish

You can even make it easier to distribute by compiling to a single file:

dotnet publish MyProgram.sln -r win-x64 -p:PublishSingleFile=true --self-contained true

That will produce one file: MyProgram.exe.

Please note: You do not need the .NET Core runtimes installed if you use this method.

Andy
  • 12,859
  • 5
  • 41
  • 56
  • I know but I asked about .NET Framework not .NET Core. The .NET Core template actually produces an executable, as I mentionned. `dotnet run` doesn't work with the DLL I have. – nc404 May 21 '21 at 15:59
  • you had .NET Core in your tags. – Andy May 21 '21 at 16:05
  • This applies to a desktop app. for a web application, they STILL must be running IIS, and there is no simple and single .exe option. The poster likly was confused that in the past they were able to run a asp.net web site from a single .exe. They could run desktop applications - but never was some simple .exe produced for the WHOLE web site. There is certainly a difference between a web application, and a web site. The web application means that the whole site and hooks to IIS occur at a deeper level. And in this case, a single .dll is produced for all code in the site, but not a single .exe – Albert D. Kallal May 21 '21 at 22:25
  • @AlbertD.Kallal -- i didn't get that from his question at all. He specifically says *"I could then implement a ServiceBase class to turn my program into a service, which is ultimately what I want."* -- which tells me he wants to run a Windows service. Maybe he means WebAPI service. Who knows. At the end of the day he asked for an executable file that could execute in lieu of a DLL. – Andy May 21 '21 at 22:29
  • Yes, and the question title is this: How to run a ASP.NET web application (.net Frame Work) outside of the IDE ---- as I stated, with the IDE the web site works like magic - but STILL requires IIS. He wants to run the asp.net web application OUTSIDE of the IDE - and that means running IIS againast that web site. Crystal clear to me. – Albert D. Kallal May 22 '21 at 00:09