繁体   English   中英

消耗WCF Web服务

[英]Consuming WCF Web Service

我有一个运行的简单Web服务,并且有一个使用该服务的控制台应用程序客户端。 在运行该程序时确实遇到了问题,并且该社区中的一些很棒的人为我提供了帮助。

我还有另一个问题:如果我想从客户端循环调用服务,那么它将无法正常工作。 它仅在第一次工作,然后一直保持等待状态。 为什么会发生这种情况,我该如何解决。

编码:

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();

}

谢谢,

只是一个猜测,但听起来与您与服务的连接未关闭有关系...请尝试以下操作:

   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
          }
        }
   } 

您尚未发布代码,因此我将给出一个疯狂的猜测:

您已将打开的连接的最大数量设置为1,并且正在循环中打开与Web服务的连接,但没有在循环中关闭连接。 这会导致您的第二次迭代正在等待第一个连接上的超时(可能设置为10分钟)的情况。

看起来您正在尝试实现Chunking频道。 看一下这篇文章 ,它定义了如何实现它。

在文章底部附近,它说明了如何设置WCF示例项目。 示例项目中有一个分块示例。

我非常确定连接没有关闭,因为您尚未完成所有数据的读取。 因为您正在分块,所以并非所有数据都同时传送。 您需要一个块,对其进行处理,然后再请求另一个块。

祝好运,

帕特里克

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM