5

We need to self-host an Asp.Net Core application in a WPF APP, APIs are working fine but there is some issues loading cshtml views.

Here is our code: Host Builder:

public static class HostBuilder
    {
        private static IWebHost host;

        public static async Task Start()
        {
            if (host == null)
            {
                var ip = System.Net.IPAddress.Parse("127.0.0.1");
                var port = 9388;

                host = new WebHostBuilder()
                   .UseKestrel(options =>
                   {
                       options.Listen(ip, port);
                   })
                   .UseStartup<HostStartup>()
                   .Build();

                await host.RunAsync();
            }
        }

    }

Host startup:

public class HostStartup
    {
        public IConfiguration Configuration { get; }

        public HostStartup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, System.IServiceProvider serviceProvider)
        {

            app.UseMvcWithDefaultRoute();
        }
    }

Controller:

[AllowAnonymous]
    [Route("api")]
    public class LoginController : Controller
    {
        [HttpGet]
        [Route("/myview")]
        public ViewResult MyView()
        {
            var v = View("myview");
            return v;
        }

        [HttpGet]
        [Route("test")]
        public IActionResult Test()
        {
            return Ok("Good!");
        }
    }

The url http://127.0.0.1:9388/api/test is working! (web API) But when we navigate to http://127.0.0.1:9388/myview browser shows http error 500.

Am I missing something? There is no exceptions on WPF.

Alexandre
  • 7,004
  • 5
  • 54
  • 72
  • I'm confused, are you trying to send a Razor View to a WPF app and asking the WPF to render it? – Adam Vincent Jul 26 '18 at 13:59
  • Does "myview" exist? have you stepped through the debugger over 'var v = View("myview");` to see what the error is? – Adam Vincent Jul 26 '18 at 14:01
  • _There is no exceptions on WPF_ D'oh... of course there are no exception in WPF, the exceptions are in your ASP.NET Core app. Check the logs (logging to debugging should be enabled to default, in debug console during a debug session) – Tseng Jul 26 '18 at 14:19

1 Answers1

5

Ok, it's working now! You need to do a few things manually:

1) Migrate your WPF projet to VS2017 format, here is how: How-to migrate Wpf projects to the new VS2017 format (find for @stil answer)

2) Install Asp.net Core And Asp.net Core MVC

3) Edit the csproj again and change the root Sdk attribute from Microsoft.NET.Sdk to Microsoft.NET.Sdk.Razor

4) Point your WPF project to framework 4.6.2, 4.7 > doesn't work!

5) Your cshtml must have the build option "Content", but, the instruction on csproj need to be removed, otherwise, it'll not compile.

Here is an example: https://github.com/alexandrereyes/wpf-aspnetcore-mvc

Alexandre
  • 7,004
  • 5
  • 54
  • 72
  • 2
    Actually, it's a lot easier: Add a new ASP.NET Core Website to your solution. In the wizard, select "Full Framework". Change the project type to "ClassLibrary" and call the WebHost.StartAsync() (see Program.cs) in your WPF Project. – Tho Mai Jan 08 '19 at 15:08