在Azure Web Apps中运行自托管OWIN应用程序

时间:2015-04-29 06:47:19

标签: azure owin azure-web-sites kudu suave

由于名为It is possible的IIS8功能,

suave.ioAzure Web Apps上运行HttpPlatformHandler个应用。我尝试以相同的方式运行自托管的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:\Users\egger\Workspace\WebAppSample\WebAppSample\Program.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%\site\wwwroot\WebAppSample.exe"
            arguments="%HTTP_PLATFORM_PORT%">
        </httpPlatform>
    </system.webServer>
</configuration>

我使用HTTP_PLATFORM_PORT来侦听正确的端口。我的网络应用程序启动服务器如下:

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("/");
    }
}

网址似乎没问题,因为我得到如下输出:Starting web app at http://127.0.0.1:32880

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

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

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

1 个答案:

答案 0 :(得分:5)

我有同样的问题。只需将Visual Studio作为Admin运行即可解决。

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