WCF Web --------返回自定义格式报文结构
从MSDN中可以看出WCF Web 模式除了可以返回和接受 XML, JSON 以及聚合Feed,还可以返回自定义格式的数据.
下面做简单介绍:
(1)返回图片格式的流数据给客户端:
1
public Stream GetImage(int width, int height)
2
{
3
Bitmap bitmap = new Bitmap(width, height);
4
for (int i = 0; i < bitmap.Width; i++)
5
{
6
for (int j = 0; j < bitmap.Height; j++)
7
{
8
bitmap.SetPixel(i, j, (Math.Abs(i - j) < 2) ? Color.Blue : Color.Yellow);
9
}
10
}
11
MemoryStream ms = new MemoryStream();
12
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
13
ms.Position = 0;
14
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
15
return ms;
16
}
public Stream GetImage(int width, int height)2
{3
Bitmap bitmap = new Bitmap(width, height);4
for (int i = 0; i < bitmap.Width; i++)5
{6
for (int j = 0; j < bitmap.Height; j++)7
{8
bitmap.SetPixel(i, j, (Math.Abs(i - j) < 2) ? Color.Blue : Color.Yellow);9
}10
}11
MemoryStream ms = new MemoryStream();12
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);13
ms.Position = 0;14
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";15
return ms;16
}
(2)返回自定义报文格式的流数据给客户端:
1
public Stream DownloadTransInfo()
2
{
3
4
string strBlackList = FormatBlackList();
5
byte[] bBlackList = System.Text.Encoding.UTF8.GetBytes(strBlackList );
6
MemoryStream strStream = new MemoryStream(bBlackList );
7
8
WebOperationContext.Current.OutgoingResponse.ContentType
9
= "text/html; charset=utf-8";
10
11
return strStream;
12
}
public Stream DownloadTransInfo()2
{3
4
string strBlackList = FormatBlackList();5
byte[] bBlackList = System.Text.Encoding.UTF8.GetBytes(strBlackList );6
MemoryStream strStream = new MemoryStream(bBlackList );7

8
WebOperationContext.Current.OutgoingResponse.ContentType 9
= "text/html; charset=utf-8";10

11
return strStream;12
}
还有其他格式,下次继续.


浙公网安备 33010602011771号