【问题标题】:ServiceFabric communication with local WPF AppServiceFabric 与本地 WPF 应用程序的通信
【发布时间】:2021-06-04 14:33:43
【问题描述】:

有什么方法可以将 Service Fabric Stateless 服务(在本地)连接到我的本地 WPF 应用程序?我想处理一些关于无状态服务的数据并将更新发送到 WPF 应用程序,例如使用 NServiceBus

【问题讨论】:

    标签: c# wpf azure-service-fabric


    【解决方案1】:

    是的,您可以为此使用消息队列。

    您的无状态服务将创建一条消息并将其放入队列:

        static async Task SendMessageAsync()
        {
            // create a Service Bus client 
            await using (ServiceBusClient client = new ServiceBusClient(connectionString))
            {
                // create a sender for the queue 
                ServiceBusSender sender = client.CreateSender(queueName);
    
                // create a message that we can send
                ServiceBusMessage message = new ServiceBusMessage("Hello world!");
    
                // send the message
                await sender.SendMessageAsync(message);
                Console.WriteLine($"Sent a single message to the queue: {queueName}");
            }
        }
    

    WPF 应用程序将接收该消息并对其进行处理:

        // handle received messages
        static async Task MessageHandler(ProcessMessageEventArgs args)
        {
            string body = args.Message.Body.ToString();
            // update the ViewModel here
    
            // complete the message. messages is deleted from the queue. 
            await args.CompleteMessageAsync(args.Message);
        }
    

    我建议发送一个序列化的对象,而不是发送一个简单的字符串,例如使用 JSON。

    更多信息在这里: link 和这里: link

    【讨论】:

    • 这正是我想要的,非常感谢! ?
    猜你喜欢
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    相关资源
    最近更新 更多