WCF Error on Streaming back to client callback using NetTcpBinding

2.2k Views Asked by At

I have a WCF service, using NetTcpBinding TransferMode.Streamed, I'm looking to stream back to client using its callback, but i get this exception on the line host.Open:

Contract requires Duplex, but Binding 'NetTcpBinding' doesn't support it or isn't configured properly to support it.

 ServiceHost host;
    public Form1()
    {
        InitializeComponent();
    }   
    private void button1_Click(object sender, EventArgs e)
    {
        Uri baseAddress = new Uri(string.Format("net.tcp://{0}:1991/service", Dns.GetHostName()));
        host = new ServiceHost(typeof(WCF_Server.MainService), baseAddress);
        host.Open();
    }

service interface :

[ServiceContract(CallbackContract = typeof(IScreenCallback))]
public interface IScreenShot
{
    [OperationContract]
    Stream GetStream(int formatIndex);

    [OperationContract]
    void ShowGallery();
    [OperationContract]
    void CloseGallery();

    [OperationContract]
    void AddImage(Stream stream);
}

public interface IScreenCallback
{
    [OperationContract]
    void NextImage();

    [OperationContract]
    void PrevImage();

    [OperationContract]
    void AddImageClient(Stream stream);
}

how would i pass stream to client callback?

1

There are 1 best solutions below

4
On

Your IScreenShot contract isn't fully one-way. It needs to be for a duplex contract with is one-way in one direction and also one-way into the other.

That said, streaming and duplex don't mix, at all, because of internal mechanics that require the messages to be buffered. So this wouldn'[t work anyways.

To make this scenario work in a duplex mode you should chop up the data into reasonably sized byte[] chunks and transfer them in chunks instead of as streams. You can make that contract look pretty much like Stream's Write or even wrap an instance of the contract in a Stream-derived proxy-wrapper on the send side, so that it looks pretty much the same to whoever populates the stream.