故如虹,知恩;故如月,知明
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术

wcf DuplexHttpBinding双向通信

2755人阅读 2019/8/4 11:20 总访问:3842262 评论:0 收藏:0 手机
分类: WCF


一:建立接口 CallbackContract = typeof(ICallback)指定需要回调通信的接口,该接口方法由前端实现

[ServiceContract(CallbackContract = typeof(ICallback))]
    public interface IUserCallBack
    {
        [OperationContract]
        ReturnData<string> Login(string username, string password);
    }


    public interface ICallback
    {
        [OperationContract(IsOneWay = true)]//单向调用,不需要返回值
        void LoginCallBack(string hello);
    }

二:实现接口

public class UserCallBack : IUserCallBack
    {


           UserDao ud = new UserDao();
           public ReturnData<string> Login(string username, string password)
           {
                    ICallback callbackt = OperationContext.Current.GetCallbackChannel<ICallback>();

                    callback.LoginCallBack("successful");//可以进行直接回调当前客服端的实现的LoginCallBack方法

                   //如何需要回调特定的客服端就把callbackt 与用户标识放到键值对里,在更具特定的情况回调客服端方法
           }
    }

三:进行wcf配置

<!--这里是添加的开始-->
    <services>
      <service name="JSDService.Users.UserCallBack" >
        <endpoint address="" binding="pollingDuplexHttpBinding" contract="JSDService.Users.IUserCallBack" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      
    </services>
    <extensions>
      <bindingExtensions>
        <add name="pollingDuplexHttpBinding" type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement,System.ServiceModel.PollingDuplex"/>
      </bindingExtensions>
    </extensions>
    <!--这里是添加的结束-->

四:siverlight前端 直接放一个按钮进行测试

页面:

<Grid x:Name="LayoutRoot" Background="White">
            <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,41,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,70,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="120" />
    </Grid>

后端:

ServiceReferenceTest.UserCallBackClient client;
        public MainPage()
        {
            InitializeComponent();


            //扩展的轮询机制的双向通讯
            PollingDuplexHttpBinding binding = new PollingDuplexHttpBinding()
            {
                //每次轮询建立的有效时间为20分钟
                InactivityTimeout = TimeSpan.FromMinutes(20)
            };
            //基础的http请求方式
            //Binding binding =new BasicHttpBinding();
            //svc服务地址
            EndpointAddress endPoint = new EndpointAddress("http://localhost:8087/Users/UserCallBack.svc");
             client = new ServiceReferenceTest.UserCallBackClient(binding, endPoint);
            //异步调用SayHellow方法
            //client.SayHellowAsync(this.textBox1.Text);
            //调用完成后ShowHello回调事件
             client.LoginCallBackReceived += client_LoginCallBackReceived;


            client.LoginCompleted += client_LoginCompleted;
        }


        void client_LoginCallBackReceived(object sender, ServiceReferenceTest.LoginCallBackReceivedEventArgs e)
        {
            MessageBox.Show(e.hello);
        }


        void client_LoginCompleted(object sender, ServiceReferenceTest.LoginCompletedEventArgs e)
        {
            MessageBox.Show(e.Result.Value);
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
             client.LoginAsync("aj","aj");
        }






欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739

评价