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.
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 trying to create non-static functions in my Azure Function projet in .NET 5 (VS 2022) and the Startup Configure method is not being called.
Here's my start up class
[assembly: FunctionsStartup(typeof(AuthenticationGateway.Functions.Startup))]
namespace AuthenticationGateway.Functions
{
class Startup : FunctionsStartup //public or not, still does not get called.
{
public override void Configure(IFunctionsHostBuilder builder)
{
//break point here never gets hit...
}
}
}
And here's the function in question:
namespace AuthenticationGateway.Functions
{
public class CreationConnection
{
private AuthenticationGatewayContext Context { get; set; }
public CreationConnection(AuthenticationGatewayContext context)
{
Context = context;
}
[Function("CreationConnection")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req,
FunctionContext executionContext)
{
var response = req.CreateResponse(HttpStatusCode.OK);
return response;
}
}
}
I've tried commenting all of the code in Configure just in case it was a problem with it, not working either. Also tried marking the startup class as public too, no go.
Here are the dependencies for the projet in question
They are not the default dependencies the projet has when creating an Azure Function projet but as I tried other solutions to fix the issue, it lead me to plug those in.
Here's what the console is saying when starting the project:
Azure Functions Core Tools Core Tools Version: 3.0.3904 Commit
hash: c345f7140a8f968c5dbc621f8a8374d8e3234206 (64-bit) Function
Runtime Version: 3.3.1.0
Anything I missed ?
EDIT: I have reverted to the following dependencies as the previous ones made it so no functions would be found in the project.
On this page here it says those following dependencies have to be installed:
Microsoft.Azure.Functions.Extensions
Microsoft.NET.Sdk.Functions package version 1.0.28 or later
Microsoft.Extensions.DependencyInjection (currently, only version 3.x and earlier supported)
I have done so, except the last one because it is incompatible with .NET 5 it seems. Also, the project is now unbuildable:
error MSB4062: The "GenerateFunctionMetadata" task could not be loaded from the assembly
I have tried to reproduce the same issue which you got by following the below steps:
Created the Azure Functions (Stack: .Net5-v3) in VS2022.
Before adding Microsoft.Net.Sdk.functions to the project, it was built successfully.
After adding Microsoft.Net.Sdk.functions to the project, it has run to the same issue MSB4062 error as below:
By referencing these SO Thread1 and Thread2, removing Microsoft.Net.Sdk.functions will solve the compile issue.
I just upgraded my .net4.8 web app to .net6 using upgrade assistant among the other errors I get, in my SignIn function I cannot use the following:
WsFederationConfiguration config = FederatedAuthentication.FederationConfiguration.WsFederationConfiguration;
I get the error:
'FederatedAuthentication' does not contain a definition for 'FederationConfiguration'
It seems like FederationConfiguration is no longer supported in .net6. What is the best alternative?
Is there a Nuget/Assembly/Library that will allow me to bind BlobProperties for .NET Core Azure 3.x Functions, in both a local environment and in an Azure environment?
I have an Azure Function running on .NET Core 3 with a signature that looks like this:
public async Task DoFunctionWork([BlobTrigger("path/to/{blobName}")] Stream blobStream,
**Microsoft.Azure.Storage.Blob.BlobProperties Properties**)
{
//Function Body....
}
The problem is with the Properties parameter. If run locally, it can only be resolved if I prefix the BlobProperties parameter with the Microsoft.Azure.Storage.Blob namespace as above. In Azure, it never seems to work. When it fails - on either Azure or locally - I get one of the following messages:
Can't bind properties to type 'Azure.Storage.Blob.Properties' or Can't bind properties to type 'Microsoft.Azure.Storage.Blob.Properties'
Has anyone encountered this before? How did you solve it?
Thanks.
It looks like this is because, there is a version mismatch between what your SDK requires and what you have installed.
For instance, if you are using 3.0.9 version of Function SDK, then you might have installed Microsoft.Azure.WebJobs other than (>= 3.0.0 && < 3.1.0) versions.
Microsoft.NET.Sdk.Functions 3.0.9
Microsoft.Azure.WebJobs.Extensions.Storage 4.0.3
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.