Azure-Functions icon indicating copy to clipboard operation
Azure-Functions copied to clipboard

Event Hub Trigger not working in .Net Core

Open EhRom opened this issue 8 years ago • 22 comments

I have a function with a EventHubTrigger in Visual Studio 2017 :

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.ServiceBus;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([EventHubTrigger("myEventHub", Connection = "myConnection")]string myEventHubMessage, TraceWriter log)
        {
            log.Info($"C# Queue trigger function processed: {myEventHubMessage}");
        }
    }
}

I have this error :

[10/01/2018 16:55:13] The following 1 functions are in error: [10/01/2018 16:55:13] Function1: Unable to configure binding 'myEventHubMessage' of type 'eventHubTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.

After investigating, I figured out that Visual Studio generates a wrong function.json :

{
  "generatedBy": "Microsoft.NET.Sdk.Functions.Generator-1.0.7",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "path": "myeventhub",
      "connection": "myConnection",
      "name": "myEventHubMessage"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/FunctionApp1.dll",
  "entryPoint": "FunctionApp1.Function1.Run"
}

There is no direction element. Could it be the cause of the error ?

EhRom avatar Jan 10 '18 17:01 EhRom

The issue is likely that in .NET core the Event Hub trigger is actually no longer a part of Microsoft.Azure.WebJobs.ServiceBus. This reflects the Service Bus team pulling it out as a separate SDK..

The fact that it's not throwing an error at build is strange but see if replacing with this package resolves your issues. If so I'll open a bug to throw an error if not using this package in .net core:

https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.EventHubs

jeffhollan avatar Jan 23 '18 00:01 jeffhollan

The problem is the wrong property name in the functions.json file. -> "path" instead of "eventhubname" With the example above, if you manually change the file to have: "path": "myeventhub", "eventhubname": "myeventhub", works for me.

DamiaoCastro avatar Jan 27 '18 12:01 DamiaoCastro

@DamiaoCastro ... How does that work at deployment time? I was able to get around this issue with your suggested fix, but I'm concerned about when we actually want to move this into production.

bellyhold avatar Jan 31 '18 21:01 bellyhold

@bellyhold , the application builds fine, but when the application starts and begins to "read" all the functions, the error "Can't figure out which ctor to call." shows up. By changing the property name in the function.json file from "path" to "eventhubname" the application doesn't have that error anymore. The property name in the EventHubTriggerAttribute is "EventHubName", not path.

https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.ServiceBus/EventHubs/EventHubTriggerAttribute.cs#L28

DamiaoCastro avatar Jan 31 '18 21:01 DamiaoCastro

@DamiaoCastro , Sorry if I'm not asking the right questions. I'm new to Azure Functions... but a little more context...

We have a Jenkins build server that does a clean and build on deploy. Based on my local testing I'm assuming that the Jenkins clean and build will overwrite the manual edit (path => eventhubname) and cause everything to fail again after deploy. Do you know if there is a fix for this in the works?

bellyhold avatar Jan 31 '18 22:01 bellyhold

Sorry @bellyhold , I'm in no way involved in the development of the azure functions sdk nor extensions. I assume that the problem resides here: https://github.com/Azure/azure-webjobs-sdk-templates/blob/dev/Functions.Templates/Bindings/bindings.json#L90

But I'm not sure at all.

Maybe @jeffhollan can better answer your question.

DamiaoCastro avatar Jan 31 '18 22:01 DamiaoCastro

@DamiaoCastro, should we open an issue in the azure-webjobs-sdk-templates instead ?

EhRom avatar Feb 01 '18 06:02 EhRom

I don't know what should happen anymore. In the documentation, I see this:

image

(https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs#trigger---configuration) So the script versions of eventHub implementations work already like that...

So, I decided to issue a pull request on the "EventHubTriggerAttribute".

https://github.com/Azure/azure-webjobs-sdk/pull/1551

Because the error is inside "AttributeCloner.CreateDirect" the is a internal class in a very different part of the project, I was not able to UnitTest it. In my computer, I copied the AttributeCloner class to the EventHub project and I saw the code run correctly. Also had problems with my nuget build of the "Microsoft.Azure.WebJobs.Extensions.EventHubs" package, so I was not able to make it run in a standalone project.

DamiaoCastro avatar Feb 04 '18 09:02 DamiaoCastro

Honestly it seems like a hack that the parameter names of the attribute constructor even matter. A seemingly innocuous refactor could break things.

onionhammer avatar Feb 05 '18 01:02 onionhammer

@jeffhollan any updates on this? Seems like a barrier to cleanly utilizing this library for eventhub/iot hub.

onionhammer avatar Feb 18 '18 21:02 onionhammer

@jeffhollan Also really interested in seeing this issue get resolved. Was trying to demo an IoT solution to a friend to get them familar with Azure and was sad to see that I'm unable to use functions to connect to EventHub in this current state

WonderPanda avatar Mar 06 '18 14:03 WonderPanda

@WonderPanda it works, it's just annoying because you have to go into the bin and manually edit the generated functions.json file

onionhammer avatar Mar 06 '18 15:03 onionhammer

@onionhammer That's not working, there's clearly a bug that needs to be fixed here. I'm fine to change it for myself but the point is that these things need to be more seamless. If I'm doing a talk inside my company about building IoT apps using Azure services I don't want to have to demo the need to go in and mess with files manually in the bin folder

WonderPanda avatar Mar 06 '18 15:03 WonderPanda

@onionhammer, I totally agree with @WonderPanda : it is not suitable in industry solution to have to manually change a file for making the solutions works.

EhRom avatar Mar 06 '18 15:03 EhRom

@WonderPanda @EhRom not arguing that this is a huge annoyance and it needs to be resolve ASAP.. hint hint microsoft.. I was saying that the functionality of consuming an EH via an azure function does indeed work, just that the generation of function.json is broken.

onionhammer avatar Mar 06 '18 17:03 onionhammer

+1 please resolve - not usable in CI / CD like this

creyke avatar Mar 07 '18 19:03 creyke

Even if I manually modify the attribute from path to either eventhubname or eventHubName, the function executes locally, but when I deploy it via "func azure functionapp publish", it doesn't receive any invocations when deployed to the current Beta of Azure Functions in Azure (Runtime version: 2.0.11587.0 (beta)) so it "appears" this is still not usable?

Has anyone got this attribute working (with .NET Core) in Azure?

creyke avatar Mar 08 '18 10:03 creyke

@creyke I have it working, after modifying it. I don't deploy with func azure functionapp publish, at least not directly, I deploy locally, modify the JSON, then from VS Code, click the little up arrow to publish with the azure functions extension

onionhammer avatar Mar 08 '18 14:03 onionhammer

@onionhammer can i ask what version of azure functions you are running it in? (might differ per region) - and what versions of the dependent packages you are using too?

creyke avatar Mar 08 '18 23:03 creyke

'beta' runtime on the latest azure functions nuget

onionhammer avatar Mar 09 '18 00:03 onionhammer