ASP.NET 5 web application as Azure Web Role?

后端 未结 5 1567
我寻月下人不归
我寻月下人不归 2021-02-19 18:15

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 t

相关标签:
5条回答
  • 2021-02-19 18:28

    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.

    0 讨论(0)
  • 2021-02-19 18:30

    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.

    0 讨论(0)
  • 2021-02-19 18:32

    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/

    0 讨论(0)
  • 2021-02-19 18:48

    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

    0 讨论(0)
  • 2021-02-19 18:51

    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.

    0 讨论(0)
提交回复
热议问题