Starting Azure Service Bus Trigger Function throws InvalidOperationException for "Host not yet started"
Asked Answered
B

6

12

I have a v.2 Service Bus Trigger function which, when I attempt to start, throws the following exception:

System.InvalidOperationException
  HResult=0x80131509
  Message=The host has not yet started.
  Source=Microsoft.Azure.WebJobs.Host
  StackTrace:
   at Microsoft.Azure.WebJobs.JobHost.StopAsync() in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\JobHost.cs:line 121
   at Microsoft.Azure.WebJobs.Hosting.JobHostService.StopAsync(CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Hosting\JobHostService.cs:line 32
   at Microsoft.Extensions.Hosting.Internal.Host.<StopAsync>d__10.MoveNext()

I've searched around but cannot find anyone with a similar issue (and fix). I'm running VS 15.8.7 with all extensions and packages updated.

Here's what my function looks like:

[FunctionName("ServiceBusListenerFunction")]
        public static void Run([ServiceBusTrigger("myTopic", "MySubscription", Connection = "MyConnection")]string mySbMsg, ILogger log)
        {
            log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
        }

And here's my local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "MyConnection": "UseDevelopmentStorage=true",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  },
  "Host": {
    "LocalHttpPort": 7077
  }    
}

I also tried doing the following in launchSettings.json, but it didn't help:

{
  "profiles": {
    "MyProject": {
      "commandName": "Project",
      "executablePath": "C:\\Users\\[myUserName\\AppData\\Roaming\\npm\\node_modules\\azure-functions-core-tools\\bin\\func.dll",
      "commandLineArgs": "host start --port 7077"
    }
  }
}

I have Service Bus Explorer running and have created the above-named topic and subscription on it. The project in which the functions are located is built against .NET Standard 2.0.

Please let me know if you have any suggestions or need additional information.

EDIT: I grabbed the red exception text that appears briefly in the console window before it closes (which happens right before I get the above exception), and it reads:

Host initialized
A host error has occurred
System.Private.Uri: Value cannot be null
Parameter name: uriString
Stopping job host

Searching on this, I found this, but it doesn't seem as though I should have to change the attribute to get this working.

Thanks in advance for any help.

Budbudapest answered 23/10, 2018 at 13:35 Comment(0)
E
4

Problem is caused by this setting

"MyConnection": "UseDevelopmentStorage=true"

UseDevelopmentStorage=true represents Storage emulator connection string, for a Service Bus trigger, use Service Bus connection string(same one used in Service Bus Explorer or find it in Azure portal).

Some improvements:

In local.settings.json, LocalHttpPort somehow doesn't work in VS, you can remove it as commandLineArgs in launchSettings.json works as expected.

AzureWebJobsDashboard is not required now, so it can be deleted without special purpose.

In launchSettings.json, remove executablePath which is invalid as well. Usually we don't need this setting as VS use latest CLI by default.

Elbe answered 24/10, 2018 at 1:37 Comment(0)
M
3

One of the ways, I sorted the issue by removing the connection string from the [ServiceBusTrigger] and inserting it through local.settings.json.

in the function file.

 [ServiceBusTrigger("Your-Topics-Name", "SubscriptionName",Connection = "MyServiceBus")]

Inside the local.settings.json.

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsMyServiceBus": "your connection string",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}
  • note: connection string name start with the "AzureWebJobs" so you can put the remaining as the name.
Malfunction answered 21/5, 2019 at 6:53 Comment(2)
I hads the same problem, but from start my connection was not UseDevelopmentStorage=true and run on Any CPU was already set. Doing this resolved the issue.Birthstone
I am in awe that this happened, when did this change happen?! Thanks for the answer, it's a bit magical!Collegiate
F
3

In my case, it was just to update the version of Microsoft.Azure.WebJobs.Extensions.ServiceBus from 4.7.x to 5.x.x, and that's it :-)

Farceur answered 23/9, 2022 at 8:18 Comment(2)
This worked for me. I think 5.x.x versions sorted out the above issueOletta
For identity-based connections, it is required to use 5.x.x. If you try to set an identity-based connection with 4.x.x, you will see this error too.Pelage
O
0

I had to install Azure Functions Core Tools. It includes a version of the same runtime that powers Azure Functions runtime that you can run on your local development computer. It also provides commands to create functions, connect to Azure, and deploy function projects.

Opprobrium answered 29/9, 2022 at 21:41 Comment(0)
V
0

In my case the problem was the Platform target, change it to Any CPU instead of x86

Vapory answered 28/12, 2022 at 21:19 Comment(0)
C
0

I solve the issue by updating all the packages. I had sold packages that were incompatible with a recent package I installed.

Cletuscleve answered 17/2, 2023 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.