c# - WCF 内存急剧增加

标签 c# wcf file-upload binding stream

我正在使用 WCF 服务。我遇到的问题是它开始使用双内存。

我正在使用 HTTPS 绑定(bind)

<wsHttpBinding>
        <binding name="secureHttpBinding" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false"
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                  messageEncoding="Text" textEncoding="utf-8"  useDefaultWebProxy="true">
          <readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> 
          <security mode="Transport" >
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>

 <endpoint address="https://localhost/test.svc"
                 binding="wsHttpBinding"
                 bindingConfiguration="secureHttpBinding"
                 contract="IWcfContract"
                 name="SecureBasicHttpBinding_WcfContract"> 

这是我用来上传的代码

using (Stream fileStream = File.OpenRead(logsZipFullPath))
{
  // Call web server
  UploadFileResponse response = _webServiceHelper.UploadFile(fileStream, currentDate, ".zip", string.Empty);
  fileStream.Close();
}

这是我的模型

[MessageContract]
    public class UploadFileRequest : IDisposable
    {
        [MessageBodyMember(Order = 1)]
        public Stream FileByteStream;
        [MessageHeader(MustUnderstand = true)]
        public string FileDescription;

    }

我的 zip 文件是 80MB。

我遇到的问题是在服务开始时使用 26mb 这很好。在第一次调用时它使用 136MB,在调用完成后它下降到 26mb。这也很好。在第二次调用上传后,它开始使用 346MB 在服务调用后再次下降到 26mb。我的问题是为什么当文件只有 80MB 时它使用 346MB?我的 GC 和 disponse 已被正确调用。但是,这是正常行为还是我遗漏了什么?

最佳答案

终于找到解决办法了。根据this post

wsHttpBinding is the full-blown binding, which supports a ton of WS-* features and standards - it has lots more security features, you can use sessionful connections, you can use reliable messaging, you can use transactional control - just a lot more stuff, but wsHttpBinding is also a lot *heavier" and adds a lot of overhead to your messages as they travel across the network

我认为这是内存使用率高的主要原因。我们的要求是使用 HTTPS 证书,我们可以将其与 basicHttpBinding 一起使用。所以,我像这样更改了我的设置

<basicHttpBinding>
        <binding name="BasicHttpBinding_WcfContract" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false"
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed" useDefaultWebProxy="true">
          <readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <security mode="Transport"> <-- this is the main change
            <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
            <message clientCredentialType="UserName" algorithmSuite="Default"/>
          </security>
        </binding>
      </basicHttpBinding>

注意:我将安全模式none更改为transport以支持https。

现在,一切正常,没有问题。 两种可能导致内存问题的事情。

  1. wsHttpBinding 开销
  2. wsHttpBinding 不支持流模式

关于c# - WCF 内存急剧增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46169910/

相关文章:

c# - 如何使用 LINQ to SQL 创建通用数据访问对象 (DAO) CRUD 方法

c# - 什么是更好的做法 : duplicate code or use goto statement?

wcf 使用扁平化 WSDL 从 WCF 服务中提取 wsdl

php - Apache 2.4 + PHP 7.2 prefork mpm + 文件上传导致20秒后部分上传错误

用于 pdf、mp3 的 php 文件类型

c# - 所有版本的 Windows 上的 SystemColors 都一样吗?

c# - 在 C# 中生成 1 MB(或 n MB)文本文件

.net - 从 powershell 执行单向 wcf 服务操作

c# - .NET Web 服务 - 如何调用非托管 C dll

php - 使用 file_get_contents vs curl 获取文件大小