Consuming C# Webservice

Posted by Debby on Stack Overflow See other posts from Stack Overflow or by Debby
Published on 2010-06-10T19:33:24Z Indexed on 2010/06/10 19:53 UTC
Read the original article Hit count: 325

Filed under:
|

Hi,

I have a simple webservice 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 doesnt 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,

© Stack Overflow or respective owner

Related posts about c#

Related posts about webservice