ASP.NET 5 Web 应用程序作为 Azure Web 角色?
ASP.NET 5 web application as Azure Web Role?
我们的解决方案中有一个 ASP.NET 5 Web 应用程序。
通常,我们可以右键单击 Cloud Service "Roles" 项,然后从解决方案中的现有项目中添加一个新角色。
但它无法将此项目标识为 Web 角色:
我们如何在 Azure Web 角色中托管 ASP.NET 5 项目?
编辑:我们正在使用 Azure SDK 2.7
目前官方似乎不支持将其作为 Web 角色。它似乎只与网络应用程序兼容,而不兼容旧的网络角色。当前的解决方法记录在此站点上:
http://www.codeproject.com/Articles/331425/Running-an-EXE-in-a-WebRole-on-Windows-Azure
您可能必须使用 CSPack 自己构建您的包。这里有一个使用 PowerShell 和 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
它还允许您在一个网络角色上托管多个站点。
抱歉,我们目前不支持 WebRoles。你可能*能够破解你的方式,但正式没有支持。这意味着您所做的任何修改都将在文本编辑器中进行,而不是通过工具进行。
但是,您可以改用 Azure 网站。完全支持。
* 它可能根本不起作用。我不知道有谁这样做过。
编辑:无法使用 Azure 存储模拟器完成...
我真的很难解决这个问题,因为我发现文档非常糟糕,没有合适的示例,所以这里是我的脚本和文件的完整示例,供其他人使用,基于 Martin Brandl 的回答。
对于 Web 角色,您不需要 webRoleEntryPoint
。仅用于辅助角色。
在您的项目中创建一个新的空云服务。这将为您生成空的 ServiceConfigiguration.Cloud.csfg
、ServiceConfigiguration.Cloud.csfg
和 ServiceDefinition.csdef
文件以及一个空的角色文件夹。您还可以添加一个 web/worker 角色,让 visual studio 在其中生成配置,然后相应地修改它们。
修改这些文件(将physicalDirectory
改成你自己的):
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">
是重要的一行,这个 physicalDirectory
是相对于你的 .csdef
文件所在的位置的,我想让我的主项目 SoundVast
成为网络角色位于上一层。
ServiceConfiguration.Cloud.csfg
和ServiceConfiguration.Local.csfg
(两者可以相同):
<?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>
重要的部分是角色名称与您的 <Role name="WebRole1">
服务定义文件网络角色名称匹配。
# 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
一个 .cspkg
文件现在应该已经在您的 $PackagePath
.
的位置生成了
我刚刚在博客上介绍了如何执行此操作(使用 VS 工具支持!):https://oren.codes/2017/10/16/using-asp-net-core-with-azure-cloud-services/
我们的解决方案中有一个 ASP.NET 5 Web 应用程序。
通常,我们可以右键单击 Cloud Service "Roles" 项,然后从解决方案中的现有项目中添加一个新角色。
但它无法将此项目标识为 Web 角色:
我们如何在 Azure Web 角色中托管 ASP.NET 5 项目?
编辑:我们正在使用 Azure SDK 2.7
目前官方似乎不支持将其作为 Web 角色。它似乎只与网络应用程序兼容,而不兼容旧的网络角色。当前的解决方法记录在此站点上: http://www.codeproject.com/Articles/331425/Running-an-EXE-in-a-WebRole-on-Windows-Azure
您可能必须使用 CSPack 自己构建您的包。这里有一个使用 PowerShell 和 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
它还允许您在一个网络角色上托管多个站点。
抱歉,我们目前不支持 WebRoles。你可能*能够破解你的方式,但正式没有支持。这意味着您所做的任何修改都将在文本编辑器中进行,而不是通过工具进行。
但是,您可以改用 Azure 网站。完全支持。
* 它可能根本不起作用。我不知道有谁这样做过。
编辑:无法使用 Azure 存储模拟器完成...
我真的很难解决这个问题,因为我发现文档非常糟糕,没有合适的示例,所以这里是我的脚本和文件的完整示例,供其他人使用,基于 Martin Brandl 的回答。
对于 Web 角色,您不需要 webRoleEntryPoint
。仅用于辅助角色。
在您的项目中创建一个新的空云服务。这将为您生成空的
ServiceConfigiguration.Cloud.csfg
、ServiceConfigiguration.Cloud.csfg
和ServiceDefinition.csdef
文件以及一个空的角色文件夹。您还可以添加一个 web/worker 角色,让 visual studio 在其中生成配置,然后相应地修改它们。修改这些文件(将
physicalDirectory
改成你自己的):
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">
是重要的一行,这个 physicalDirectory
是相对于你的 .csdef
文件所在的位置的,我想让我的主项目 SoundVast
成为网络角色位于上一层。
ServiceConfiguration.Cloud.csfg
和ServiceConfiguration.Local.csfg
(两者可以相同):
<?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>
重要的部分是角色名称与您的 <Role name="WebRole1">
服务定义文件网络角色名称匹配。
# 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
一个 .cspkg
文件现在应该已经在您的 $PackagePath
.
我刚刚在博客上介绍了如何执行此操作(使用 VS 工具支持!):https://oren.codes/2017/10/16/using-asp-net-core-with-azure-cloud-services/