Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JobHostConfiguration gone? #1871

Closed
johnkwaters opened this issue Sep 1, 2018 · 29 comments

Comments

@johnkwaters
Copy link

commented Sep 1, 2018

I have been using WebJobs in a dotnet core project, so I have code like this:

class Program
{
static void Main(string[] args)
{
var loggerFactory = new LoggerFactory();
using (loggerFactory)
{
IServiceCollection serviceCollection = new ServiceCollection();

            var cfg = ConfigureServices(serviceCollection, loggerFactory);

            var config = new JobHostConfiguration();
            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(10);
            config.Queues.BatchSize = 1;
            config.JobActivator = new ServiceJobActivator(serviceCollection);
            config.UseTimers();
            config.LoggerFactory = loggerFactory;

            var host = new JobHost(config);

            host.CallAsync(typeof(WebJobsMethods).GetMethod("ProcessMethod"));

            host.RunAndBlock();
        }
    }

This is deployed along with a netcore webapi to an Azure App Service. This code doesn't compile any more due to JobHostConfiguration being gone... but it probably goes way deeper than that!

Maybe the approach is completely different now? Where do I find the new pattern? Including configuring DI? My webjobs has a continuous job, some timer triggers, and a queuetrigger, configures DI, uses application insights, and logs.

@xt0rted

This comment has been minimized.

Copy link
Contributor

commented Sep 1, 2018

See my comments in #1870 (comment)

@johnkwaters

This comment has been minimized.

Copy link
Author

commented Sep 1, 2018

Great. So a few things not in that sample:

  1. Continuous web job - how do I do that?
  2. Dependency injection - where do I add my services to the DI container?
  3. How to use timertriggers - they don't get called with this code
@xt0rted

This comment has been minimized.

Copy link
Contributor

commented Sep 1, 2018

I haven't used the most recent version so someone else will have to answer the continuous job question, but DI should be handled via calling .ConfigureServices() on the HostBuilder. There's some examples of this in the hosting repo at https://github.com/aspnet/Hosting/tree/c9a4ee8b719ffb01eb44705d68efd158eb80c221/samples/GenericHostSample

@johnkwaters

This comment has been minimized.

Copy link
Author

commented Sep 1, 2018

And how to use TimerAttribute based web job functions? Don't see that in the codebase anywhere, and when I use the code in the sample project, I don't see anything configuring timers. The timers don't get called.
It used to be config.UseTimers();

@actias

This comment has been minimized.

Copy link

commented Sep 1, 2018

I found an AddTimers extension off of the IWebJobsBuilder but it takes a TimersOptions without any properties.

I'm having the same problem as you @johnkwaters. I can't get Timers to work at all and there's no configuration options for ServiceBus. Weirdly, I see a lot of options for EventHubs.

I think we're missing some extensions or something. It looks like the team at MS is moving everything into extension classes and just hasn't finished yet or something. I've noticed that certain pieces of the Azure Functions libraries and the runtime are getting updated at different times.

If you can, I'd just pin to Beta5 and skip Beta8. I'm using beta 5 right now and it's working great.

I'm sure the rest will come soon.

@isaac2004

This comment has been minimized.

Copy link

commented Sep 4, 2018

Maybe I am missing something, but where do you wire up DI in an Azure Function? Do we now need to wire up HostBuilder?

@actias

This comment has been minimized.

Copy link

commented Sep 4, 2018

@isaac2004 I was using webjobs due to limitations with Azure Functions. I believe @johnkwaters is using webjobs as well.

@isaac2004

This comment has been minimized.

Copy link

commented Sep 4, 2018

I think I may have figured out Functions for basic DI. This is a great repo
https://github.com/ArtemTereshkovich/DependencyInjectionAzureFunction

@mathewc

This comment has been minimized.

Copy link
Contributor

commented Sep 22, 2018

@xt0rted answered the original question, pointing to the sample https://github.com/Azure/azure-webjobs-sdk/blob/dev/sample/SampleHost/Program.cs showing the new host model.

@mathewc mathewc closed this Sep 22, 2018
@KalyanChanumolu-MSFT

This comment has been minimized.

Copy link

commented Oct 18, 2018

Microsoft.Azure.WebJobs 3.0.1 was released last night & I tried upgrading from 3.0.0-beta5.
I see that
JobHostConfiguration is missing
RunAndBlock() is missing from JobHost
QueueTrigger Attribute is missing.

What are the equivalents for these?

@Zordid

This comment has been minimized.

Copy link

commented Oct 23, 2018

How can you close this issue...? I am still lost without JobHostConfiguration as I don't see how to get a Logger with App Insights as described here: https://github.com/Azure/azure-webjobs-sdk/wiki/Application-Insights-Integration

@mathewc

This comment has been minimized.

Copy link
Contributor

commented Oct 23, 2018

The sample here shows the new host configuration process for a continuous WebJob. That sample doesn't add/use all possible extensions, it's meant as a starting point/sample. More notes to answer various questions added above:

