如何从WCF服务[nettcpbinding]返回流?

时间:2012-05-15 19:20:32

标签: c# wcf stream

我有一个WCF服务,我希望客户端通过调用应该返回client.GetStream();

MemoryStream来接收来自此服务的图片
    public Stream GetStream()
    {
        Rectangle bounds = Screen.GetBounds(Point.Empty);
        using (Bitmap bmp = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
            }
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            img = Image.FromStream(ms);
            ms.Position = 0;
            OperationContext.Current.OutgoingMessageHeaders.Action = "image/jpeg";
            return ms;
        }
    }

IScreenShot界面:

[ServiceContract]
public interface IScreenShot
{
    [OperationContract]
    Stream GetStream();
}

我得到的第一个例外是 MaxReceivedMessageSize ,然后我把它增加到2MB。
现在我有了一个新的例外:

Multiple headers with name 'Action' and namespace 'http://www.w3.org/2005/08/addressing' found.

请注意,我按照此示例的步骤操作:

Sample Example(returning Image as Stream) from MSDN 但我使用NetTcpBinding的区别。

代码有问题吗?或者它不能与nettcpbinding一起使用?

这是客户:

public partial class ScreenImage : Form
{
    ScreenShotClient client;

    public ScreenImage(string baseAddress)
    {
        InitializeComponent();
        NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
        binding.MaxReceivedMessageSize = 1024 * 1024 * 2;
        client = new ScreenShotClient(binding, new EndpointAddress(baseAddress));
    }

    private void btnNew_Click(object sender, EventArgs e)
    {
        picBox.Image = Image.FromStream(client.GetStream());
    }
}

0 个答案:

没有答案