2

In a WCF service there is a method for uploading image files:

UploadImage(Stream imageData);

Edit - as the answers suggest, the next line is wrong, and it's possible!

As Stream is being used, no other parameters are allowed for this method, but are needed.

I understand there are some open source projects which can handle multi-part stream in which I can pass more params, but was thinking request headers might be a simpler solution.

So I'm wondering what might be the downside of using the "request headers" approach in this case?

Thanks!

SuperFrog
  • 7,631
  • 9
  • 51
  • 81
  • 2
    Downvotes are fine, really. But without a comment it's not really constructive. – SuperFrog Jun 27 '14 at 15:36
  • You can wrap your stream and additional parameters in a `MessageContract`, as shown here for example: http://stackoverflow.com/questions/1339857/wcf-using-streaming-with-message-contracts – Kirill Shlenskiy Jun 27 '14 at 15:54

1 Answers1

2

As Stream is being used, no other parameters are allowed for this method, but are needed.

Not really. This should work.

[OperationContract,WebInvoke(UriTemplate="{name}")]
UploadImage(Stream imageData, string name);

Here is a working sample

async void TestMethod()
{
    Task.Run(() =>
    {
        var host = new WebServiceHost(typeof(MyContract), new Uri("http://0.0.0.0:8088/Test"));
        host.Open();
    });

    await Task.Delay(2000);

    new Webclient().UploadData("http://localhost:8088/Test/UploadImage/abc.bmp", new byte[] { 65, 66, 67, 68, 69 });
}


[ServiceContract]
class MyContract 
{
    [OperationContract, WebInvoke(UriTemplate = "/UploadImage/{name}")]
    public void UploadImage(Stream s, string name)
    {
        Console.WriteLine(name  +  " -> " + new StreamReader(s).ReadToEnd());
    }
}
L.B
  • 114,136
  • 19
  • 178
  • 224
  • I guess my assumption was wrong, though it was based on quite a few questions I read on SO and other resources on the web. – SuperFrog Jun 27 '14 at 15:53
  • Correct me if I'm wrong, but the way I am reading the documentation this may very well work, but the transfer will not actually be *streamed* (which I am assuming is the point of the exercise): http://msdn.microsoft.com/en-us/library/ms789010(v=vs.110).aspx – Kirill Shlenskiy Jun 27 '14 at 16:09
  • @KirillShlenskiy `WebServiceHost` can easily be configured as `webHttpBinding.TransferMode = TransferMode.Streamed;`. I skipped all details to post a *small and readible* answer. – L.B Jun 27 '14 at 16:12
  • @L.B: "Note that adding a second parameter to the following Echo or ProvideInfo operations causes the service model to revert back to a buffered strategy and use the run-time serialization representation of the stream. Only operations with a single input stream parameter are compatible with end-to-end request streaming. This rule similarly applies to message contracts." http://msdn.microsoft.com/en-us/library/ms733742(v=vs.110).aspx – Kirill Shlenskiy Jun 27 '14 at 16:23
  • @KirillShlenskiy Let's take a more concrete way to see what is going on really. Instead of referencing some links, use Fiddler2 and and run above code. You will not see a *serialized* content in the http-body, just pure binary data I sent with webClient. – L.B Jun 27 '14 at 16:33
  • @KirillShlenskiy what I see is `Content-Length: 5`. – L.B Jun 27 '14 at 16:39