ASP.NET WebApi SelfHost服务在HTTP URL注册时失败

Omr*_*itt 7 c# wcf azure wcf-web-api asp.net-web-api

我正在尝试使用新的Microsoft.AspNet.WebApi.SelfHost NuGet包在Azure辅助角色上托管ASP.NET WebApi端点.我的worker的Run()代码看起来大致如下:

// Endpoint is defined as in ServiceDefinition.csdef as 
//   HTTP, external port 8080, internal port 8080 (or 8081 - error both ways)
RoleInstanceEndpoint externalEndPoint =
    RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint"]; 
string baseAddress= String.Format("http://{0}", externalEndPoint.IPEndpoint);
var maxsize = 1024 * 1024;  
var config = new HttpSelfHostConfiguration(baseAddress) 
{ 
    MaxBufferSize = maxsize, MaxReceivedMessageSize = maxsize 
};
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

// Create and open the server
var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();

// keep the worker thread alive
while (true)
    Thread.Sleep(Timeout);
Run Code Online (Sandbox Code Playgroud)

这在开发结构中工作正常,但在部署到Azure时,我从server.OpenAsync()调用获得了一个AggregateException,其中包含以下异常堆栈:

[0] One or more errors occurred.
[1] HTTP could not register URL http://+:8081/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
[2] Access is denied
Run Code Online (Sandbox Code Playgroud)

我只是扮演一个香草工人的角色,这似乎是自我主持人的"你好世界"......

我的ServiceDefinition.csdef的端点部分如下所示:

<Endpoints>
  <InputEndpoint name="Endpoint" protocol="http" port="8080" localPort="8081" />
</Endpoints>
Run Code Online (Sandbox Code Playgroud)

我从RoleEnvironment InstanceEndpoint获取的baseAddress看起来是合法的 - http://10.115.[X].[Y]:8081

我看到失败是否使用相同的端口/ localPort(8080)或我执行映射时,如上所述.

很明显,以这种方式可以在工作者角色中托管传统的WCF服务 - ASP.NET WebApi SelfHost在这种配置中无法工作的原因是什么?

dun*_*nry 4

默认情况下,为了安全起见,RoleEntryPoint 在权限极小的用户帐户下运行。正如错误所示,由于这些权限,它无法保留该端口。您在这里有两个选择:

  1. 通过将 Runtime 元素添加到您的角色定义中,以 SYSTEM 身份运行 Worker 进程(即<Runtime executionContext="elevated"/>
  2. 创建一个运行提升的启动脚本并为您保留该端口。

为了解决问题(如果是权限问题则进行故障排除),执行#1 是测试它的快速方法。

编辑:我似乎记得在进行通配符保留时存在 WCF 和 Windows Azure 的权限问题。使用完整主机名时它曾经工作正常,例如

host.AddServiceEndpoint(
 typeof(IEchoService), new BasicHttpBinding(BasicHttpSecurityMode.None) { HostNameComparisonMode = HostNameComparisonMode.Exact }, "echo");
Run Code Online (Sandbox Code Playgroud)