访问双工服务

View Code
// Code in the MainPage.xaml.cs page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
using DuplexClient.OrderService;


namespace DuplexClient
{
public partial class MainPage : UserControl
{

public MainPage()
{
InitializeComponent();

// If using the PollingDuplexHttpBinding on the service
EndpointAddress address = new EndpointAddress("http://localhost:19021/Service1.svc");
PollingDuplexHttpBinding binding
= new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll);

DuplexServiceClient proxy
= new DuplexServiceClient(binding, address);

// If using a NetTcpBinding on the service
// DuplexServiceClient proxy = new DuplexServiceClient();
proxy.ReceiveReceived += new EventHandler<ReceiveReceivedEventArgs>(proxy_ReceiveReceived);
proxy.OrderAsync(
"Widget", 3);
reply.Text
= "Sent order of 3 Widgets." + Environment.NewLine;

}

void proxy_ReceiveReceived(object sender, ReceiveReceivedEventArgs e)
{
if (e.Error == null)
{
reply.Text
+= "Service reports Widget order is " + e.order.Status + "." + Environment.NewLine;

if (e.order.Status == OrderStatus.Completed)
{
reply.Text
+= "Here is the completed order:" + Environment.NewLine;

foreach (string order in e.order.Payload)
{
reply.Text
+= order + Environment.NewLine;
}
}

}
}

}
}

原文地址:https://www.cnblogs.com/landexia/p/1994439.html