DAPR Error when running from a method in c# console application

131 views Asked by At

I am seeing a weird scenario while using DAPR in c# console application. I have create a console app and running as a cron job in Kubernetes. The code is supposed to publish a message to Azure service bus queue . I am using a DAPR sidecar. The issue I am facing is what when I try to publish message from Main body, it works fine but when I publish it from a separate method I get DaprException . Below is the code:

 static async Task Main(string[] args)
    {
        using (var client = new DaprClientBuilder().Build())
        {
            string topicName = "topic1";
            await client.PublishEventAsync("pubsub", topicName, "testdapr2");               
            Console.WriteLine("Message published successfully.");
        }
        await PublishMessage();
    }

    private static async Task PublishMessage()
    {
        Console.WriteLine("Getting Error");
        using (var client = new DaprClientBuilder().Build())
        {
            string topicName = "topic1";
            await client.PublishEventAsync("pubsub", topicName, "testdapr2");
            Console.WriteLine("Message published successfully.");
        }
    }

The DAPR publish in Main works fine but when I try to publish message in "PublishMessage()" I am getting below error:

DaprException: Publish operation failed: the Dapr endpoint indicated a failure. See InnerException for details.
---> Grpc.Core.RpcException: Status(StatusCode="Unavailable", Detail="Error connecting to subchannel.", DebugException="System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it.")
---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|281_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
at Grpc.Net.Client.Balancer.Internal.SocketConnectivitySubchannelTransport.TryConnectAsync(ConnectContext context)
--- End of inner exception stack trace ---
at Grpc.Net.Client.Balancer.Internal.ConnectionManager.PickAsync(PickContext context, Boolean waitForReady, CancellationToken cancellationToken)
at Grpc.Net.Client.Balancer.Internal.BalancerHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Grpc.Net.Client.Internal.GrpcCall`2.RunCall(HttpRequestMessage request, Nullable`1 timeout)
at Dapr.Client.DaprClientGrpc.MakePublishRequest(String pubsubName, String topicName, ByteString content, Dictionary`2 metadata, String dataContentType, CancellationToken cancellationToken)
--- End of inner exception stack trace ---

Please suggest if anyone have faces this error.

1

There are 1 answers

0
Sampath On

The error message DaprException: Publish operation failed: the Dapr endpoint indicated a failure. See InnerException for details. suggests that the problem lies in establishing a connection to the Dapr sidecar from your console application. The inner exceptions, Grpc.Core.RpcException and System.Net.Sockets.SocketException, indicate a network-related issue ,Incorrect network configuration and Service bus configuration error

Followed the Dapr Docs how to Publish a message and subscribe to a topic

enter image description here

dapr run --app-id checkout --app-port 6002 --dapr-http-port 3602 --dapr-grpc-port 60002 --app-protocol https dotnet run enter image description here

Code:

string PUBSUB_NAME = "azure-service-bus-pubsub";
            string TOPIC_NAME = "orders";

            while (true)
            {
                await Task.Delay(5000); // Delay for demonstration purposes

                // Message to publish
                string message = "testdapr2";

                using var client = new DaprClientBuilder().Build();

                // Publish the message to the topic
                await client.PublishEventAsync(PUBSUB_NAME, TOPIC_NAME, message);
                Console.WriteLine($"Message sent: {message}");




Output:

enter image description here

enter image description here

  • Another method is to connect Dapr to RabbitMQ and integrate Service Bus with RabbitMQ.