对于WCF服务来说,默认情况下,其接收的最大数据量为65535字节。如超过这个范围,就必须修改默认设置。
1 客户端。客户端接收的最大数据值由传输协议的MaxReceivedMessageSize确定,示例如下:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class
Program
{
static void Main( string [] args)
{
Console.WriteLine( " Invocate self-host GetPictureFilestreem service... ... " );
XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
quotas.MaxArrayLength = 65535 ;
BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.ReaderQuotas = quotas;
httpBinding.MaxReceivedMessageSize = 6553600 ;
EndpointAddress httpAddress = new EndpointAddress( " http://localhost:8888/PictureStreemTransfers " );
WCFStreemTransfersClient client = new WCFStreemTransfersClient(httpBinding, httpAddress);
string desPath = System.Environment.CurrentDirectory + " \\result.jpg " ;
byte [] buf = client.GetPictureFilestreem();
File.WriteAllBytes(desPath,buf);
Console.WriteLine( @" write file = " + desPath);
Console.Read();
}
}
{
static void Main( string [] args)
{
Console.WriteLine( " Invocate self-host GetPictureFilestreem service... ... " );
XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
quotas.MaxArrayLength = 65535 ;
BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.ReaderQuotas = quotas;
httpBinding.MaxReceivedMessageSize = 6553600 ;
EndpointAddress httpAddress = new EndpointAddress( " http://localhost:8888/PictureStreemTransfers " );
WCFStreemTransfersClient client = new WCFStreemTransfersClient(httpBinding, httpAddress);
string desPath = System.Environment.CurrentDirectory + " \\result.jpg " ;
byte [] buf = client.GetPictureFilestreem();
File.WriteAllBytes(desPath,buf);
Console.WriteLine( @" write file = " + desPath);
Console.Read();
}
}
上例中调用服务端的GetPictureFilestreem()契约,获取返回的2进制数据流,写入result.jpg文件。
由于数据流的长度大于65535,所以通过设定
quotas.MaxArrayLength = 65535;
httpBinding.ReaderQuotas = quotas;
httpBinding.MaxReceivedMessageSize = 6553600;
修改客户端接收的最大数据值。
上例中关联client定义如下:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class
WCFStreemTransfersClient : ClientBase
<
IStreemTransfers
>
, IStreemTransfers
{
public WCFStreemTransfersClient() : base ()
{ }
public WCFStreemTransfersClient( string endpointName)
: base (endpointName)
{ }
public WCFStreemTransfersClient(Binding binding, EndpointAddress address)
: base (binding, address)
{ }
public byte [] GetPictureFilestreem() {
return this .Channel.GetPictureFilestreem();
}
}
// IStreemTransfers
[ServiceContract]
public interface IStreemTransfers
{
[OperationContract]
byte [] GetPictureFilestreem();
}
{
public WCFStreemTransfersClient() : base ()
{ }
public WCFStreemTransfersClient( string endpointName)
: base (endpointName)
{ }
public WCFStreemTransfersClient(Binding binding, EndpointAddress address)
: base (binding, address)
{ }
public byte [] GetPictureFilestreem() {
return this .Channel.GetPictureFilestreem();
}
}
// IStreemTransfers
[ServiceContract]
public interface IStreemTransfers
{
[OperationContract]
byte [] GetPictureFilestreem();
}
2 服务端。
wcf并没有限制服务端发送数据的大小,因此服务端简单实现该服务契约就可以了。
服务契约接口:
[ServiceContract]
public interface IStreemTransfers
{
[OperationContract]
byte [] GetPictureFilestreem();
}
public interface IStreemTransfers
{
[OperationContract]
byte [] GetPictureFilestreem();
}
//服务端实现
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class
Program
{
static void Main( string [] args)
{
ServiceHost host = new ServiceHost( typeof (PictureStreemTransfers));
host.Open();
Console.WriteLine( " 服务中 " );
Console.Read();
Console.Read();
}
}
// GetPictureFilestreem
public class PictureStreemTransfers : IStreemTransfers
{
public byte [] GetPictureFilestreem() {
try
{
string path = System.Environment.CurrentDirectory + " \\test.jpg " ;
Console.WriteLine( " Server send File = " + path);
return File.ReadAllBytes(path);
}
catch (Exception ex) {
Console.WriteLine( " Server Error: " + ex.Message);
return null ;
}
}
{
static void Main( string [] args)
{
ServiceHost host = new ServiceHost( typeof (PictureStreemTransfers));
host.Open();
Console.WriteLine( " 服务中 " );
Console.Read();
Console.Read();
}
}
// GetPictureFilestreem
public class PictureStreemTransfers : IStreemTransfers
{
public byte [] GetPictureFilestreem() {
try
{
string path = System.Environment.CurrentDirectory + " \\test.jpg " ;
Console.WriteLine( " Server send File = " + path);
return File.ReadAllBytes(path);
}
catch (Exception ex) {
Console.WriteLine( " Server Error: " + ex.Message);
return null ;
}
}