读书人

Silverlight调用WCF双工通信有关问题

发布时间: 2012-03-25 20:55:17 作者: rapoo

Silverlight调用WCF双工通信问题
silverlight调用WCF服务时,服务端我做了一个timer,想定时将信息发送到SL端,但是这个timer好像根本没有工作。请大家帮忙看看。
WCF端服务契约代码:

C# code
    [ServiceContract(CallbackContract = typeof(IDuplexClient))]    public interface IDuplexService    {        [OperationContract]        void Order();    }    [ServiceContract]    public interface IDuplexClient    {        [OperationContract(IsOneWay = true)]        void Receive(string strMsg);    }

实现接口
C# code
    public class OrderService : IDuplexService    {        IDuplexClient client;         public void Order()        {            client = OperationContext.Current.GetCallbackChannel<IDuplexClient>();            Timer timer = new Timer(new TimerCallback(CallClient),this,500,1000); //timer不能定时执行方法                    }        private void CallClient(object o)        {              client.Receive(DateTime.Now + "调用一次!");          }     }

客户端代码:
C# code
public MainPage() 
{
InitializeComponent();
this.Loaded+=new RoutedEventHandler(MainPage_Loaded);
}

private void MainPage_Loaded(object sender,RoutedEventArgs e)
{
EndpointAddress address = new EndpointAddress("http://localhost:1802/Service1.svc");
PollingDuplexHttpBinding binding = new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll);

DuplexServiceClient proxy = new DuplexServiceClient(binding, address);

proxy.ReceiveReceived += new EventHandler <ReceiveReceivedEventArgs>(proxy_ReceiveReceived);

proxy.OrderAsync();

reply.Text = DateTime.Now + ":开始调用" + Environment.NewLine;
}

void proxy_ReceiveReceived(object sender, ReceiveReceivedEventArgs e)
{
if (e.Error == null)
{
reply.Text = e.strMsg;
}
}


[解决办法]
这个问题比较难判断,通常来说应该是服务的问题,如果没有timer,可以正常调用么?服务调用的数据量大么?如果数据量大造成超时,尽管timer发出请求,wcf将不给予response。

这里有两篇教程,可以帮助解决你的问题:

Silverlight与WCF之间的通信(1)SL客户端定时请求WCF服务
http://www.silverlightchina.net/html/tips/2010/0619/1292.html

Silverlight与WCF之间的通信(2)利用WCF的双工通信“推送”
http://www.silverlightchina.net/html/tips/2010/0619/1294.html
[解决办法]
呵呵。。。顶了
[解决办法]
路过。。。
[解决办法]
“http://localhost:1802/Service1.svc”

这个EndPoint不是基于Soap的吗? Soap是没有连接状态的,
你想用双向通信的,应该首先使用net.tcp的绑定方式来保持连接吧?
[解决办法]


最近才研究WCF中的双工问题,其实LZ的问题出现的原因很简单,
应该说是微软自己给出的例子就有问题,LZ应当该让主程序等待足够长的时间,让 timer 有时间运行完成,
微软给出的例子代码不是这样的么:

C# code
using (Timer timer = new Timer(new TimerCallback(CallClient), null, 5000, 5000)){    Thread.Sleep(8000);}
[解决办法]
探讨
“http://localhost:1802/Service1.svc”

这个EndPoint不是基于Soap的吗? Soap是没有连接状态的,
你想用双向通信的,应该首先使用net.tcp的绑定方式来保持连接吧?

[解决办法]
标记一下!学习了!帮顶

读书人网 >CAD教程

热点推荐