I've upgraded my .NET (not .NET Core) WebJob from V2 (which was working fine) to V3. I'm having trouble getting it to run. I just want the webjob to call a function I've written according to this CRON schedule: "0 0 8,10,12,14,16,18,20 * * *". The website it's running with is .NET also, not .NET Core.
How do I do this? I just want a simple working .NET code sample. I've seen this question New Azure WebJob Project - JobHostConfiguration/RunAndBlock missing after NuGet updates and this example https://github.com/Azure/azure-webjobs-sdk/blob/00686a5ae3b31ca1c70b477c1ca828e4aa754340/sample/SampleHost/Program.cs and this documentation https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#triggers but none of it is helpful.
Actually the use of .Net webjob or .Net Core webjob are almost same, cause the 3.0 sdk targets .NET standard 2.0. I test with Microsoft.Azure.WebJobs -version 3.0.4 and Microsoft.Azure.WebJobs.Extensions -version 3.0.1, i think your TimerTrigger doesn't work cause you lost call the AddTimers extension methods. You could find the description here:Binding types.
Other package I use:
Microsoft.Extensions.Logging -version 2.2.0
Microsoft.Extensions.Logging.Console -version 2.2.0
This is my main method:
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
namespace ConsoleApp20
{
class Program
{
static void Main(string[] args)
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddTimers();
});
builder.ConfigureLogging((context, b) =>
{
b.AddConsole();
});
var host = builder.Build();
using (host)
{
host.Run();
}
}
}
}
This is my Functions.cs:
public static void Run([TimerTrigger("0 0 8,10,12,14,16,18,20 * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
And use a appsettings.json(Don't forget set the Copy to Output Directory to Copy always) to configure the storage connection string.
Here is the result:
Scheduled .NET WebJob V3 example
No, it is not possible.
WebJob 3.x only support for .NET Core. Here is the article.
Here is a SO thread about Webjob 3.x for .net core to do some settings.
Related
I am migrating some legacy .Net code from an App Service into an Azure Function triggered by a Service Bus topic. This will run under .Net Framework 4.8 as an Azure function V1. I am running into the error "No job functions found. Try making your job classes and methods public" etc.
To test this, I created a brand new Azure function using the Visual Studio 2022 template, and it does not have this warning. However, looking at the NuGet packages, I can see that the template installs Microsoft.Azure.WebJobs.ServiceBus, which is deprecated.
I removed that package and installed the new Microsoft.Azure.WebJobs.Extensions.ServiceBus package that is recommended.
The only change to the code that I had to make was to remove the AccessRights attribute, since that's not supported any more.
Now, when I run the Azure Function, I get the "No job functions found" warning.
The warning suggests that I am supposed to call config.UseServiceBus() somewhere. I've seen from some questions on this where people do this in Program.cs in the Main function. However, this is a DLL library project, so there is no Program.cs or startup code.
I've looked for a Migration Guide and searched for similar problems, but everything I've found is for .Net 5, not Framework 4.8.
What is the correct way to initialize a .Net48 Azure Function built as a DLL? Should I convert this project to a Console Program instead, and initialize in Main()?
Edit:
This is the template-generated function that works before updating the library:
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.ServiceBus.Messaging;
namespace AFTest
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([ServiceBusTrigger("testtopic", "testsubscription", AccessRights.Listen, Connection = "ServiceBusConnectionString")]string mySbMsg, TraceWriter log)
{
log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
}
}
Here's the slightly modified code (removing the Access attribute) that doesn't work:
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
namespace AFTest
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([ServiceBusTrigger("testtopic", "testsubscription", Connection = "ServiceBusConnectionString")]string mySbMsg, TraceWriter log)
{
log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
}
}
I'm developing an Azure webjob using .NET Framework but when I'll run this localy, I've this exception after the startup.
Microsoft.Azure.WebJobs.Host.Listeners.FunctionListenerException: The listener for function [Name of function] was unable to start.
Inner exception:
ArgumentNullException: Value cannot be null.
Parameter name: connectionString
This is the code inside the Program class of the web job.
static void Main()
{
HostBuilder builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddTimers();
});
using (IHost host = builder.Build())
{
host.Run(); // <-- error happens on this line
}
}
Inside the App.config I've added next two connection strings:
<add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName=[Name];AccountKey=[Key]" />
<add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=https;AccountName=[Name];AccountKey=[Key]" />
With [Name] and [Key] as the account name en key from a live environment.
Also when I change the connection strings to UseDevelopmentStorage=true, I'm getting the same exception.
How could I solve this?
Update:
If you're using Azure WebJobs SDK of version 3.x with .NET Framework, there is an issue: Version 3.x is incompatible with .NET Framework and PackageReference.
So there're several ways as workaround:
Directly use the Azure WebJobs template from Visual studio, it's based on .net framework.
You can add an appsettings.json file (Note: right click the file → select Properties → then set "Copy to Output Directory" to "Copy if newer") to the .NET Framework project, it will work(based on 1, it will throw errors, but it can work). The JSON file looks like below:
{
"AzureWebJobsStorage": "{storage connection string}"
}
The best solution is to use .NET Core with WebJobs SDK of version 3.x.
Original answer:
If you're using WebJob SDK version 3.x, I suggest you should create a .NET Core console project instead of .NET Framework console project, then you should follow this official doc.
First, create a .NET Core console app. And install all the necessary packages mentioned in the official doc.
Your program.cs:
static void Main()
{
HostBuilder builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
b.AddTimers();
});
using (IHost host = builder.Build())
{
host.Run();
}
}
Then create a function, please refer to this section.
Then add an appsettings.json file (Note: right click the file → select Properties → then set "Copy to Output Directory" to "Copy if newer"), add the AzureWebJobsStorage inside it:
{
"AzureWebJobsStorage": "{storage connection string}"
}
Please let me know if you still have more issues about that.
I have a set of Azure Functions, written in C#, and running on Azure Function v2 runtime (.NET Core 2.2), which work just fine.
Now I was going to create a new set of Azure Function and I want to use the v3 runtime (.NET Core 3.1). However, when "transferring" the code from my existing code base, I ran into this problem: I have a Startup.cs file that's setting up the Dependency Injection for the Azure Functions, and this is what it looked like in my Azure Function v2 project:
[assembly: FunctionsStartup(typeof(MyCorp.MyProject.Infrastructure.Startup))]
namespace MyCorp.MyProject.RisWebportalService.Infrastructure
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
// more lines here, setting up DI
}
}
}
When I tried to use this in the Azure Function v3 project, I get an error on the builder.Services.AddHttpClient(); line - seems IFunctionsHostBuilder in v3 doesn't have this extension method anymore......
So what do I do instead? I cannot seem to find any really useful documentation on any breaking changes in Azure Function runtime between v2 and v3 - any pointers?
You should install the package Microsoft.Extensions.Http, version 3.1.3.
The test result after installing it:
I found the same issue here.
I am creating an application framework that can be shared between .Net Core 1.2, .Net Core 2.0 and .NET Framework 4.6+. So I choose the target framework of my project as .NET Standard. In my framework project I have factory class that is used to send a text message or email. On application startup, each application configures the factory with the supported services as singleton instance with DI framework.
public class MyFactory : IMyFactory
{
private ILogger _logger;
private IDictionary<string,IMyService> _container = new Dictionary<string,IMyService>();
// this method is called on application startup to configure supported services
public void Configure(string[] keys, ILogger logger)
{
foreach(var key in keys)
{
if(key == "text")
{
container.Add(key,new TextMessageService());
}
if(key == "email")
{
container.Add(key,new EmailService());
}
}
}
//application call this method to send message
public bool SendMessage(string key, string message)
{
// we don't want application to stop when publish fail, so we add try-catch and log the message
var flag = false;
try
{
var service= container[key];
service.Publish(message);
flag = true;
}
class(Exception ex)
{
_logger.Error(ex);
}
return flag;
}
}
Issue: the publish method could fail for any reason. And in such case I don't want application to stop, instead the framework should log the error message. My problem is since the framework can be shared between different .NET frameworks, I don't know which type of ILooger I should be using that can be shared between .NET Core and .NET Full.
I am trying to avoid creating my own ILogger interface. Also currently all applications are using Serilog but in future that could change.
Can Microsoft.Extensions.Logging be used here?
Yes it can. If you have to support netcore 1, then
Install-Package Microsoft.Extensions.Logging -Version 1.1.2
would work:
https://www.nuget.org/packages/Microsoft.Extensions.Logging/1.1.2
tells that it requires netstandard1.1
https://github.com/dotnet/standard/blob/master/docs/versions.md tells you
that all of your three target platforms implement netstandard1.1
But better still your reusable component only need rely on the Abstractions:
Install-Package Microsoft.Extensions.Logging.Abstractions -Version 1.1.2
Your component only need reference ILogger, and the applications that use your component are not tied to using the MS extensions logger. In particular, if you look at the dependencies for
Serilog.Extensions.Logging -Version 2.0.2
At https://www.nuget.org/packages/Serilog.Extensions.Logging/ , you can see that it depends on Abstractions but doesn't depend on the MS Extensions logger.
Serilog does depend on netstandard1.3. But again the netstandard version page tells you all your targets support it.
The applications using your framework can then carry on using Serilog, so long as they know how to wrap a serilogger as a MS.Extensions ILogger. the SerilogLoggerProvider in Serilog.Extensions.Logging does the trick:
var msFrameworkLogger= new SerilogLoggerProvider(myExistingSerilog).CreateLogger("name");
As stated in official document, I am trying to implement UseOwin in the Startup.cs.I am trying to use/port IAppBuilder (Microsoft.Owin.Builder.AppBuilder) inside IApplicationBuilder (Microsoft.AspNetCore.Builder.IApplicationBuilder). I had legacy code written using IAppBuilder running fine on .Net Framework 4.5.
I have seen couple of examples about using IAppBuilder in IAplicationBuilder e.g. example 1 example 2. These attempts were about .netcore 1.1 and not .net core 2.0. May be this is the reason i am unable to port.
Please share your thoughts whether i am trying to achieve something not possible at the moment in .net core 2.0 or there is some error in my code.
Note:
I am using dotnetcore 2.0 with Visual Studio 2017
Error
I am getting following error.
return owinAppBuilder.Build,
Task>>(); TypeLoadException: Could not load type
'System.Security.Cryptography.DpapiDataProtector' from assembly
'System.Security, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
My attempt
app.UseOwin(setup => setup(next =>
{
var owinAppBuilder = new AppBuilder();
var aspNetCoreLifetime =
(IApplicationLifetime)app.ApplicationServices.GetService(typeof(IApplicationLifetime));
new AppProperties(owinAppBuilder.Properties)
{
OnAppDisposing = aspNetCoreLifetime?.ApplicationStopping ?? CancellationToken.None,
DefaultApp = next,
AppName = "test"
};
// Only required if CORS is used, configure it as you wish
var corsPolicy = new System.Web.Cors.CorsPolicy
{
AllowAnyHeader = true,
AllowAnyMethod = true,
AllowAnyOrigin = true,
SupportsCredentials = true
};
//corsPolicy.GetType()
// .GetProperty(nameof(corsPolicy.ExposedHeaders))
// .SetValue(corsPolicy, tusdotnet.Helpers.CorsHelper.GetExposedHeaders());
owinAppBuilder.UseCors(new Microsoft.Owin.Cors.CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context => Task.FromResult(corsPolicy)
}
});
PublicClientId = "self";
OAuthAuthorizationServerOptions OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new Microsoft.Owin.PathString("/Login"),
Provider = new MyServiceProvider(PublicClientId),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
AllowInsecureHttp = true,
RefreshTokenProvider = new MyRefreshTokenProvider(),
};
owinAppBuilder.UseOAuthBearerTokens(OAuthOptions);
//owinAppBuilder.UseTus(context => new DefaultTusConfiguration
//{
// // Excluded for brevity, use the same configuration as you would normally do
//});
return owinAppBuilder.Build<Func<IDictionary<string, object>, Task>>();
}));
Microsoft.Owin and related packages do not have targets for .NET Core, no for .NET Standard. All they have is dlls targeting full .NET. You can reference such libraries from your project targeting .NET Core, but they are not guaranteed to work, as you see yourself, because API (set of classes\methods\signatures) of full .NET and .NET Core are different. Visual Studio even will show a warning when you are doing that, for example:
Package 'Microsoft.Owin 3.1.0' was restored using
'.NETFramework,Version=v4.6.1' instead of the project target framework
'.NETCoreApp,Version=v2.0'. This package may not be fully compatible
with your project.
There is Microsoft.AspNetCore.Owin package and you can use OWIN middleware in .NET Core app as your first link describes, but almost all it provides is UseOwin extension method. There is no AppBuilder type there and so on, and there are no Microsoft.AspNetCore.Owin.Cors packages or similar. So you have to either implement all that yourself (no reason to, because you can use the same functionality provided by asp.net core framework) or wait for OWIN packages that target .NET Standard\Core and do that (didn't check, maybe they even exist already).
So, your code uses packages which are indeed not compatible with your target framework, as exception you have at runtime shows. So another answer (for some reason downvoted) is technically correct.
If you still want to use those packages reliably - you need to target full .NET Framework and not .NET Core. To do that, open your .csproj file and change
<TargetFramework>netcoreapp2.0</TargetFramework>
To some .NET framework version that supports .NET Standard 2.0, for example:
<TargetFramework>net47</TargetFramework>
Then go to nuget package manager and, if you have microsoft.aspnetcore.all package (or other packages targeting .NET Core) - uninstall it, you don't need it anyway. Then install Microsoft.AspNetCore package and all other asp.net core packages you need (if not installed already). Rebuild, run and it will work just fine.
That works because all (most?) AspNetCore packages target .NET Standard, not .NET Core, and you can use them in projects targeting full .NET Framework.
Note that by doing that you have asp.net Core project, but not on .NET Core, with all consequences that come from that (cannot run with dotnet run, on linux need to run with mono, and so on).
The Microsoft.Owin components will not work on dotnet core 2.0, they only work on .NET 4.5+