WCF 双工的学习

初学WCF。。。。

服务端:

服务契约:
   [ServiceContract(Namespace ="http://serverModel.Samples",SessionMode =SessionMode.Required,
         CallbackContract = typeof(ICalcualtorDuplexCallBack))]
     public interface IService2
     {
        [OperationContract(IsOneWay = true)]
         void Clear();
 
        [OperationContract(IsOneWay = true)]
         void Add(double input);
 
         [OperationContract(IsOneWay = true)]
        void Subtraction(double input);
 
         [OperationContract(IsOneWay = true)]
         void Multiplication(double input);
 
         [OperationContract(IsOneWay =true)]
         void Addition(double input);
 
     }
 
     /// <summary>
     /// 回调
     /// </summary>
     public interface ICalcualtorDuplexCallBack
     {
         [OperationContract(IsOneWay = true)]
         void Equals(double result);
 
         [OperationContract(IsOneWay = true)]
         void Equation(string eqn);
     }

服务部分:

 1   public class Service2 : IService2
 2      {
 3          private double _result;
 4          private string _equation;
 5          private ICalcualtorDuplexCallBack _callback = null;
 6          public Service2()
 7          {
 8              _result = 0.0D;
 9              _equation = _result.ToString(CultureInfo.InvariantCulture);
10              //获得Callback回调的公告
11              _callback = OperationContext.Current.GetCallbackChannel<ICalcualtorDuplexCallBack>();
12          }
13          /// <summary>
14          /// 清除
15          /// </summary>
16          public void Clear()
17          {
18              _callback.Equation(_equation+"="+_result.ToString(CultureInfo.InvariantCulture));
19              _result = 0.0D;
20              _equation = _result.ToString(CultureInfo.InvariantCulture);
21          }
22          /// <summary>
23          /// 增加
24          /// </summary>
25          /// <param name="input">参数</param>
26          public void Add(double input)
27          {
28              _result += input;
29              _equation += "+" + input.ToString(CultureInfo.InvariantCulture);
30              _callback.Equals(_result);
31          }
32          /// <summary>
33          ///34          /// </summary>
35          /// <param name="input">参数</param>
36          public void Subtraction(double input)
37          {
38              _result -= input;
39              _equation += "-" + input.ToString(CultureInfo.InvariantCulture);
40              _callback.Equals(_result);
41          }
42          /// <summary>
43          ///44          /// </summary>
45          /// <param name="input">参数</param>
46          public void Multiplication(double input)
47          {
48              _result *= input;
49              _equation += "*" + input.ToString(CultureInfo.InvariantCulture);
50              _callback.Equals(_result);
51  
52          }
53          /// <summary>
54          ///55          /// </summary>
56          /// <param name="input">参数</param>
57          public void Addition(double input)
58          {
59              _result /= input;
60              _equation += "*" + input.ToString(CultureInfo.InvariantCulture);
61              _callback.Equals(_result);  
62          }
63  
64  
65      }

 配置文件:app.config

在双工的时候 不支持 BasicHttpBinding,改wsDualHttpBinding 

 <service name="WcfServiceLibrary1.Service2">
         <!--双工不支持 BasicHttpBinding  所以在这里要用 wsDualHttpBinding -->
         <endpoint address="" binding="wsDualHttpBinding" contract="WcfServiceLibrary1.IService2">
           <identity>
             <dns value="localhost" />
           </identity>
         </endpoint>
         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
         <host>
           <baseAddresses>
             <add baseAddress="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service2/" />
           </baseAddresses>
         </host>
       </service>

客户端:

客户端
   public partial class Window1 : Window,ServiceReference2.IService2Callback
     {
         public Window1()
         {
             InitializeComponent();
         }
 
         private void button1_Click(object sender, RoutedEventArgs e)
         {
             System.ServiceModel.InstanceContext instanceContext = new InstanceContext(this);
             ServiceReference2.Service2Client clientw = new Service2Client(instanceContext);
             double value = 2.0D;
             clientw.Open();
             clientw.Add(value);
             clientw.Subtraction(value);
             clientw.Multiplication(value);
             clientw.Addition(value);
             clientw.Clear();
             clientw.Close();
      
         }
 
         public void Equals(double result)
         {
           //  MessageBox.Show("Result:"+result);
             Console.WriteLine("Result:" + result);
         }
 
         public void Equation(string eqn)
         {
 
             Console.WriteLine("Equation:" + eqn);
         }
     }
 }

 

posted @ 2012-10-30 15:13  在 水 一 方  阅读(606)  评论(0编辑  收藏  举报