在 Azure Web 应用中运行自承载 OWIN 应用程序


由于

名为 HttpPlatformHandler 的 IIS8 功能,可以在 Azure Web 应用上运行 suave.io 应用。我尝试以相同的方式运行自托管的 OWIN 应用程序,但在启动时出现异常:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.HttpListenerException: Access is denied
   at System.Net.HttpListener.SetupV2Config()
   at System.Net.HttpListener.Start()
   at Microsoft.Owin.Host.HttpListener.OwinHttpListener.Start(HttpListener listener, Func`2 appFunc, IList`1 addresses, IDictionary`2 capabilities, Func`2 loggerFactory)
   at Microsoft.Owin.Host.HttpListener.OwinServerFactory.Create(Func`2 app, IDictionary`2 properties)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(IAppBuilder builder)
   at Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer(StartContext context)
   at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context)
   at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions options)
   at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider services, StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.Start(StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.Start[TStartup](StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.Start[TStartup](String url)
   at WebAppSample.Program.Main(String[] args) in c:UserseggerWorkspaceWebAppSampleWebAppSampleProgram.cs:line 14

看来我不能开口。我的 web.config 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <remove name="httpplatformhandler" />
            <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
        </handlers>
        <httpPlatform
            stdoutLogEnabled="true"
            stdoutLogFile="site.log"
            startupTimeLimit="20"
            processPath="%HOME%sitewwwrootWebAppSample.exe"
            arguments="%HTTP_PLATFORM_PORT%">
        </httpPlatform>
    </system.webServer>
</configuration>

我使用HTTP_PLATFORM_PORT在正确的端口上侦听。我的 Web 应用按如下方式启动服务器:

class Program
{
    static void Main(string[] args)
    {
        var port = int.Parse(args[0]);
        var url = string.Format("http://127.0.0.1:{0}", port);
        Console.WriteLine("Starting web app at {0}", url);
        using (WebApp.Start<Startup>(url))
        {
            Console.ReadLine();
        }
    }
}
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseErrorPage();
        app.UseWelcomePage("/");
    }
}

URL 似乎没问题,因为我得到这样的输出:Starting web app at http://127.0.0.1:32880 .

web.config 和所有二进制文件都在根目录中,我使用本地 git 存储库发布了该应用程序。

为什么我无法使用 OWIN 打开端口?与 suave.io 样本有什么不同?

编辑:我刚刚看到有一个支持这个确切场景的请求:https://feedback.azure.com/forums/169385-web-apps-formerly-websites/suggestions/4606121-support-owin-self-hosting-in-azure-websites

我遇到了同样的问题。只需以管理员身份运行Visual Studio即可解决。

(可能还需要重启 Azure 模拟器)

最新更新