tags:

views:

31

answers:

1

Hi, I have created a WCF service to stream files(download). Code for the service is below

public Stream GetCoverScan(List<string> productIDs)
{
    FileStream stream = new FileStream("", FileMode.Open, FileAccess.Read);
    return stream;
}

Can some one tell me how do i consume this on the client side. I have already created a proxy on client and i can see the method by creating an object of the service, but how do i read the stream.

Please advise

Configuration

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="StreamedHttp" transferMode="StreamedResponse"
                     maxReceivedMessageSize="67108864">
            </binding>
        </basicHttpBinding>
    </bindings>
    <services>
        <service name="Streaming.Service1"
                 behaviorConfiguration="Streaming.Service1Behavior">
            <endpoint address="" bindingConfiguration="StreamedHttp"
                      binding="basicHttpBinding" contract="Streaming.IService1">
            </endpoint>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="Streaming.Service1Behavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

Contract

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(string name);

    [OperationContract]
    System.IO.Stream GetCoverScan(List<string> productIDs);
}

bindings

</bindings>
+1  A: 

You need to configure streaming on the binding you use. Streaming is supported for BasicHttpBinding, NetTcpBinding, and NetNamedPipeBinding. So if you have a BasicHttpBinding, your configuration should look like this:

<basicHttpBinding>
    <binding name="HttpStreaming" maxReceivedMessageSize="67108864"
             transferMode="StreamedResponse"/>
</basicHttpBinding>

I use StreamedResponse here because you only have a response that should be a stream, not a request.

How you read the stream itself depends on what's in it. Suppose you send a text file over a stream, you can use a StreamReader:

var reader = new StreamReader(service.GetCoverScan(...));
string contents = reader.ReadToEnd();

If you send an xml file, you can read it through XDocument:

var doc = XDocument.Load(service.GetCoverScan(...));

So it really depends on what you're sending over the wire.

Ronald Wildenberg
Roanld, thanks for the reply, yeah i have already done that. <basicHttpBinding> <binding name="StreamedHttp" transferMode="StreamedResponse" maxReceivedMessageSize="67108864"> </binding> </basicHttpBinding>
Amit
ronald, could you please advise me on how do i get the stream at client side? Darin (above) advised on using the belowbyte[] result = proxy.GetCoverScan(productIDs);File.WriteAllBytes("foo.dat", result);But using this will buffer the response i guess but i want to stream. Its a little confusing please helpthanks
Amit
The generated client proxy should have a signature that returns a Stream, **not** a byte array. I just tested this myself. So something goes wrong while generating the client. Can you add your configuration to your question?
Ronald Wildenberg
Ronald, i have added the configuration in the question. Please advise
Amit
Can you also add your bindings (at least the basicHttpBinding)?
Ronald Wildenberg
Sorry i haven't defined any binding outside of the config file.
Amit
Please find the binding below<bindings> <basicHttpBinding> <binding name="StreamedHttp" transferMode="StreamedResponse" maxReceivedMessageSize="67108864"> </binding> </basicHttpBinding> </bindings>
Amit
i have added the bindings above in the question as well but for some reason they are not appearing. I can see them when i edit my question
Amit
But this binding is not in your configuration file???
Ronald Wildenberg
it is there in the config file but for some reason this website is not displaying it
Amit
Strange. I use the same configuration you have and my client returns a stream and not a byte array. How do you generate the client? Via svcutil or via Add Service Reference in Visual Studio?
Ronald Wildenberg
i do it through "add web reference"
Amit
What version of Visual Studio do you use? 'Add web reference' does not generate a WCF client. Before WCF you had ASMX web services that supported only a small part of the SOAP stack. Streaming is not supported for these types of clients.
Ronald Wildenberg
i m on framework 3.5
Amit
Do you not have a 'Add Service Reference' option somewhere? I'm using VS2010 myself. I suppose you use VS2008?
Ronald Wildenberg
i think the way i m creating the proxy is incorrect I am doing add web reference whereas i should be doing add service reference
Amit
Precisely :) You should use 'Add Service Reference'.
Ronald Wildenberg
Ronald, I used add service reference and i can now see it returns a stream. I can't thank you enough. Appreciate your patience.One small lil thing now that i have the stream as my return type how do i read from it.
Amit
Your welcome :) I added some info to my answer that tells you how to read from a stream.
Ronald Wildenberg
lovely thank you. You are a champion mate :)
Amit