Wcf突然挂起(网络获取操作)

Vla*_*žić 6 c# wcf

我阅读了很多关于客户端应该关闭连接的文章,client.Close()因此WCF默认限制不会被超过.事实上,我有WCF WebGet操作,基本上只返回一个图像.

[ServiceContract(SessionMode = SessionMode.NotAllowed)]
[ValidationBehavior]
public interface IImagesService
{
    [OperationContract(Name = "ImagesGet4")]
    [WebGet(UriTemplate = "/Image/{partner}/{id}/{image}_{width}_{height}.jpg")]
    Stream ImagesGet2(string partner, string id, string image, string width, string height);
  }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,客户端是浏览器,无论我使用wcf配置做什么都无济于事.maxConnections,maxReceivedMessageSize,maxBytesPerRead 很多其他参数都被淘汰出局,但仍然没有运气.

编辑: 这是附加代码: ImageGet服务调用的方法:

       public Stream ImagesGet(string partner, string id, string image, string      width = null, string height = null)
        {
           WSData.EventLogs.MinimalEventSource.Log.ClientIp("");
           WSData.EventLogs.MinimalEventSource.Log.ServicePath("");
           WSData.EventLogs.MinimalEventSource.Log.Message( DateTime.Now + " | " + partner );


        bool cache;
        var images = new Images();
        var stream = images.ImagesGetStream(out cache, partner, id, image, width, height);

        if (cache)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Cache-Control", "public, max-age=604800");
            WebOperationContext.Current.OutgoingResponse.LastModified = DateTime.Today;
            WebOperationContext.Current.OutgoingResponse.SetETag(id);
        }

        WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";

        OperationContext clientContext = OperationContext.Current;
        clientContext.OperationCompleted += new EventHandler(delegate (object sender, EventArgs args)
        {
            if (stream != null)
                stream.Dispose();
        });


        return stream;
    }
Run Code Online (Sandbox Code Playgroud)

ImagesGetStream其通过上述方法调用的方法是:

 public Stream ImagesGetStream( out bool cache, string partner, string id, string image, string width = null, string height = null, string background = null )
    {

        string PARTNER = partner;
        cache = true;

        try
        {
            EventLogs.MinimalEventSource.Log.Load( 10, "DatabaseCall" );
            var img = ImagesDL.GetImage( PARTNER, new PrimaryKey( id ) );
            EventLogs.MinimalEventSource.Log.Unload( 13 );

            EventLogs.MinimalEventSource.Log.Load( 14, "GettingImageDir" );
            var imagesRoot = Path.Combine( BaseConfiguration.GetDocumentsSharedDirectory( PARTNER ), img.Url );


            var isWatermarked = img.Group.Contains( "WEBES" ) == false && ( partner.ToUpper() == "ZG-ADRIAGATE1" || partner.ToUpper() == "LENIO-ADRIAGATE2" || partner.ToUpper() == "LENIO" );

            EventLogs.MinimalEventSource.Log.Unload( 15 );

            EventLogs.MinimalEventSource.Log.Load( 16, "ImageToStream" );
            var stream = new FileStream( imagesRoot, FileMode.Open, FileAccess.Read, FileShare.Read );
            EventLogs.MinimalEventSource.Log.Unload( 17 );


            if (!string.IsNullOrEmpty(width))
            {
                var isBackground = !string.IsNullOrEmpty(background);

                int widthp = 0;
                int heightp = 0;

                int.TryParse(width, out widthp);
                int.TryParse(height, out heightp);

                return ResizeImage(partner, stream, new Size(widthp, heightp), isBackground, img.Guest, isWatermarked, background);
            }
            else if(img.Group.Contains("WEBES") == false) {

                Image imgToResize = Image.FromStream(stream);

                if(imgToResize.Width > imgToResize.Height && imgToResize.Width > 2048 )
                    return ResizeImage(partner, stream, new Size(2048, 1536), false, img.Guest, isWatermarked, background);

                else if (imgToResize.Width < imgToResize.Height && imgToResize.Height > 2048)
                    return ResizeImage(partner, stream, new Size(1536, 2048), false, img.Guest, isWatermarked, background);

                else if (imgToResize.Width == imgToResize.Height && imgToResize.Height > 2048)
                    return ResizeImage(partner, stream, new Size(2048, 2048), false, img.Guest, isWatermarked, background);
            }

            return isWatermarked ? WatermarkingImage( partner, stream, img.Guest, isWatermarked ) : stream;



        }
        catch ( Exception )
        {
            cache = false;
            return new FileStream( AppPath + @"\App_Resorces\NoImage.jpg", FileMode.Open, FileAccess.Read, FileShare.Read );
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是配置的相关部分:

<service name="WSTraveller.ImagesService" behaviorConfiguration="ServiceBehavior">
    <endpoint address="soap" binding="basicHttpBinding" behaviorConfiguration="soapBehavior" contract="WSTraveller.IImagesService" bindingConfiguration="soapBinding" bindingName="soapBinding" bindingNamespace="http://ws.adriagate.com/TRWS/ImagesService.svc/soap"/>
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="poxBehavior" contract="WSTraveller.IImagesService" bindingConfiguration="webBinding" bindingName="webBinding" bindingNamespace="http://ws.adriagate.com/TRWS/ImagesService.svc/pox"/>
  </service>
Run Code Online (Sandbox Code Playgroud)

...

        <basicHttpBinding>
    <binding name="soapBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" messageEncoding="Text">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
      <security mode="None"/>
    </binding>
  </basicHttpBinding>
Run Code Online (Sandbox Code Playgroud)

小智 1

我的两分钱..检查这是否对您有帮助:

1)尝试使用gzip流压缩图像,在我们的项目中,我们必须使用wcf通过线路发送巨大的数据集,因此我们过去常常压缩然后发送它。

2)如果数据库是瓶颈,那么您可以使数据库调用异步并使用队列机制来存储结果,并且wcf客户端请求将尝试定期将结果出列。