I am trying to place an ASP.NET WebApi endpoint on a working Azure role using the new Microsoft.AspNet.WebApi.SelfHost NuGet package. My run () working code looks something like this:
// 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);
This works fine in a dev structure, but when deploying to Azure, I get an AggregateException message from a server.OpenAsync () call containing the following exception stack:
[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
I just fulfill the role of a vanilla worker, and this seems to be a "hello world" self-service ...
The final part of my ServiceDefinition.csdef looks like this:
<Endpoints> <InputEndpoint name="Endpoint" protocol="http" port="8080" localPort="8081" /> </Endpoints>
The base address that I get from the RoleEnvironment InstanceEndpoint instance looks legit - http: //10.115. [X]. [Y]: 8081
I see an error if I use the same port / localPort (8080) or when I perform the mapping as described above.
It is clear that you can put a regular WCF service in a working role this way - is there a reason why ASP.NET WebApi SelfHost will not work in this configuration?
Omri gazitt
source share