Skip to content

does “dotnet script” (csx) support ASP.NET Core app development? #602

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

Closed
jagchat opened this issue Dec 23, 2020 · 2 comments
Closed

Comments

@jagchat
Copy link

jagchat commented Dec 23, 2020

I am able to get basic examples working fine. However, I am trying to check the possibility of ASP.NET Core using "dotnet script" (just using plain csx).

main.csx:

#r "netcoreapp3.1\Microsoft.AspNetCore.dll" 
#r "netcoreapp3.1\Microsoft.AspNetCore.Hosting.dll" 
#r "netcoreapp3.1\Microsoft.Extensions.DependencyInjection.dll" 
#r "netcoreapp3.1\Microsoft.Extensions.Hosting.dll"
#load "Startup.csx"

using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

CreateHostBuilder(null).Build().Run();

Startup.csx:

#r "netcoreapp3.1\Microsoft.AspNetCore.Diagnostics.dll" 
#r "netcoreapp3.1\Microsoft.AspNetCore.Hosting.dll" 
#r "netcoreapp3.1\Microsoft.AspNetCore.Http.dll" 
#r "netcoreapp3.1\Microsoft.Extensions.DependencyInjection.dll" 
#r "netcoreapp3.1\Microsoft.Extensions.Hosting.dll" 

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGet("/", async context =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        });
    }
}

And I experience the following error:

System.BadImageFormatException: Could not load file or assembly 'Microsoft.Extensions.Hosting, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (0x80131058)
File name: 'Microsoft.Extensions.Hosting, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' ---> System.BadImageFormatException: Could not load file or assembly 'Microsoft.Extensions.Hosting, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (0x80131058)
File name: 'Microsoft.Extensions.Hosting, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at System.Reflection.Assembly.LoadFromResolveHandler(Object sender, ResolveEventArgs args)
   at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)
   at System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly, String assemblyFullName)
   at Submission#0.CreateHostBuilder(String[] args)
   at Submission#0.<<Initialize>>d__0.MoveNext() in C:\temp\sample01\main.csx:line 51
--- End of stack trace from previous location ---
   at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in /private/var/folders/6j/4hkjjhd50fg27s933nctz0480000gn/T/tmp3iI6tV/Dotnet.Script.Core/ScriptRunner.cs:line 52

What am I doing irrelevant?

@AmSmart
Copy link

AmSmart commented Jan 5, 2021

Are you looking for a way to use ASP.NET Core in the most minimal setup possible? You might want to check up Route to Code with Basic JSON Apis and @davidfowl 's single file web API

@filipw
Copy link
Member

filipw commented Jun 6, 2023

you can use the SDK feature https://github.com/dotnet-script/dotnet-script#specifying-an-sdk to specify the Web SDK

@filipw filipw closed this as completed Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants