当前位置: 代码迷 >> Web Service >> Silverlight+WCF双工有关问题,初学者,
  详细解决方案

Silverlight+WCF双工有关问题,初学者,

热度:243   发布时间:2016-05-02 02:46:31.0
Silverlight+WCF双工问题,菜鸟求助,急
本帖最后由 alian_1126 于 2011-03-13 10:56:49 编辑 我在Silverlight项目里添加了WCF服务引用,在Page.xaml.cs里用Service References智能提示可以弹出WCF服务接口IXMLService和IXMLServiceClient,却弹不出回调接口IXMLServiceCallBack,为什么啊,我要在SL里实现这个回调啊,弹不出来怎么实现啊,刚学,遇到问题了,前辈们帮帮我,谢谢
以下是WCF里的代码

//契约
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;

namespace WcfService
{
    [ServiceContract(CallbackContract = typeof(IXMLClient))]
    public interface IXMLService//服务接口
    {
        [OperationContract(IsOneWay = true)]
        void GetData(Uri uri);
    }
    
    public interface IXMLClient//回调接口
    {
        [OperationContract(IsOneWay = true)]
        void BackToClient(String xmlData);
    }
}
//服务
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace WcfService
{
    
    public class Service:IXMLService
    {
        IXMLClient client = OperationContext.Current.GetCallbackChannel<IXMLClient>();
        public void GetData(Uri uri)
        {
            WebClient webclient = new WebClient();
            webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted);
            webclient.DownloadStringAsync(uri);
        }

        void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            if (null == e.Error) { return; }
            client.BackToClient(e.Result);
        }
    }
}

------最佳解决方案--------------------
仔细看了下你前面说的内容,你现在能看到 IXMLServiceClient 是吧。
IXMLServiceClient里是什么内容?

另外现在用的 WSDualHttpBinding 还需要定义 cliendAddress。你现在还没有定义。

------其他解决方案--------------------
1. IXMLClient client = OperationContext.Current.GetCallbackChannel<IXMLClient>();
你应该放到你GetData方法里,另外GetData应该用同步的DownloadString,否则客户端会死掉。

2. 客户端要把带有回调实例的InstanceContext加到WCF Client里去。
------其他解决方案--------------------
你这是什么协议的?
  相关解决方案