通过函数计算的Custom Runtime结合HTTP触发器的方式,可以将F#的ASP.NETCore项目一键迁移,并可直接使用浏览器或者cURL等HTTP客户端工具访问函数。您可按需修改F#的示例代码实现二次开发。

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. 执行以下命令,进入项目目录。
    cd start-fc-custom-samples/f#/http-demo/
  3. 执行以下命令,安装依赖。
    make build
    输出示例:
    docker run --rm -it -v $(pwd):/tmp mcr.microsoft.com/dotnet/core/sdk:3.1 bash -c "cd /tmp/FSharpDemo && dotnet publish -r linux-x64 -c Release --self-contained true && cd /tmp/FSharpDemo/bin/Release/netcoreapp3.1/linux-x64/publish && mv FSharpDemo bootstrap && chmod +x bootstrap"
    Microsoft (R) Build Engine version 16.7.2+b60ddb6f4 for .NET
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      Determining projects to restore...
      Restored /tmp/FSharpDemo/FSharpDemo.fsproj (in 7.04 sec).
      FSharpDemo -> /tmp/FSharpDemo/bin/Release/netcoreapp3.1/linux-x64/FSharpDemo.dll
      FSharpDemo -> /tmp/FSharpDemo/bin/Release/netcoreapp3.1/linux-x64/publish/
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2021-09-09T11:06:13.262] [INFO ] [S-CLI] - Start ...
    [2021-09-09T11:06:14.391] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-09T11:06:14.392] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-09T11:06:14.392] [INFO ] [FC-DEPLOY] - Using accessKeyID: 1880770869023420****
    [2021-09-09T11:06:14.392] [INFO ] [FC-DEPLOY] - Using accessKeySecret: LTAI4G4cwJkK4Rza6xd9****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-09T11:06:15.127] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T11:06:15.371] [INFO ] [FC-DEPLOY] - Checking Function fsharpHttpFunc exists
    [2021-09-09T11:06:21.944] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
    [2021-09-09T11:06:21.973] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
      Make service custom-demo success.
      Make function custom-demo/fsharpHttpFunc success.
      Make trigger custom-demo/fsharpHttpFunc/http_t success.
    [2021-09-09T11:13:26.237] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T11:13:26.406] [INFO ] [FC-DEPLOY] - Checking Function fsharpHttpFunc exists
    [2021-09-09T11:13:26.505] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
    [2021-09-09T11:13:26.657] [INFO ] [FC-DEPLOY] - Using customDomain: auto: fc will try to generate related custom domain resources automatically
      End of request
      Deployed.
      End of request
    [2021-09-09T11:13:31.351] [INFO ] [FC-DEPLOY] - Generated auto custom domain: fsharphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    [2021-09-09T11:13:31.352] [INFO ] [FC-DEPLOY] - Creating custom domain: fsharphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    [2021-09-09T11:13:31.525] [INFO ] [FC-DOMAIN] - Creating custom domain: fsharphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    
    
    fc-fsharp_demo-fc_fsharp:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: fsharpHttpFunc
        runtime: custom
        handler: Program.main
        memorySize: 1024
        timeout: 3
      url:
        system_url: >-
          https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/custom-demo/fsharpHttpFunc/
        custom_domain:
          - domain: >-
              http://fsharphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
      triggers:
        - type: http
          name: http_t

执行结果

成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该临时域名可以访问刚部署的应用,例如执行以下命令访问应用:

curl http://fsharphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名