新建一個WinForm項目WCF_WinformCient:
右擊項目名稱,選擇“添加服務引用...”
app.config:
app.config代碼:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDataTransfers" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536000" maxBufferPoolSize="524288000" maxReceivedMessageSize="65536000"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="163840000"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/WCFMassData/Service.svc/basic"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDataTransfers"
contract="WS.IDataTransfers" name="BasicHttpBinding_IDataTransfers" />
</client>
</system.serviceModel>
</configuration>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDataTransfers" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536000" maxBufferPoolSize="524288000" maxReceivedMessageSize="65536000"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="163840000"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/WCFMassData/Service.svc/basic"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDataTransfers"
contract="WS.IDataTransfers" name="BasicHttpBinding_IDataTransfers" />
</client>
</system.serviceModel>
</configuration>
最後添加form1窗體:
form1.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WCF_WinformCient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//實例化WCF客戶端
WS.DataTransfersClient client = new WS.DataTransfersClient("BasicHttpBinding_IDataTransfers");
MemoryStream stream = new MemoryStream();
byte[] buffer;
//獲取所用塊壓縮流,並組裝
buffer = client.GetCurrentBuffer();
//while (client.ReadNextBuffer())
stream.Write(buffer,0,buffer.Length);
stream.Position = 0;
buffer = new byte[stream.Length];
stream.Read(buffer,0,buffer.Length);
stream.Close();
//解壓壓縮流
byte[] bytes = Compression(buffer, CompressionMode.Decompress);
stream = new MemoryStream(bytes);
IFormatter formatter = new BinaryFormatter();
//反序列化
DataSet ds = (DataSet)formatter.Deserialize(stream);
stream.Close();
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "test";
this.label1.Text = ds.Tables[0].Rows.Count.ToString();
client.Close();
}
private byte[] Compression(byte[] data, CompressionMode mode)
{
DeflateStream zip = null;
try
{
if (mode == CompressionMode.Compress)
{
MemoryStream ms = new MemoryStream();
zip = new DeflateStream(ms, mode, true);
zip.Write(data, 0, data.Length);
zip.Close();
return ms.ToArray();
}
else
{
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, data.Length);
ms.Flush();
ms.Position = 0;
zip = new DeflateStream(ms, mode, true);
MemoryStream os = new MemoryStream();
int SIZE = 1024;
byte[] buf = new byte[SIZE];
int l = 0;
do
{
l = zip.Read(buf, 0, SIZE);
if (l == 0) l = zip.Read(buf, 0, SIZE);
os.Write(buf, 0, l);
} while (l != 0);
zip.Close();
return os.ToArray();
}
}
catch
{
if (zip != null) zip.Close();
return null;
}
finally
{
if (zip != null) zip.Close();
}
}
}
}
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WCF_WinformCient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//實例化WCF客戶端
WS.DataTransfersClient client = new WS.DataTransfersClient("BasicHttpBinding_IDataTransfers");
MemoryStream stream = new MemoryStream();
byte[] buffer;
//獲取所用塊壓縮流,並組裝
buffer = client.GetCurrentBuffer();
//while (client.ReadNextBuffer())
stream.Write(buffer,0,buffer.Length);
stream.Position = 0;
buffer = new byte[stream.Length];
stream.Read(buffer,0,buffer.Length);
stream.Close();
//解壓壓縮流
byte[] bytes = Compression(buffer, CompressionMode.Decompress);
stream = new MemoryStream(bytes);
IFormatter formatter = new BinaryFormatter();
//反序列化
DataSet ds = (DataSet)formatter.Deserialize(stream);
stream.Close();
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "test";
this.label1.Text = ds.Tables[0].Rows.Count.ToString();
client.Close();
}
private byte[] Compression(byte[] data, CompressionMode mode)
{
DeflateStream zip = null;
try
{
if (mode == CompressionMode.Compress)
{
MemoryStream ms = new MemoryStream();
zip = new DeflateStream(ms, mode, true);
zip.Write(data, 0, data.Length);
zip.Close();
return ms.ToArray();
}
else
{
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, data.Length);
ms.Flush();
ms.Position = 0;
zip = new DeflateStream(ms, mode, true);
MemoryStream os = new MemoryStream();
int SIZE = 1024;
byte[] buf = new byte[SIZE];
int l = 0;
do
{
l = zip.Read(buf, 0, SIZE);
if (l == 0) l = zip.Read(buf, 0, SIZE);
os.Write(buf, 0, l);
} while (l != 0);
zip.Close();
return os.ToArray();
}
}
catch
{
if (zip != null) zip.Close();
return null;
}
finally
{
if (zip != null) zip.Close();
}
}
}
}