Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm <em>fairly</em> certain that there's no combination of WCF bindings that will allow you to <em>literally</em> write to the client's stream. It does seem logical that there should be, given that somewhere beneath the surface there's definitely going to be a <code>NetworkStream</code>, but once you start adding encryption and all that fun stuff, WCF would have to know how to wrap this stream to turn it into the "real" message, which I don't think it does.</p> <p>However, the I-need-to-return-a-stream-but-component-X-wants-to-write-to-one scenario is a common one, and I have a catch-all solution that I use for this scenario, which is to create a two-way stream and spawn a worker thread to write to it. The <em>easiest</em> and <em>safest</em> way to do this (by which I mean, the way that involves writing the least code and therefore least likely to be buggy) is to use anonymous pipes.</p> <p>Here's an example of a method that returns an <a href="http://msdn.microsoft.com/en-us/library/system.io.pipes.anonymouspipeclientstream(VS.90).aspx" rel="nofollow noreferrer"><code>AnonymousPipeClientStream</code></a> that can be used in WCF messages, MVC results, and so on - anywhere you want to invert the direction:</p> <pre><code>static Stream GetPipedStream(Action&lt;Stream&gt; writeAction) { AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(); ThreadPool.QueueUserWorkItem(s =&gt; { using (pipeServer) { writeAction(pipeServer); pipeServer.WaitForPipeDrain(); } }); return new AnonymousPipeClientStream(PipeDirection.In, pipeServer.ClientSafePipeHandle); } </code></pre> <p>An example usage of this would be:</p> <pre><code>static Stream GetTestStream() { string data = "The quick brown fox jumps over the lazy dog."; return GetPipedStream(s =&gt; { StreamWriter writer = new StreamWriter(s); writer.AutoFlush = true; for (int i = 0; i &lt; 50; i++) { Thread.Sleep(50); writer.WriteLine(data); } }); } </code></pre> <p>Just two caveats about this approach:</p> <ol> <li><p>It will fail if the <code>writeAction</code> does anything to dispose the stream (that's why the test method doesn't actually dispose the <code>StreamWriter</code> - because that would dispose the underlying stream);</p></li> <li><p>It might fail if the <code>writeAction</code> doesn't actually write any data, because <code>WaitForPipeDrain</code> will return immediately and create a race condition. (If this is a concern, you can code a little more defensively to avoid this, but it complicates things a lot for a rare edge case.)</p></li> </ol> <p>Hopefully that helps in your specific case.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload