Search code examples
azure-service-fabricasp.net-core-webapiweblistener

Using WebListener in ASP.NET Core in Azure Service Fabric


I want to use WebListener in an ASP.NET Core Application in Azure Service Fabric.

I have did the following:

Task<string> ICommunicationListener.OpenAsync(CancellationToken cancellationToken)
{
    var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(_endpointName);

    string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}";

    _webHost = new WebHostBuilder().UseWebListener()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .UseUrls(serverUrl)
                .Build();

    _webHost.Start();

    return Task.FromResult(serverUrl);
}

Unfortunatelly this does not work. I created a WEB API Controller and when I call this it Returns 404.

When I use Kestrel it works:

_webHost = new WebHostBuilder().UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseStartup<Startup>()
    .UseUrls(serverUrl)
    .Build();

This is my projects.json:

{
"dependencies": {  
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.Server.WebListener": "1.1.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.ServiceFabric": "5.3.301",
    "Microsoft.ServiceFabric.Data": "2.3.301",
    "Microsoft.ServiceFabric.Services": "2.3.301"
},

"commands": {
    "weblistener": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener"
 },

 "tools": {
 },

"frameworks": {
    "net452": {
      "dependencies": {
       }
    }
},

"buildOptions": {
     "emitEntryPoint": true,
     "preserveCompilationContext": true
 },

"publishOptions": {
   "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
   ]
},

"scripts": {
}
}

Am I missing something? The reason to use Web Listener is that I want to use Windows Authentication with my intranet application so that users will not have to manually enter a username and Password.


Solution

  • Try using a wildcard host, like this:

    string serverUrl = $"{endpoint.Protocol}://+:{endpoint.Port}";