欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WCF實現(xiàn)進程間管道通信Demo分享

 更新時間:2017年12月15日 15:43:33   作者:秋荷雨翔  
下面小編就為大家分享一篇WCF實現(xiàn)進程間管道通信Demo,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

一、代碼結(jié)構(gòu):

二、數(shù)據(jù)實體類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace DataStruct
{
 /// <summary>
 /// 測試數(shù)據(jù)實體類
 /// </summary>
 [DataContract]
 public class TestData
 {
  [DataMember]
  public double X { get; set; }

  [DataMember]
  public double Y { get; set; }
 }
}

三、服務(wù)端服務(wù)接口和實現(xiàn):

接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using DataStruct;

namespace WCFServer
{
 /// <summary>
 /// 服務(wù)接口
 /// </summary>
 [ServiceContract]
 public interface IClientServer
 {
  /// <summary>
  /// 計算(測試方法)
  /// </summary>
  [OperationContract]
  double Calculate(TestData data);
 }
}

實現(xiàn):

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using DataStruct;

namespace WCFServer
{
 /// <summary>
 /// 服務(wù)實現(xiàn)
 /// </summary>
 [ServiceBehavior()]
 public class ClientServer : IClientServer
 {
  /// <summary>
  /// 計算(測試方法)
  /// </summary>
  public double Calculate(TestData data)
  {
   return Math.Pow(data.X, data.Y);
  }
 }
}

四、服務(wù)端啟動服務(wù):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;
using WCFServer;

namespace 服務(wù)端
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
   BackWork.Run(() =>
   {
    OpenClientServer();
   }, null, (ex) =>
   {
    MessageBox.Show(ex.Message);
   });
  }

  /// <summary>
  /// 啟動服務(wù)
  /// </summary>
  private void OpenClientServer()
  {
   NetNamedPipeBinding wsHttp = new NetNamedPipeBinding();
   wsHttp.MaxBufferPoolSize = 524288;
   wsHttp.MaxReceivedMessageSize = 2147483647;
   wsHttp.ReaderQuotas.MaxArrayLength = 6553600;
   wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647;
   wsHttp.ReaderQuotas.MaxBytesPerRead = 6553600;
   wsHttp.ReaderQuotas.MaxDepth = 6553600;
   wsHttp.ReaderQuotas.MaxNameTableCharCount = 6553600;
   wsHttp.CloseTimeout = new TimeSpan(0, 1, 0);
   wsHttp.OpenTimeout = new TimeSpan(0, 1, 0);
   wsHttp.ReceiveTimeout = new TimeSpan(0, 10, 0);
   wsHttp.SendTimeout = new TimeSpan(0, 10, 0);
   wsHttp.Security.Mode = NetNamedPipeSecurityMode.None;

   Uri baseAddress = new Uri("net.pipe://localhost/pipeName1");
   ServiceHost host = new ServiceHost(typeof(ClientServer), baseAddress);

   ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
   host.Description.Behaviors.Add(smb);

   ServiceBehaviorAttribute sba = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
   sba.MaxItemsInObjectGraph = 2147483647;

   host.AddServiceEndpoint(typeof(IClientServer), wsHttp, "");

   host.Open();
  }
 }
}

五、客戶端數(shù)據(jù)實體類和服務(wù)接口類與服務(wù)端相同

六、客戶端服務(wù)實現(xiàn):

using DataStruct;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using WCFServer;

namespace DataService
{
 /// <summary>
 /// 服務(wù)實現(xiàn)
 /// </summary>
 public class ClientServer : IClientServer
 {
  ChannelFactory<IClientServer> channelFactory;
  IClientServer proxy;

  public ClientServer()
  {
   CreateChannel();
  }

  /// <summary>
  /// 創(chuàng)建連接客戶終端WCF服務(wù)的通道
  /// </summary>
  public void CreateChannel()
  {
   string url = "net.pipe://localhost/pipeName1";
   NetNamedPipeBinding wsHttp = new NetNamedPipeBinding();
   wsHttp.MaxBufferPoolSize = 524288;
   wsHttp.MaxReceivedMessageSize = 2147483647;
   wsHttp.ReaderQuotas.MaxArrayLength = 6553600;
   wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647;
   wsHttp.ReaderQuotas.MaxBytesPerRead = 6553600;
   wsHttp.ReaderQuotas.MaxDepth = 6553600;
   wsHttp.ReaderQuotas.MaxNameTableCharCount = 6553600;
   wsHttp.SendTimeout = new TimeSpan(0, 10, 0);
   wsHttp.Security.Mode = NetNamedPipeSecurityMode.None;

   channelFactory = new ChannelFactory<IClientServer>(wsHttp, url);
   foreach (OperationDescription op in channelFactory.Endpoint.Contract.Operations)
   {
    DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;

    if (dataContractBehavior != null)
    {
     dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
    }
   }
  }

  /// <summary>
  /// 計算(測試方法)
  /// </summary>
  public double Calculate(TestData data)
  {
   proxy = channelFactory.CreateChannel();

   try
   {
    return proxy.Calculate(data);
   }
   catch (Exception ex)
   {
    throw ex;
   }
   finally
   {
    (proxy as ICommunicationObject).Close();
   }
  }
 }
}

七、客戶端調(diào)用服務(wù)接口:

using DataService;
using DataStruct;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;
using WCFServer;

