.net - 在同一 Azure WebRole 上运行多个应用程序(云服务)

标签 .net azure asp.net-web-api azure-web-roles azure-cloud-services

我目前有一个带有 Web 角色的 Azure 云服务(因此它是一个安装了 IIS 的虚拟机)。此 Web 角色当前运行一个独特的 Web 应用程序(WebAPI 项目)。

我想做的是创建一个新的 Web 应用程序,但我需要它在同一个 Web 角色上运行(即同一虚拟机上的两个应用程序)以降低成本。我知道这是可能的(两个 Web 应用程序将在同一台计算机上的同一 IIS 实例上使用不同的端口运行),但我找不到有关如何使用最新版本的 Azure SDK 执行此操作的资源。

我找到了很棒的资源:

但都有点旧(<= 2013)并且不再适用。

我目前使用的是 Azure SDK 2.6。我尝试修改我的 ccproj 文件(云服务项目文件),以便两个 WebAPI 项目共享相同的 RoleName:

<ProjectReference Include="..\MyProject1.csproj">
  <Name>Website</Name>
  <Project>{...}</Project>
  <Private>True</Private>
  <RoleType>Web</RoleType>
  <RoleName>MyRole</RoleName>
</ProjectReference>
<ProjectReference Include="..\MyProject2.csproj">
  <Name>Website</Name>
  <Project>{...}</Project>
  <Private>True</Private>
  <RoleType>Web</RoleType>
  <RoleName>MyRole</RoleName>
</ProjectReference>

这不起作用,Visual Studio 中我的项目的“角色”节点显示错误(“没有关联的项目 [项目 2 名称])。

我还尝试修改我的 ServiceDefinition.csdef 文件:

<WebRole name="xxxx.Frontend.Azure" vmsize="Small">
    <Sites>
        <Site name="Web">
            <Bindings>
                <Binding name="Endpoint1" endpointName="Endpoint1" />
            </Bindings>
        </Site>
        <Site name="AnotherWeb" physicalDirectory="foo">
            <Bindings>
                <Binding name="Endpoint2" endpointName="Endpoint2" />
            </Bindings>
        </Site>
    </Sites>
    <Endpoints>
        <InputEndpoint name="Endpoint1" protocol="http" port="8080" />
        <InputEndpoint name="Endpoint2" protocol="http" port="8090" />
    </Endpoints>
</WebRole>

也不走运。

我应该如何进行?

<小时/>

编辑:

这是我当前的 csdef 文件,经过修改以支持多个站点:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="AzureCloudService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2015-04.2.6">
  <WebRole name="XXX.YYY" vmsize="Small">
    <Sites>
      <Site name="Web">
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
    </Endpoints>
    <Imports>
      <Import moduleName="RemoteAccess" />
    </Imports>
  </WebRole>
  <WebRole name="XXX.ZZZ" vmsize="Small">
    <Sites>
      <Site name="Web">
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
      <Site name="ZZZ" physicalDirectory="..\..\..\XXX.ZZZ\azure.publish">
        <Bindings>
          <Binding name="Endpoint2" endpointName="Endpoint2" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="8081" />
      <InputEndpoint name="Endpoint2" protocol="http" port="8090" />
    </Endpoints>
    <Imports>
      <Import moduleName="RemoteAccess" />
    </Imports>
  </WebRole>
</ServiceDefinition>

最佳答案

我认为 csdef 文件更改的方向正确,而 ccproj 更改的方向错误。因此,请摆脱您的第二个 ProjectReference,因为您最终只需要一个 WebRole。

应该在 ccproj 文件中添加如下行:

<MSBuild Projects="..\MyProject2\MyProject2.csproj" ContinueOnError="false" Targets="PublishToFileSystem" Properties="Configuration=$(Configuration);PublishDestination=..\Publish\MyProject2\;AutoParameterizationWebConfigConnectionStrings=False" />

显然,它有假路径,但重要的是确保 csproj 文件的路径有效,并确保 PublishDestination 路径与您在 csdef 文件中为该站点指定的 PhysicalDirectory 路径相匹配,并且请注意,PhysicalDirectory 路径和 PublishDestination 路径相对于同一位置。例如,我们在解决方案根目录中保留一个“Publish”目录,因此对我们来说 PublishDestination 看起来像

..\Publish\MyProject2\ 

物理目录看起来像

..\..\..\Publish\MyProject2

您的端点配置看起来正确,但如果无法远程访问某个角色,请确保您的 csdef 文件在端点中包含一行:

    <Endpoints>
        <InputEndpoint name="MyProject1Endpoint" protocol="http" port="80" />
        <InputEndpoint name="MyProject2Endpoint" protocol="http" port="8080" />
    </Endpoints>

...具有与每个站点的端点名称相对应的“名称”:

    <Sites>
        <Site name="MyProject1Site" physicalDirectory="..\..\..\MyProject1">
            <Bindings>
                <Binding name="MyProject1Binding" endpointName="MyProject1Endpoint" />
            </Bindings>
        </Site>
        <Site name="MyProject2Site" physicalDirectory="..\..\..\Publish\MyProject2">
            <Bindings>
                <Binding name="MyProject2Binding" endpointName="MyProject2Endpoint" />
            </Bindings>
        </Site>
    </Sites>

假设您希望每个站点都可以在其托管的同一端口上公开访问。如果出于某种原因,MyProject2 在端口 8081 上本地运行,您需要将 localPort="8081" 添加到该 InputEndpoint。

关于.net - 在同一 Azure WebRole 上运行多个应用程序(云服务),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33810252/

相关文章:

azure - 将 Azure Functions 从 "Consumption plan"切换到 "Premium plan"

.net - 在 Controller 文件夹中添加子文件夹 - .net webapi

c# - 将excel工作簿转换为内存流/字节数组以下载

c# - 创建一个通用方法来接受类型对象并返回一个新的类型对象

c# - 是否可以在我的应用程序中添加数据源连接向导?

azure - Azure 应用服务现在支持 GDI+ (System.Drawing) 吗?

git - 将 azure 数据工厂设置为 azure devops git 时出现问题

c# - web api 2.0过滤器实现customFilter的最佳选择

c# - 如何将日期正确转换为 UTC,然后再将其转换回来?

c# - 如何构建可扩展的数据模型