在MVVM中接收WCF通知 [英] Receiving WCF notifications in MVVM

查看:71
本文介绍了在MVVM中接收WCF通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来很愚蠢,但是我迷失了正确的执行方法.基本上可以算出来.

假设我有一个简单的控制台应用程序,该应用程序连接到WCF端点并通过简单的SendMessage(string text)调用发送文本消息.

我想编写一个MVVM UI,基本上将它们呈现出来.因此,我最初的想法是在ViewModel中创建一个承载WCF服务的线程,以某种方式捕获该服务中的事件,更新属性,然后将其绑定到UI层.

所以我想我的问题是;我该怎么做呢?更具体地说;
1.承载WCF服务的是Model还是ViewModel?
2.如何传递服务中的事件?我有服务失火事件吗?

有没有类似的例子可以让我看清楚?使用WCF之类的MVVM聊天客户端/服务器?

So this is going to sound silly, but I''m lost as to the correct way to do this. Its basically an excercise to figure it out.

Say I have a simple console app that connects to a WCF endpoint and sends text messages over a simple SendMessage(string text) call.

I want to write an MVVM UI that basically renders these as they come in. So my initial thinking is create a thread in the ViewModel that hosts the WCF service, somehow traps the events from the service, updates a property, and then that gets bound to the UI layer.

So I guess my question is; how do I do this? To be more specific;
1. Is it Model or ViewModel that hosts the WCF service?
2. How do I pass the events from the service? Do I have the service fire events?

Is there an example of something similar that I can look at somewhere that might make sense? An MVVM chat client/server that uses WCF or something?

推荐答案

由于我需要解决类似的难题,因此我给了它一些,这就是我的想法:您可以使用绑定而不是事件来执行此操作,我将使用服务来获取消息,这与您的WCF服务不同,MVVM中的服务是为ViewModel做事的类,因此ViewModel不会我们需要知道消息是如何到达的,让我们看一些代码:

WCF服务:
Since I needed to solve a similar conundrum I gave it some though and this is what I came up: you can do this with binding instead of an event, I would use a service to get the messages, this is different than your WCF service, a service in MVVM is a class that does things for the ViewModel, this way the ViewModel doesn’t need to know how the messages get to it, they just do, lets’ see some code:

The WCF service:
public class MessagesWFCService : IMessagesWFCService
    {
        public void SendMessage(string message)
        {
            MessageService.Instance.AddMessage(message);
        }
    }



服务内容:



The service:

public interface IMessageService
    {
        void AddMessage(string message);
    }

    public class MessageService : IMessageService
    {        
        private static MessageService _Instance = null;
        
        private MessageViewModel _MessageViewModel = null;
        
        private ServiceHost _ServiceHost = null;
        public MessageService()
        {
            _Instance = this;
            _ServiceHost = new ServiceHost(typeof(MessagesWFCService));            
        }
        public void AddMessage(string message)
        {
            _MessageViewModel.Messages.Add(message);
        }
        public void Close()
        {
            _ServiceHost.Close();
        }
        public static MessageService Instance
        {
            get
            {
                return _Instance;
            }
            set
            {
                if(_Instance != value)
                    _Instance = value;
            }
        }
        public MessageViewModel MessageViewModel
        {
            get
            {
                return _MessageViewModel;
            }
            set
            {
                if(_MessageViewModel != value)
                    _MessageViewModel = value;
            }
        }
        public void Open()
        {
            _ServiceHost.Open();
        }
    }



ViewModel



The ViewModel

public class MessageViewModel : INotifyPropertyChanged
    {        
        public event PropertyChangedEventHandler PropertyChanged;

        private ObservableCollection<string> _Messages = null;
                
        private IMessageService _MessageService = null;

        public MessageViewModel()
        {
            _MessageService = new MessageService();
            _MessageService.MessageViewModel = this;
            _MessageService.Open();
        }
        public ObservableCollection<string> Messages
        {
            get
            {
                return _Messages;
            }
            set
            {
                if(_Messages != value)
                {
                    _Messages = value;
                    PropertyChangedEventHandler eventHandler = PropertyChanged;
                    if(eventHandler != null)
                        eventHandler(this, new PropertyChangedEventArgs("Messages"));
                }
            }
        }
    }



当然,您需要将视图的DataContext设置为ViewModel,并将控件的ItemsSource属性绑定到ViewModel的Messages属性.

如果您使用的是棱镜



Of course you’ll need to set the view’s DataContext to the ViewModel and bind the ItemsSource property of the control to the ViewModel’s Messages property.

If you are using Prism
or Unity you should use the Container to resolve the MessageService instead of creating the instance directly.

Take a look, keep in mind that I haven’t tested this yet, but I will, and I think it can give you some guidance.


没什么意义..

您建议:
客户<-> IIS上的WCF<-> MVVM UI

那是可行的,但是需要IIS.从本质上讲,我想做同样的事情,但是将其托管在应用程序本身内部的ServiceHost上. IE.该应用程序是服务主机,因此客户端可以直接连接到它.
that makes less sense..

you''re suggesting:
client <--> WCF on IIS <--> MVVM UI

That''s doable, but requires IIS. I want to do the same thing, essentially, but host it over ServiceHost inside the app itself. I.E. the app IS the service host, so that clients connect to it directly.


xeroxducati写道:
xeroxducati wrote:

是是托管WCF服务的Model还是ViewModel?

Is it Model or ViewModel that hosts the WCF service?



两者都不托管服务. IIS可以.



Neither hosts the service. IIS does.


这篇关于在MVVM中接收WCF通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