namespace 客戶端
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  //測試1
  private void button1_Click(object sender, EventArgs e)
  {
   button1.Enabled = false;
   txtSum.Text = string.Empty;

   IClientServer client = new ClientServer();
   double num1;
   double num2;
   double sum = 0;
   if (double.TryParse(txtNum1.Text, out num1) && double.TryParse(txtNum2.Text, out num2))
   {
    DateTime dt = DateTime.Now;
    BackWork.Run(() =>
    {
     sum = client.Calculate(new TestData(num1, num2));
    }, () =>
    {
     double time = DateTime.Now.Subtract(dt).TotalSeconds;
     txtTime.Text = time.ToString();
     txtSum.Text = sum.ToString();
     button1.Enabled = true;
    }, (ex) =>
    {
     button1.Enabled = true;
     MessageBox.Show(ex.Message);
    });
   }
   else
   {
    button1.Enabled = true;
    MessageBox.Show("請輸入合法的數(shù)據(jù)");
   }
  }

  //測試2
  private void button2_Click(object sender, EventArgs e)
  {
   button2.Enabled = false;
   txtSum.Text = string.Empty;

   IClientServer client = new ClientServer();
   double num1;
   double num2;
   double sum = 0;
   if (double.TryParse(txtNum1.Text, out num1) && double.TryParse(txtNum2.Text, out num2))
   {
    DateTime dt = DateTime.Now;
    BackWork.Run(() =>
    {
     for (int i = 0; i < 1000; i++)
     {
      sum = client.Calculate(new TestData(num1, num2));
     }
    }, () =>
    {
     double time = DateTime.Now.Subtract(dt).TotalSeconds;
     txtTime.Text = time.ToString();
     txtSum.Text = sum.ToString();
     button2.Enabled = true;
    }, (ex) =>
    {
     button2.Enabled = true;
     MessageBox.Show(ex.Message);
    });
   }
   else
   {
    button2.Enabled = true;
    MessageBox.Show("請輸入合法的數(shù)據(jù)");
   }
  }
 }
}

八、工具類BackWork類:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

/**
 * 使用方法:

BackWork.Run(() => //DoWork
{

}, () => //RunWorkerCompleted
{

}, (ex) => //錯誤處理
{

});
 
*/

namespace Utils
{
 /// <summary>
 /// BackgroundWorker封裝
 /// 用于簡化代碼
 /// </summary>
 public class BackWork
 {
  /// <summary>
  /// 執(zhí)行
  /// </summary>
  /// <param name="doWork">DoWork</param>
  /// <param name="workCompleted">RunWorkerCompleted</param>
  /// <param name="errorAction">錯誤處理</param>
  public static void Run(Action doWork, Action workCompleted, Action<Exception> errorAction)
  {
   bool isDoWorkError = false;
   Exception doWorkException = null;
   BackgroundWorker worker = new BackgroundWorker();
   worker.DoWork += (s, e) =>
   {
    try
    {
     doWork();
    }
    catch (Exception ex)
    {
     isDoWorkError = true;
     doWorkException = ex;
    }
   };
   worker.RunWorkerCompleted += (s, e) =>
   {
    if (!isDoWorkError)
    {
     try
     {
      if (workCompleted != null) workCompleted();
     }
     catch (Exception ex)
     {
      errorAction(ex);
     }
    }
    else
    {
     errorAction(doWorkException);
    }
   };
   worker.RunWorkerAsync();
  }

 }
}

九、效果圖示:

以上這篇WCF實現(xiàn)進程間管道通信Demo分享就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#中按指定質(zhì)量保存圖片的實例代碼

    C#中按指定質(zhì)量保存圖片的實例代碼

    這篇文章主要介紹了C#中按指定質(zhì)量保存圖片的實例代碼,有需要的朋友可以參考一下
    2013-12-12
  • Unity Shader實現(xiàn)黑幕過場效果

    Unity Shader實現(xiàn)黑幕過場效果

    這篇文章主要為大家詳細介紹了Unity Shader實現(xiàn)黑幕過場效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • C# Winform 實現(xiàn)TCP發(fā)消息

    C# Winform 實現(xiàn)TCP發(fā)消息

    這篇文章主要介紹了C# Winform 實現(xiàn)TCP發(fā)消息的示例,幫助大家更好的理解和學(xué)習(xí)使用c#技術(shù),感興趣的朋友可以了解下
    2021-03-03
  • Unity UGUI的LayoutElement布局元素組件介紹使用示例

    Unity UGUI的LayoutElement布局元素組件介紹使用示例

    這篇文章主要為大家介紹了Unity UGUI的LayoutElement布局元素組件介紹使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • C#使用Lazy<T>實現(xiàn)對客戶訂單的延遲加載

    C#使用Lazy<T>實現(xiàn)對客戶訂單的延遲加載

    這篇文章介紹了C#使用Lazy<T>實現(xiàn)對客戶訂單延遲加載的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • MVVM簡化的Messager類實例代碼

    MVVM簡化的Messager類實例代碼

    這篇文章主要給大家介紹了關(guān)于MVVM簡化的Messager類的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • C#裝箱和拆箱的原理介紹

    C#裝箱和拆箱的原理介紹

    這篇文章介紹了C#裝箱和拆箱的原理,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • C#實現(xiàn)自定義屏保的示例代碼

    C#實現(xiàn)自定義屏保的示例代碼

    這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)自定義屏保的功能,文中的示例代碼講解詳細,對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • C#實現(xiàn)UI控件輸出日志的方法詳解

    C#實現(xiàn)UI控件輸出日志的方法詳解

    一般情況下,我們的日志文件是用來記錄一些關(guān)鍵操作或者異常,并且是后臺存儲,并不對外開放的,但是也有些時候,需要將一些操作步驟、記錄等直接顯示在窗體上。本文就將利用UI控件輸出日志效果,需要的可以參考一下
    2022-10-10
  • Unity制作圖片字體的方法

    Unity制作圖片字體的方法

    這篇文章主要為大家詳細介紹了Unity制作圖片字體的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12

最新評論