tags:

views:

162

answers:

3

Hi,

I have a simple web service running and I have a console application client consuming the service. I did have issues getting this running and I had been helped by some wonderful people in this community.

I have another problem: if I want to call the service from the client in a loop, it doesn't work. It works only for the first time and then it just keeps waiting. Why is this happening and how can I resolve it.

The code:

namespace WebService 
{ 
   [ServiceContract] 
   public interface IService 
   { 
     [OperationContract(Name="Result")] 
     [WebGet(UriTemplate = "/")] 
     Stream Result();   
   } 

   public class Service:IService 
   { 
        public Stream Result() 
        { 
           // read a file from the server and return it as stream 
        } 
   } 
}

The client:
namespace WebServiceClient
{
   [ServiceContract] 
   public interface IService 
   { 
     [OperationContract(Name="Result")] 
     [WebGet(UriTemplate = "/")] 
     Stream Result();   
   } 

}

static void Main()
{

     Console.WriteLine("Press enter when the service is available");
     Console.ReadLine();

     // creating factory
     HttpChunkingBinding binding = new HttpChunkingBinding();
     binding.MaxReceivedMessageSize = 0x7fffffffL;

     ChannelFactory<WebServiceClient.IService> factory = new   ChannelFactory<WebServiceClient.IService>
            (binding, new EndpointAddress("http://localhost/WebService/Service"));

            WebServiceClient.IService service = factory.CreateChannel();

       for(int i = 0; i < 10; i++)
       {
            Stream s = service.Result();
            // write this stream to a file and close the stream
       }

            //Closing our channel.
            ((IClientChannel)service).Close();

}

Thanks,

A: 

You haven't posted code, so I'm going to give a wild guess:

You have a maximum number of open connections set to 1 and are opening a connection to the webservice in the loop, yet not closing the connection as part of the loop. This creates a situation where your second iteration is waiting for timeout on the first connection (which is probably set to 10 minutes).

Randolpho
I think you're onto something, but he's clearly not manually opening the channel; more likely, he's not closing the stream properly, which will force the connection to stay open.
Aaronaught
A: 

Looks like you're trying to implement a Chunking Channel. Have a look at this article that defines how to implement it.

Near the bottom of the article, it explains how to set up the WCF sample project. There is a chunking example in the sample project.

I'm pretty sure that the connection isn't closing because you haven't finished reading all of the data. Because you are Chunking, not all of the data is delivered at the same time. You ask for a chunk, process it and then ask for another chunk.

Good Luck,

Patrick

Patrick
FWIW, in my WCF REST projects (which is what this is AFAICT) I haven't needed to use an external chunking channel (like the sample from the SDK), the stack already supports accepting and returning Stream just fine (and it's worked great for me).FWIW, AFAICT it does use http chunking to implement that Stream interface (quick check with Fiddler), but they abstract it away for you :)
James Manning
Interesting... In that case, maybe @Debby could try using a WebHttpBinding instead of the HttpChunkingBinding used in the example code. If the REST service does implement Chunking in the background, it should work. Give it a Try @Debby and let us know if it works. Thanks for your input @James Manning
Patrick
A: 

Just a guess, but sounds like it has something to do with your connection to the service not closing... try the following:

   for(int i = 0; i < 10; i++) 
   { 
        ChannelFactory<WebServiceClient.IService> factory = 
           new ChannelFactory<WebServiceClient.IService>(
               binding, 
               new EndpointAddress("http://localhost/WebService/Service")); 

        WebServiceClient.IService service = factory.CreateChannel(); 
        using(service as IDsposable)
        {
          using(MemoryStream s = service.Result() as MemoryStream)
          {
             // write this stream to a file
          }
        }
   } 
Partha Choudhury