  • in general all the previous config.UseXXX extension methods migrated to IHostBuilder builder.AddXXX methods. As in the sample here
  • In order for the builder.AddXXX methods to be present in your solution, you have to add the relevant nuget package for the extension you're trying to use. E.g.:
    • to use any of the Azure Storage bindings (Queues/Blobs/Tables) you have to add the Microsoft.Azure.WebJobs.Extensions.Storage package and call builder.AddAzureStorage(). You'll also generally want to call builder.AddAzureStorageCoreServices() to register the storage based implementations of various system services (singleton blob lease management, etc.). This was a big change from v2 - all the Azure Storage bindings used to be baked into the core WebJobs package. We've moved them out into their own extension package.
    • to use ServiceBus you have to add the Microsoft.Azure.WebJobs.Extensions.ServiceBus package and call builder.AddServiceBus(). Note that there was an package rename here to standardize our extension package naming. The package used to be called Microsoft.Azure.WebJobs.ServiceBus - don't use that one in v3.
    • to use Timers you have to add the Microsoft.Azure.WebJobs.Extensions package and call builder.AddTimers(). To use timers, you also need to call builder.AddAzureStorageCoreServices() because timers rely on blob leases behind the scenes
    • and so on for the other extensions. Look for the relevant Microsoft.Azure.WebJobs.Extensions.XXX package, add it to your project and call the builder AddXXX method.
  • to use Application Insights, call builder.AddApplicationInsights() as in the sample here.
  • Yes host.RunAndBlock() is gone - the new pattern is shown in the sample I linked to above (here). I.e. call await host.RunAsync() from your async main method after building the host
@Zordid

This comment has been minimized.

Copy link

commented Oct 24, 2018

Thanks @mathewc for your further explanation. I was not able to deduct this from any previous comment and found nothing in the web.
But one last question: why is the API changed with broken backwards compatibility from one beta to the next? I mean, usually I would only expect breaking changes when at least the version number indicates breaking changes.

@Zordid

This comment has been minimized.

Copy link

commented Oct 24, 2018

@mathewc The link you show for AppInsights is not really helpful - where can I get my logger from that code? I mean like shown here: https://github.com/Azure/azure-webjobs-sdk/wiki/Application-Insights-Integration
I don't want to run anything in a new fashion, I just need the logger, not a host...

@RexfelisLOC

This comment has been minimized.

Copy link

commented Jan 16, 2019

I have Visual Studio 15.9.5, which is the latest. It apparently has been months since the pattern for web job startup was changed. Why isn't a new Web Job project following the new pattern?

@mathewc

This comment has been minimized.

Copy link
Contributor

commented Jan 17, 2019

Yes, the WebJobs project template is woefully out of date. As you can see, it targets .NET Framework and version 1.x of the WebJobs SDK. We really need a new template, updated for the latest 3.x SDK. @paulbatum @fabiocav I thought there was a tooling issue for this somewhere, but I can't find it?

@fabiocav

This comment has been minimized.

Copy link
Member

commented Jan 17, 2019

I'm not aware of an issue tracking that. @vijayrkn for the WebJobs tools/templates, where should we open an issue to track moving to 3.x?

@KalyanChanumolu-MSFT

This comment has been minimized.

Copy link

commented Jan 17, 2019

Having a project template that targets the latest SDK will solve many issues.
The code samples on git and the web refer to 3.x SDK. But the project template in VS refers to an old SDK.

@vijayrkn

This comment has been minimized.

Copy link

commented Jan 17, 2019

@phenning - can you please a look?

@RexfelisLOC

This comment has been minimized.

Copy link

commented May 14, 2019

The project template for Visual Studio 2019 is still out of date.

@nicholastic

This comment has been minimized.

Copy link

commented May 28, 2019

I don't know if anyone from Microsoft is even looking at this, but if you're going to introduce breaking changes and not update the project templates (everything breaks when you update the packages to the latest version), then at least write a decent tutorial on how to get this working. After screwing around with this for several days I'm using version 2.3 and only looking at material from 2016 or thereabouts because the current state of this is completely unusable. There is just no decent information at all on how to get this working.

@paulbatum

This comment has been minimized.

Copy link
Member

commented May 28, 2019

@nicholastic This resource should help - it explicitly covers what steps need to be performed after updating to the 3.x packages:
https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started

@nicholastic

This comment has been minimized.

Copy link

commented May 29, 2019

@paulbatum Thank you Paul, but that link discusses a .NET Core console app, and I don't think I can use V3 (is V3 necessarily .NET Core?), because the web app I'm setting up Web Jobs for (and all the class libraries it uses) is .NET. Is that the case? The Web Job needs to call repositories (which use Entity Framework 6) in separate class libraries, for example. I tried calling these .NET data access methods from V2 of Azure Functions (which I understand is .NET Core, please correct me if I'm wrong) and I just couldn't easily get it to work (and I couldn't get it to continuously deploy with my web app either, but had no trouble doing this with web jobs).

In addition to my previous questions, is it the case that upgrading the Web Jobs project template to all the latest nuget packages makes it .NET Core? I'm rather confused on this point. One blog post said that V3 "looks more like .NET Core".

@paulbatum

This comment has been minimized.

Copy link
Member

commented May 29, 2019

@nicholastic WebJobs 3.x targets .NET Standard 2.0. This means you can use it from a .NET Framework application or a .NET Core 2.x application, the choice is up to you. If you want to make a .NET Framework application that uses WebJobs, then follow the same guide but with a .NET Framework console app as the starting point (instead of .NET Core).

Since you're planning on deploying this webjob to a webapp that is running a .NET Framework application, I think it makes sense to use .NET framework for the webjob too. I suspect its possible to have a mix (e.g. a .NET Framework web app with .NET core webjob) but I have not tried it and I'm not sure its worth the trouble of experimenting.

To answer your last question - no, upgrading an existing WebJobs 2.x project to 3.x does not make it a .NET Core application, hopefully that makes sense based on what I mentioned above (.NET Standard 2.0 libraries let you choose what type of application you use them from). The blog post you read was probably referring to the fact that we changed the design of WebJobs 3.x to fit more naturally with Generic Host (this post is a good intro), a concept that was born from ASP.NET Core but it is actually usable in .NET Framework apps too.

@nicholastic

This comment has been minimized.

Copy link

commented May 29, 2019

@paulbatum Thanks Paul, this is really helpful, maybe that document should say explicitly at the start that if you want to use .NET, just follow the same steps but with a .NET console app; I think that would clear up alot of confusion, I did see that document but thought it only applied to .NET Core.

But just to be really clear (this is the part that's been difficult for me), can I use Entity Framework 6 (which is .NET) in a WebJobs 3.x .NET Console app directly (which as you say targets .NET Standard 2.0), and much more importantly, call repository methods (which use EF6) that are defined in a separate .NET class library that are referenced by the WebJobs 3.x .NET Console app? Whew.

@paulbatum

This comment has been minimized.

Copy link
Member

commented May 29, 2019

Yeah as far as I know, you got that all correct and it should work. Also I reached out to some folks about whether we can make that document clearer about how WebJobs 3.x lets you choose between .NET Framework or .NET Core.

@nicholastic

This comment has been minimized.

Copy link

commented May 29, 2019

@paulbatum Great, I'll have a crack at setting up a V3 .NET project because I like being up to date. More tutorials and samples would be really nice, otherwise it's a huge time sink to even get started (lots of tutorials and samples and good documentation is a big reason why people use Microsoft: they just want to build stuff and not waste time).

As I understand it, Azure Functions V2, although targeting .NET Standard, is implemented with .NET Core, and therefore can't call code in .NET Framework class library projects?

@paulbatum

This comment has been minimized.

Copy link
Member

commented May 29, 2019

A V2 Function App is a .NET Core 2.2 application. That application will load your functions from a class library that is typically written as a .NET Core class library, but can be written as a .NET standard class library if you so choose.

An attempt to use a class library that targets .NET Framework from a .NET Core app will succeed or fail depending on what APIs that library uses. For example you can try this out yourself just by making a .NET Framework class library and a .NET Core console app. The reference should work fine initially. Then if you update your class library to use some .NET Framework types that do not exist in .NET Core, you'll start getting errors.

@nicholastic

This comment has been minimized.

Copy link

commented Jul 30, 2019

Since there's no documentation and I can't get it to work, I'd appreciate it if someone could answer my question with a working code example for a .NET V3 webjob that calls a function on a CRON schedule:

https://stackoverflow.com/questions/57264806/scheduled-net-webjob-v3-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.