Azure / Azure-Functions

1.12k stars 198 forks source link

Hosting asp.net core web api inside Functions throwing error #1114

Open kumaranvellaisamy opened 5 years ago

kumaranvellaisamy commented 5 years ago

Hi,

I tried to host aspnet core web api app inside azure functions. Followed the below article. https://blog.wille-zone.de/post/serverless-webapi-hosting-aspnetcore-webapi-in-azure-functions/

Web API as Azure Function Url https://aspnetcoreproxytest.azurewebsites.net/api/values/test

Error

InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server.

Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter+<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)

InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server.
    Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter+<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
    Microsoft.AspNetCore.HostFilteringStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
    Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
    Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
fabiocav commented 5 years ago

@kumaranvellaisamy the message above states the issue. When hosted, Azure Functions runs in-process in IIS, and needs to be properly configured, without the IIS/IIS Integration configuration.

ionutafloarei commented 5 years ago

It is properly configured. The hosted asp.net core works ok on local host IIS. I even set .UseIISIntegration() for azure function app. but the result was the same. Looks like a bug in asp.net core.

ColbyTresness commented 5 years ago

Can this be closed @fabiocav ?

ChristianWeyer commented 5 years ago

Did you get this fixed @ionutafloarei ? Thanks.

ChristianWeyer commented 5 years ago

Because you need to not have UseIIS() in your code - a bit confusing, yes ;-)

ionutafloarei commented 5 years ago

tested again and the problem is still not solved. When I run the application from local visual studio (F5 or CTRL +F5) the application works very well. I tested the Azure Functions Core Tools with Function Runtime Version: 2.0.12382.0 and the code works ok. I was able to call any web api call from localhost.

However, after I deploy the same project on Azure Function App I have the following error:

An error occurred while starting the application.

image

The code used is described here:

` public static class Function1 { private static readonly HttpClient _client;

    static Function1()
    {
        var functionPath = new FileInfo(typeof(Function1).Assembly.Location).Directory.Parent.FullName;
        Directory.SetCurrentDirectory(functionPath);
        var server = CreateServer(functionPath);
        _client = server.CreateClient();

    }

    private static TestServer CreateServer(string functionPath) =>
        new TestServer(WebHost
            .CreateDefaultBuilder()
            .ConfigureAppConfiguration((builderContext, config) =>
            {
                config
                    .SetBasePath(functionPath)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{builderContext.HostingEnvironment.EnvironmentName}.json",
                        optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables();
            })
            .CaptureStartupErrors(true) // the default
            .UseSetting("detailedErrors", "true")
            //.UseKestrel()
            //.UseIISIntegration()
            .UseStartup<Startup>());

    [FunctionName("Proxy")]
    public static Task<HttpResponseMessage> Run([HttpTrigger(
            AuthorizationLevel.Anonymous,
            "get", "post", "put", "patch", "options",
            Route = "{*x:regex(^(?!admin|debug|monitoring).*$)}")] HttpRequestMessage req,
        ILogger log)
    {
        log.LogInformation("***HTTP trigger - ASP.NET Core Proxy: function processed a request.");

        return _client.SendAsync(req);
    }
}

`

jbellmore commented 5 years ago

I can confirm I am also seeing the above error as well. Everything works great locally but when deployed to Azure function I see the same error. I have tried every combination of UseKestrel() and UseIISIntegration() (both enabled, both disabled, one enabled, etc) and it seems to have no effect.

ChristianWeyer commented 5 years ago

Yes, the issue is the .CreateDefaultBuilder() - remove that (and set up the builder manually). E.g.: https://github.com/thinktecture/serverless-microservices/blob/master/identity-server/Host.cs#L33

jbellmore commented 5 years ago

Thanks @ChristianWeyer, having the full example really helped!

Following that I was able to deploy the app to azure and it is working great.

ionutafloarei commented 5 years ago

Thank you very much @ChristianWeyer ! I confirm that it is working.

The first observation is that the custom routes on web api are not working as before for e.g. [Route("api/admin/[controller]")] -> I get a 404 NotFound.

However, Route("api/[controller]")] is working very well.

But for this issue, I have to look on your github link that you proveided first.

Thanks again!

ionutafloarei commented 5 years ago

...and now it works. Thanks again! After updated host.json and added also the [FunctionName("Root")] from Host.cs it worked very well.

peterson1 commented 5 years ago

After updated host.json and added also the [FunctionName("Root")] from Host.cs it worked very well.

Hi @ionutafloarei , I'm also having the same problem. What changes did you do to host.json to make it work? I can't refer to the link that @ChristianWeyer mentioned because it is currently broken.

ionutafloarei commented 5 years ago

this was the change that was done to the host.json. Hope this helps.

image

ChristianWeyer commented 5 years ago

This is the new link: https://github.com/thinktecture/serverless-microservices/blob/master/identity-server/Functions/Host.cs#L32

peterson1 commented 5 years ago

Thanks @ionutafloarei and @ChristianWeyer ! I got it working now.

w0ns88 commented 5 years ago

^ the link is broken again

ChristianWeyer commented 5 years ago

Sorry: https://github.com/thinktecture/serverless-microservices/blob/master/identity-service-dotnet/Functions/Host.cs#L32

tntwist commented 5 years ago

Hi, if someone looks for a way without using the TestServer. I created a dotnet new template for hosting an Asp.Net Core WebApp inside an Azure Funtion: https://github.com/tntwist/NL.Serverless.AspNetCore

Hope it can help someone.

Cubelaster commented 4 years ago

I am experiencing this issue on App Service. Managed to narrow it down to inProcess vs outOfProcess, but am stuck. No combination of IIS or Kestrel works. Also, I'm in desperate need of help

Cubelaster commented 4 years ago

image Ok, I managed to solve this issue. As someone stated somewhere, there should NOT be either IIS or Kestrel mentions. And then it works. However, this is most probably a bug.