aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

How to publish console app as a WebJob rather than Web App #7677

Closed Liero closed 6 years ago

Liero commented 6 years ago

Hi, I have a console app that I want to deploy as web job. It's based on Entropy/samples/Mvc.RenderViewToString/ sample.

The problem is, that is uses Microsoft.NET.Sdk.Web rather than Microsoft.NET.Sdk in order to render the razor views although it is not web application.

When I invoke the publish dialog, it publishes it as web app, not as web job. When I replace Microsoft.NET.Sdk.Web with Microsoft.NET.Sdk, it shows the correct web job publish dialog, but then I get runtime exception when I try to invoke the razor view.

<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <LangVersion>latest</LangVersion>
    <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
    <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\DataAccess\TACS.DataAccess.csproj" />
  </ItemGroup>

  <ItemGroup>
    <None Include="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="Settings.job">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="MyProject.ReminderWebJob.cmd">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Properties\" />
    <Folder Include="Properties\PublishProfiles\" />
  </ItemGroup>
</Project>
mkArtakMSFT commented 6 years ago

Hi @Liero. What are you trying to do? WebJobs supposed to be running in background and have no UI. Why are you trying to do that?

Liero commented 6 years ago

@mkArtakMSFT: In fact, I do have an application, that is supposed to be running on the background without UI, just like the Mvc.RenderViewToString sample does, but I'm forced to use the Web SDK anyway in order to make it work.

My WebJob is supposed to send email reminders in background, but in order to use Razor as "mail templates" I had to reference the web sdk

mkArtakMSFT commented 6 years ago

@pranavkm, can you please look into this one? Thanks!

pranavkm commented 6 years ago

but then I get runtime exception when I try to invoke the razor view.

What's the exception you're seeing? You likely need to set <PreserveCompilationContext>true</PreserveCompilationContext> in your app - my guess is Razor compilation cannot find reference assemblies to compile against.

Liero commented 6 years ago

@pranavkm, you are right about the exception and setting PreserveCompilationContext does help indeed.

This solves my situation (feel free to close the issue), but:

  1. it's still unclear to me, why project using Microsoft.NET.Sdk cannot compile the views without PreserveCompilationContext and Microsoft.NET.Sdk.Web can. What are side effects of PreserveCompilationContext.

  2. are you sure that the project SDK should determine how the app will be published? If yes, it would be good to have an workaround.

pranavkm commented 6 years ago
  1. PreserveCompilationContext results in the app including reference assemblies in the output along with metadata in the deps file. The side effect is a larger deploy\publish size. These are only ever required if you're doing runtime compilation. Most desktop applications don't do this, which is why you it's only enabled by default by the Web Sdk.

are you sure that the project SDK should determine how the app will be published?

The Web.Sdk is opinionated about the kind of target you're deploying it and shows a dialog best suited for their targets. Not sure if there's a way to change this behavior - you could try asking at https://github.com/aspnet/websdk.