17

We're setting up an existing Web API server to serve site(s) alongside an existing API. I have been loosely following this article.

Here's what my Global.asax.cs looks like:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        AutoMapperConfig.RegisterMappings();

        var host = new WebHostBuilder()
           .UseKestrel()
           .UseWebRoot("wwwroot")
           .UseIISIntegration()
           .UseStartup<Startup>()
           .Build();

        host.Run();
    }
}

and Startup.cs:

public partial class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseDefaultFiles();
        app.UseStaticFiles();
    }
}

When I run the project, I get the error

Unable to load DLL 'libuv': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

libuv is a dependency of Kestrel. If I manually copy it from the packages folder to the bin folder, it works. That seems to make sense with this GitHub Issue comment. Now that project.json is being moved away from, how can I get it to copy automatically?

Some have posited that it does not know whether to use 32 or 64 bit version of libuv because the Platform is set to Any CPU in the project properties. I have tried setting it to x64 in both the solution and project settings and the problem persists.

How can I make libuv.dll copy directly to the build directory automatically?

I do not consider including the file in the project (instead of in the packages folder) and setting it to copy to the output directory a real solution, only a workaround. I'm hoping to find a solution, not a workaround.

Scotty H
  • 6,432
  • 6
  • 41
  • 94
  • 1
    Are you really trying to run an ASP.NET Core site inside of a System.Web project? Why? – davidfowl May 24 '17 at 03:57
  • @davidfowl No I'm just trying to serve static files from inside a Web API project. – Scotty H May 24 '17 at 04:01
  • An ASP.NET Core Web API project? – davidfowl May 24 '17 at 04:04
  • @davidfowl Right. – Scotty H May 24 '17 at 10:17
  • Why don't you run it as console app with IIS integration? This is how ASP.NET Core projects are meant to be used. Why are you trying to host it inside System.Web.HttpApplication? – Andrii Litvinov May 25 '17 at 20:14
  • @AndriiLitvinov I need both the routes and the files to go through port 80. Could a console app be configured to listen to port 80 if Web API already is? – Scotty H May 25 '17 at 20:29
  • My apologies, I realize I misunderstood things about the Web API I am using and said conflicting things. We have an ASP.NET .NET Framework Web API project. We were trying to use Kestrel, but I believe we now have it working using OWIN. – Scotty H May 25 '17 at 21:38
  • If you are using WebAPI 2 and OWIN I think it should be easy for you to migrate in ASP.NET Core 1.1 on Full Framework. But if you only want to server static IIS can already do it for you. And you can also configure WebAPI to serve static files, check https://github.com/Andrii-Litvinov/agile-engine-assesment/blob/master/src/MessageService.App/Startup.cs. If that sounds like something that might work for you I can post an answer. – Andrii Litvinov May 26 '17 at 05:32

4 Answers4

11

I've had a similar problem before when migrating projects. Visual Studio may misbehave a lot with mismatched projects.

Simple Answer: You need to change your projects to the MSBuild/csproj format.

To start with, if you are trying to use .NET Core in your solution, then right-click your project(s) in Visual Studio, and if you don't see Edit xxxxxx.csproj then you're likely going to have issues like the one you are reporting above.

Basically, the .NET Core project templates uses different tooling when compiling the project.

The solution below is a generic way to solve almost all issues regarding projects that try to target .NET Core, but wish to use libraries from another framework. There isn't a good tool (so far) to bridge this problem, so you're going to have to go manual-mode.


Let's get started.

The solution is quite simple (but it's a bit tedious).

Step 1: Make a new project using the new MSBuild/csproj format

Create a new project, and select ".NET Core".

step1

In almost all cases, you probably want to avoid using the ASP.NET Core Web Application template, but that's for another discussion all together.

Step 2: Target the correct framework

Right-click the project and select Edit xxxxxx.csproj

<PropertyGroup>
    <TargetFramework>net452</TargetFramework>
    <!--you will also probably want to note that you need these for a console app -->
    <OutputType>Exe</OutputType>
    <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
</PropertyGroup>

Pick a framework you want to target, and make sure that it's supported (here is a table).
I have used net452 in the above code snippet for an example. You can find out more about the naming here.

step2

Step 3. Repeat for all projects.

You're going to have to do this for every project in your solution to keep Visual Studio from behaving unexpectedly.

There really isn't much online about how to get ASP.NET Core to work well with old frameworks. Hopefully this will help you out. I wish I had this advice earlier on myself.

Svek
  • 12,350
  • 6
  • 38
  • 69
  • Repeat for all projects? Creating a new project from scratch using .Net Core and the full framework? Or just creating a new project file for each project? – Zarepheth Jul 12 '17 at 19:45
1

I just install nuget package to my project and redeploy.

Install-Package Libuv -Version 1.10.0

kepung
  • 2,102
  • 2
  • 22
  • 24
0

Try this - it could solve your problem:

var host = new WebHostBuilder()
            .UseKestrel()
            // .UseWebRoot("wwwroot") keep it if you need it
            .UseContentRoot(Directory.GetCurrentDirectory()) // this could solve your problem
            // .UseUrls("http://0.0.0.0:5000") use this if you're using nginx
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();
Mafii
  • 7,227
  • 1
  • 35
  • 55
0

Write these codes:

using Microsoft.Owin;
using Owin;
using System.Web.Http;

[assembly: OwinStartup(typeof(WebApiAndStaticFiles.OwinStartup))]

namespace WebApiAndStaticFiles
{
    public class OwinStartup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseDefaultFiles();
            app.UseStaticFiles();

            HttpConfiguration webApiConfiguration = new HttpConfiguration();
            GlobalConfiguration.Configure(webApiConfiguration); // Instead of GlobalConfiguration.Configure(WebApiConfig.Register);
            app.UseWebApi(webApiConfiguration);

        }
    }
}

and install these nuget packages:

<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net462" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net462" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net462" />
  <package id="Microsoft.Owin" version="3.1.0" targetFramework="net462" />
  <package id="Microsoft.Owin.FileSystems" version="3.1.0" targetFramework="net462" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.1.0" targetFramework="net462" />
  <package id="Microsoft.Owin.StaticFiles" version="3.1.0" targetFramework="net462" />
  <package id="Newtonsoft.Json" version="10.0.2" targetFramework="net462" />
  <package id="Owin" version="1.0" targetFramework="net462" />

You can't simply use asp.net core pipelines inside asp.net/iis hosted app pipeline. But Owin has integration for that pipeline which works like a charm.

Yaser Moradi
  • 3,267
  • 3
  • 24
  • 50