Consuming WCF Web Service
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")]
开发者_StackOverflow [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,
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
}
}
}
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).
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
精彩评论