c# - HTTP WCF Service keep the stream open -


i'm trying stream data client on http. achieve i'm using wcf service webhttpbinding. problem system.io.stream operation returns closed before can write it. i'd keep stream open until data needs written it. not more half minute.

in service request method create new instance of system.io.memorystream put collection of streams , return function output. later on when there audio data available i'm writing streams in collection. the requests have been closed. when go endpoint url browsers standard player greyed out. tested rest client, showed me request closes after return statement.

the problem use libspotify sdk retrieve music. sends 8192 bytes of pcm data per cycle. make possible users play music chromecast device. chromecast doesn't support pcm data that's why convert mp3 libmp3lame , send through output stream chromecast. approach work need connection stay alive though there no actual data being send through stream.

the libspotify music delivery callback can found here.

this how set-up service:

/// <summary> /// wcf service host. /// </summary> private servicehost servicehost;  /// <summary> /// start http wcf service. /// </summary> public void startlistening() {     if (servicehost == null)     {         servicehost = new servicehost(typeof(streamingservice));          var binding = new webhttpbinding(webhttpsecuritymode.none);         binding.transfermode = transfermode.streamedresponse;          var endpoint = servicehost.addserviceendpoint(typeof(streamingcontract), binding, new uri(streamaddress));         endpoint.endpointbehaviors.add(new webhttpbehavior());          servicehost.open();     } } 

this service implementation:

[servicecontract(name="streamingcontract")] interface streamingcontract {     [webget(uritemplate="audio")]     [operationcontract()]     stream audio(); }  [servicebehavior(instancecontextmode = instancecontextmode.single,                  includeexceptiondetailinfaults = true)] public class streamingservice : streamingcontract {     public system.io.stream audio()     {         var stream = new system.io.memorystream();         app.logic.streaming.streams.add(stream);          weboperationcontext.current.outgoingresponse.contenttype = "audio/mp3";         weboperationcontext.current.outgoingresponse.contentlength = 1000;          return stream;     } } 

i tried setting: [operationcontext(autodisposeparameter=false)] on audio() in servicecontract. started throwing system.invalidoperationexception. thought maybe it's problem content length unknown, didn't either.

your service doing should - returning empty stream , closing connection.

it sounds want wait stream asynchronously filled. in order that, you'll have implement kind of callback. should task.run() method, standard way of implementing asynchronous logic in .net.


Popular posts from this blog