Publish simple Asp.Net Core App (.Net Framework) with WebJob

前端 未结 4 400
萌比男神i
萌比男神i 2020-12-10 08:14

I would like to create a simple ASP.NET Core Web Application (.NET Framework) and publish a WebJob alongside it to Azure.

I\'m using Visual Studio 2017, but the resu

相关标签:
4条回答
  • 2020-12-10 08:40

    To add to Tomas' answer, if you want to set this up using git deploy, you need to modify the deploy.cmd file inside your Azure WebApp.

    There's a working example here.

    In order for this to work, make sure that you add a run.cmd file to your WebJob project:

    @echo off
    dotnet SampleWebJob.dll (you can change this to run an .exe if you want to)
    

    You also need to modify your .csproj and include

    <Target Name="PostpublishScript" AfterTargets="Publish">
        <Exec Command="dotnet publish ..\SampleWebJob\ -o $(ProjectDir)obj\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\PubTmp\Out\App_Data\Jobs\Continuous\SampleWebJob" />
    </Target>
    

    Also, if you want to automate this using git deployment, you need to modify the deploy.cmd under site/deployments/tools and include this line.

    :: 2.1 Build and publish WebJobs
    echo Building and deploying Radius365.WebJob
    call :ExecuteCmd dotnet publish "SampleWebJob\SampleWebJob.csproj" -o "%DEPLOYMENT_TEMP%\App_Data\Jobs\Continuous\SampleWebJob" -c Release
    

    You can get more information here. and here.

    Hope this helps.

    0 讨论(0)
  • 2020-12-10 08:41

    I managed to get it works in all cases to publish VS 2017 csproj using VSTS or locally with these lines:

    <Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
      <PropertyGroup>
        <!-- Ensure any PublishDir has a trailing slash, so it can be concatenated -->
        <PublishDir Condition="!HasTrailingSlash('$(PublishDir)')">$(PublishDir)\</PublishDir>
      </PropertyGroup>
    
      <PropertyGroup>
        <PublishDirFullPath>$([System.IO.Path]::GetFullPath('$(PublishDir)'))</PublishDirFullPath>
      </PropertyGroup>
    </Target>
    
    <Target Name="PostpublishScript" AfterTargets="Publish">
      <Exec Command="dotnet publish ..\SampleWebJob\ -r $(RuntimeIdentifier) -f $(TargetFramework) -c $(Configuration) -o $(PublishDirFullPath)App_Data\jobs\continuous\SampleWebJob\" />
    </Target>
    

    It ensures:

    • PublishDir has a trailing slash (could miss when using dotnet publish -o option).
    • We are using the full path (unlike previous versions, using dotnet publish [project] now change the current directory, so with a relative path like the default /bin/[configuration]/[framework]/publish the WebJob would be published in its own bin folder instead of the Web project's one).
    • We are using the same publish configuration (RuntimeIdentifier, TargetFramework, Configuration) than the Web project's one.

    Hope that helps!

    0 讨论(0)
  • 2020-12-10 09:03

    There is a way to do it, it works fine with VS 2015 and project.json (and even allows you to build and deploy to Azure from VSTS without any modifications), and kinda works in VS 2017 - publish from VS works as expected, but I didn't manage to make it work on VSTS without adding some additional build steps.

    VS 2015 + project.json

    1. Create web app project as usual, without any additional webjob related files.
    2. Create .NET Core console app for webjob.
    3. Create necessary folders for webjob in web app project. For continuous webjob named TheJob it should be App_Data/Jobs/Continuous/TheJob.
    4. Add run.cmd file to above folder with content like:

      @echo off
      The.Job.Project.Name.exe
      
    5. In web project's project.json file, add following scripts:

      "scripts": {
        "postpublish": [
          "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%",
          "dotnet publish ..\\Web.Job.Project.Name\\ -o %publish:OutputPath%\\app_data\\jobs\\continuous\\TheJob\\"
        ]
      }
      

    The trick is in the second script - it publishes webjob project directly into web app's App_Data directory, so it gets published with the website.

    VS 2017 + .csproj

    After converting projects to VS 2017 format, the scripts are getting converted, but unfortunately don't work, webjob files aren't published.

    One way I found to make it partially work is to publish webjob to web app's temporary publish directory, so it is picked up later and published from VS, but unfortunately it works only when publishing directly from Visual Studio. It doesn't work with VSTS builds.

    To do it, add following section to web app's project file:

    <Target Name="PostpublishScript" AfterTargets="Publish">
      <Exec Command="dotnet publish ..\Web.Job.Project.Name\ -o $(ProjectDir)obj\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\PubTmp\Out\App_Data\Jobs\Continuous\TheJob" />
    </Target>
    

    Hope it helps :)

    0 讨论(0)
  • 2020-12-10 09:03

    You can use this lines for VSTS builds VS2017 csproj

    <Target Name="PostpublishScript" AfterTargets="Publish">
        <Exec Command="dotnet publish ..\SampleWebJob\ -o $(PublishDir)App_Data\Jobs\Continuous\SampleWebJob" />
      </Target>
    
    0 讨论(0)
提交回复
热议问题