Cannot create an abstract class? - c#

I'm trying to run firebase functions locally but I get the error:
Exception while executing function: Functions.TestMe. Microsoft.Azure.WebJobs.Host: One or more errors occurred. Exception binding parameter 'req'. mscorlib: Cannot create an abstract class.
I have an azure cloud function project in VSCode with just this function:
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Learning.Platform
{
public static class TestMe
{
[FunctionName("TestMe")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
var db = new MongoClient(/*snipped*/);
var hey = db.GetDatabase("dude").GetCollection<object>("hey");
return (ActionResult)new OkObjectResult($"Hello, {hey}");
}
}
}
I would have thought this would just work because it's a fairly basic example of azure functions.
I'm using the Azure .net SDK version 2.9, Azure Tools 1.3.0 and the .Net Core 2.0 framework.

I gave up out of frustration and uninstalled the .net core framework and azure tools then reinstalled both.
Issue resolved itself.

For anyone who runs into this. I had the same issue, and was able to resolve it. I ran into this while trying to mess around and create an F# HttpTrigger function against v2 of the azure function runtime (since they don't have templates for that yet...).
Anyway, my issue was that I had installed the System.AspNetCore.Http Nuget package into my project while I was troubleshooting something else. After removing the reference to the Nuget package (Leaving only the Microsoft.NET.Sdk.Functions) it started working again.

There was an an issue within the azure storage assembly - i run in the same problem as i tried run a webjob - but the azure storage team fixed it

Related

'No job functions found' .Net Azure Function after migration from Microsoft.Azure.WebJobs.ServiceBus to Microsoft.Azure.WebJobs.Extensions.ServiceBus

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}");
}
}
}

Trouble with Azure Function v3 in C#

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.

Trying to make HttpTrigger pass a blob

I'm trying to make a HTTP Trigger in Visual Studio Code which just need to make a file and pass it to blob storage, but I get an error saying: "The type or namespace 'Blob' could not be found"
Here is a full working example. I think you are just missing a using / referece a nuget package.
Reference Nuget package Microsoft.Azure.WebJobs.Extensions.Storage
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;
namespace SampleFunctions
{
public static class Http2BlobFunction
{
[FunctionName("Http2BlobFunction")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
[Blob("myblobcontainer/{rand-guid}.txt", FileAccess.Write)] CloudBlockBlob blob,
ILogger log)
{
log.LogInformation("Received file upload request");
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
await blob.UploadTextAsync(requestBody);
return new OkObjectResult(blob.Name);
}
}
}
If you VS Code to develop Azure Function V2.0, you need to manually install package vai .NET.CLI. Since you use storage binding, please run the command dotnet add package Microsoft.Azure.WebJobs.Extensions.Storage --version 3.0.7 to install the package. For more details, please refer to the document.
You can also use the azure CLI to install extensions.
func extensions install
Finally, you can use the portal

In the using directive "using Accord.Video.FFMPEG", FFMPEG does not exist in the name space

I am building an Azure Function in Visual Studio to convert a videos frames to images. I'm using the VideoFileReader class from Accord.Video.FFMPEG class. The code works on my machine but when trying to build this as an Azure Function Project, the using directive Accord.Video.FFMPEG errors.
And subsequently the type VideoFileReader can not be found.
I have tried re-installing the Accord, Accord.Video and Accord.Video.FFMPEG NuGet packages.
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Accord;
using Accord.Video;
using Accord.Video.FFMPEG;
namespace ConvertVideo
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([BlobTrigger("videos/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
//start a new videoFileReader
using (var vFReader = new VideoFileReader())
{
//open the video
vFReader.Open(name);
//get the framerate
double frameRate = vFReader.FrameRate.ToDouble();
//more code which converts a frame to jpg
}
}
}
}
It seems the problem is the FFMPEG dll from Accord is only for .Net Frameworks and doesn't work with .Net Standard or .Net Core which Azure function app uses.
I had to give up with Function App and use an Azure Webjob instead. Webjobs can use .Net frameworks.
If you didn't publish "Accord.Video.FFMPEG" to azure function successfully, you can add it manually on Azure portal.
First go to you function on Azure portal and click "Platform features" --> "Advanced tools(Kudu)"(shown as below screenshot)
Then click "Debug console" --> "CMD" --> "site" --> "wwwroot" --> "New folder", name the new folder with "bin".(shown as below screenshot)
Download the nupkg of "Accord.Video.FFMPEG" and drag the dll file in nupkg from local to "bin" folder which you created above.
Then use it in your function(shown as below screenshot)

Error with Azure Function and Docker

I am trying to implement the ICS creator sample for Azure Functions: https://github.com/Azure-Samples/azure-functions-create-ics-file-using-csharp-sample.
I followed all the steps there, but the difference with my implementation is that I'm running the function locally with Docker, and I am getting this error:
An unhandled exception occurred while processing the request.
CompilationErrorException: Script compilation failed.
Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker+d__26.MoveNext()
in DotNetFunctionInvoker.cs, line 313
FunctionInvocationException: Exception while executing function:
Functions.swinvite
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Per my understanding, the error is related with the ical.net library, that is not being imported to the image.
Any ideas?
Thank you in advance.
You are right, error is related to Ical.net library. You can try this repository.
More details
The guide you follow is to create function in function runtime 1.x(.net framework), where packages will be restored according to project.json. But you want to run using docker(image uses runtime 2.x, based on .net core), where project.json is invalid. So the file can be dropped.
Then we have to add Ical.Net related assemblies manually. We can download latest version package as the one in that guide is out of date.
After downloading the package, create a bin folder under ~\GetInvite. Copy Ical.Net.dll and NodaTime.dll(dependency of Ical.Net) to this folder.
And some changes in run.csx.
// add assembly
#r "Ical.Net.dll"
// remove some unnecessary namespaces
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using Ical.Net;
using Ical.Net.DataTypes;
using Ical.Net.CalendarComponents;
using Ical.Net.Serialization;
// remove async as the method has no await method
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
{
... // remain the same
// Event is deprecated in new version, use CalendarEvent
var icalevent = new CalendarEvent{...}
... // remain the same
}
One more point, in function.json change authLevel from function to anonymous. Or you will get 401 error.

Categories

Resources