ASP.NET 5 web application as Azure Web Role?

2019-04-04 16:42发布

We have a ASP.NET 5 web application in our solution.

Typically, we could right click on the Cloud Service "Roles" item and add a new role from an existing project in the solution.

But it cannot identity this project as a Web Role:

Add Web Role Project in solution...

How are we able to host a ASP.NET 5 project in a Azure Web Role?

Edit: We are using Azure SDK 2.7

5条回答
地球回转人心会变
2楼-- · 2019-04-04 17:22

Sorry, we don't support WebRoles at the moment. You might* be able to hack your way around but officially there is no support. That means that any hack you do, will be in a text editor, not from tooling.

However, you can use an Azure WebSite instead. That's fully supported.

* it might not work at all. I am not aware of anyone who did this.

查看更多
三岁会撩人
3楼-- · 2019-04-04 17:31

You probably have to build your package yourself using CSPack. Here an example using PowerShell and CSPack:

# path to cspack
$cspackPath = Join-Path $env:ProgramFiles 'Microsoft SDKs\Azure\.NET SDK\v2.7\bin\cspack.exe'

$PackagePath = 'c:\mycloudpackage.cspkg'
$serviceDefinitionFile = 'c:\myProject\ServiceDefinition.csdef'
$webRoleName = 'MyWebRole'
$webRolePath = 'c:\myProject'
$webRoleEntryPoint = 'MyWebRole.dll'

# define the cspack parameters
$cspackParameter = @(
        "/out:$PackagePath",
        $serviceDefinitionFile,
        "/role:$webRoleName;$webRolePath;$webRoleEntryPoint",
        "/sites:$webRoleName;Web;$webRolePath"
    )

# execute cspack
& $cspackExe @cspackParameter

It also allows you to host multiple sites on a single web role.

查看更多
倾城 Initia
4楼-- · 2019-04-04 17:41

It appears that it isn't officially supported as a web role at this time. It seems that it is only compatible with the web apps and not the older web role. The current work around is documented on this site: http://www.codeproject.com/Articles/331425/Running-an-EXE-in-a-WebRole-on-Windows-Azure

查看更多
混吃等死
5楼-- · 2019-04-04 17:41

I've just blogged on how to do this (with VS tooling support!) here: https://oren.codes/2017/10/16/using-asp-net-core-with-azure-cloud-services/

查看更多
祖国的老花朵
6楼-- · 2019-04-04 17:42

Edit: Cannot be done with Azure Storage Emulator...

I really struggled with this as I found the documentation seriously poor with no proper examples, so here's a full example of my scripts and files for anyone else, based on Martin Brandl's answer. You do not need webRoleEntryPoint for only a web role. Only used for worker roles.

  • Create a new empty cloud service in your project. This will generate emptyServiceConfigiguration.Cloud.csfg, ServiceConfigiguration.Cloud.csfg and ServiceDefinition.csdef files for you as well as an empty roles folder. You could also add a web/worker role to let visual studio generate the configuration in them and then just modify them accordingly.

  • Modify these files (change the physicalDirectory to your own):

ServiceDefinition.csdef:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="SoundVast.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2015-04.2.6">
  <WebRole name="WebRole1" vmsize="Small">
    <Sites>
      <Site name="Web" physicalDirectory="../SoundVast">
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
    </Sites>
    <ConfigurationSettings>
      <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" />
    </ConfigurationSettings>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
    </Endpoints>
  </WebRole>
</ServiceDefinition>

<Site name="Web" physicalDirectory="../SoundVast"> is the important line, this physicalDirectory is relative to wherever your .csdef file is located and I wanted to make my main project SoundVast the web role which was located one level up.

ServiceConfiguration.Cloud.csfg and ServiceConfiguration.Local.csfg (both can be the same):

<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="SoundVast.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2015-04.2.6">
  <Role name="WebRole1">
    <Instances count="1" />
    <ConfigurationSettings>
      <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

The important part is that the role name matches your <Role name="WebRole1"> service definition files web role name.

# path to cspack
$cspackPath = Join-Path $env:ProgramFiles 'Microsoft SDKs\Azure\.NET SDK\v2.8\bin\cspack.exe'

$PackagePath = 'C:\Users\Yamo\Documents\visual studio 2015\Projects\SoundVast\SoundVast.Azure\SoundVast.cspkg'
$serviceDefinitionFile = 'C:\Users\Yamo\Documents\visual studio 2015\Projects\SoundVast\SoundVast.Azure\ServiceDefinition.csdef'
$webRoleName = 'WebRole1'
$webRolePath = 'C:\Users\Yamo\Documents\visual studio 2015\Projects\SoundVast\SoundVast.Azure'

# define the cspack parameters
$cspackParameter = @(
        $serviceDefinitionFile,
        "/role:$webRoleName;$webRolePath;",
        "/sites:$webRoleName;SoundVast;$webRolePath",
        "/out:$PackagePath"
    )

# execute cspack
& $cspackPath @cspackParameter

A .cspkg file should now have been generated at the location of your $PackagePath.

查看更多
登录 后发表回答