【问题标题】:WCF problem with uploading large file, hosted in IIS上传大文件的 WCF 问题,托管在 IIS 中
【发布时间】:2017-03-03 21:26:02
【问题描述】:

我正在尝试将大文件上传到托管在 IIS 中的 WCF 服务。

我正在使用 Restful 和 Streaming 方法。

但我无法上传超过 64KB 的文件。

我通过更改 web.config 文件中所有与大小相关的元素尝试了很多,但未能解决问题。

这是我的代码和配置,如果有人在代码中发现任何问题以及如何解决,请告诉我。

运营合同

[OperationContract]
[WebInvoke(UriTemplate = "/UploadImage/{filename}")]
bool UploadImage(string filename, Stream image);

运营合同的实施

public bool UploadImage(string filename, Stream image)
{
    try
    {
        string strFileName = ConfigurationManager.AppSettings["UploadDrectory"].ToString() + filename;

        FileStream fileStream = null;
        using (fileStream = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            const int bufferLen = 1024;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = image.Read(buffer, 0, bufferLen)) > 0)
            {
                fileStream.Write(buffer, 0, count);
            }
            fileStream.Close();
            image.Close();
        }

        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

web.config

  <system.serviceModel>
    <services>
      <service name="Service" behaviorConfiguration="ServiceBehavior">
        <endpoint address="http://localhost/WCFService1" behaviorConfiguration="web"
                  binding="webHttpBinding" bindingConfiguration="webBinding"
                  contract="IService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webBinding"
            transferMode="Streamed"
            maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
            openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00" 
            receiveTimeout="00:25:00" >
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<httpRuntime maxRequestLength="2097151"/>

服务托管在 IIS 中托管

客户端代码(控制台应用程序)

private static void UploadImage()
{
    string filePath = @"F:\Sharath\TestImage\TextFiles\SampleText2.txt";
    string filename = Path.GetFileName(filePath);

    string url = "http://localhost/WCFService1/Service.svc/";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "UploadImage/" + filename);

    request.Accept = "text/xml";
    request.Method = "POST";
    request.ContentType = "txt/plain";

    FileStream fst = File.Open(filePath, FileMode.Open);
    long imgLn = fst.Length;
    fst.Close();
    request.ContentLength = imgLn;

    using (Stream fileStream = File.OpenRead(filePath))
    using (Stream requestStream = request.GetRequestStream())
    {
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int byteCount = 0;
            while ((byteCount = fileStream.Read(buffer, 0, bufferSize)) > 0)
            {
                requestStream.Write(buffer, 0, byteCount);
            }
    }

    string result;

    using (WebResponse response = request.GetResponse())
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
            result = reader.ReadToEnd();
    }

    Console.WriteLine(result);
}

有了这么多代码,我可以上传 64KB 的文件,但是当我尝试上传大小超过 64KB 的文件时,我收到了类似的错误,

The remote server returned an error: (400) Bad Request


我按照你说的做了,但我仍然遇到同样的问题,这就是我的配置现在的样子,你能告诉我这里还缺少什么

<services>
  <service name="Service" behaviorConfiguration="ServiceBehavior">
    <endpoint address="http://localhost/WCFService1" behaviorConfiguration="web"
              binding="webHttpBinding" bindingConfiguration="webBinding"
              contract="IService">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding transferMode="Streamed"
             maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
             openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00" receiveTimeout="00:25:00"
             name="webBinding">
      <readerQuotas maxDepth="64" 
                    maxStringContentLength="2147483647" 
                    maxArrayLength="2147483647" 
                    maxBytesPerRead="2147483647" 
                    maxNameTableCharCount="2147483647"/>
    </binding>
  </webHttpBinding>
</bindings>

【问题讨论】:

  • 你的类的名称和命名空间是什么?
  • 任何带有完整源代码示例的最终解决方案?

标签: wcf


【解决方案1】:

wcf 的大数据传输问题:

我在 IIS 7、Windows 2008 服务器上托管了 wcf 4.0 服务。当我用小数据调用我的服务时说 4K 或 5K 字节然后请求很容易处理但是在尝试上传大尺寸时它给了我以下错误

  1. 错误请求 400
  2. 在 IIS 7 日志中看到的文件未找到 404 13
  3. 没有监听服务“myservice url”的端点

在所有情况下,我都能够使用我的客户端应用程序将小数据请求传输到服务器,但对于大尺寸消息。请求失败。

我已经尝试了所有可用的方法来解决这个问题,但对我没有任何效果。

在摸索了一个星期并阅读了所有文章和博客之后,我终于想到了

<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
        <binding name="myBinding" closeTimeout="00:10:00" maxBufferPoolSize="250000000" maxReceivedMessageSize="250000000" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" messageEncoding="Text">
        <readerQuotas maxDepth="4500000" maxStringContentLength="4500000" maxBytesPerRead="40960000" maxNameTableCharCount="250000000" maxArrayLength="4500000"/>
            </basicHttpBinding>
        </bindings>
    </system.serviceModel>
</configuration>



<system.web>

    <httpRuntime executionTimeout="4800" maxRequestLength="500000000"/>
</system.web>

<system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="500000000"></requestLimits>
            </requestFiltering>
        </security>
</system.webServer>

<!-- other useful setting -->

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

所以我认为这可能对某人有所帮助....

【讨论】:

  • 只剩下一条信息:服务名应该是.svc文件的“YourNamespace.Classname”的形式。感谢您发布这些信息。
  • 不知道如何感谢你..我也有同样的问题..现在它就像一个魅力
  • system.web 和 system.webserver 设置是否也适用于 Windows 窗体应用程序,有人吗?我也在进行大文件传输,但使用的是 Windows 窗体应用程序
  • 任何带有完整源代码示例的最终解决方案?
  • 感谢您的回答。但也许只是为了澄清一下需要增加哪些参数。对我们来说,我们只需要提高 maxStringContentLength 参数即可。
【解决方案2】:

根据您提到的大小,由于绑定级别(即 readerQuota 值)的限制命中,这似乎是错误的。您可以通过捕获 WCF 跟踪来确认拒绝服务器是否是由于超出绑定级别的限制。我们看不到您发布的配置,因此我们根据可见信息提供了最佳猜测。

我会在详细级别捕获 WCF 跟踪以解决问题。

顺便说一句,您是否尝试过增加 maxRequestLength? http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength.aspx

HTH, 阿米特·巴蒂亚

【讨论】:

    【解决方案3】:

    WaseemM 示例中解决了 404 大小的消息对我来说太大的问题:

    <system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="500000000"></requestLimits>
            </requestFiltering>
        </security>
    

    【讨论】:

      【解决方案4】:

      看...我们所有面临 64KB 上传问题的人都错过了最基本的一点。我们正在为 maxAllowedContentLength、maxReceivedMessageSize 设置较高的值...等等等等,但仍然没有任何效果:)。我们都错过了最基本的一点(......至少我是)无论我们设置什么绑定配置,我们是否在任何地方提到我们的 ENDPOINT 应该遵循 BINDING CONFIG??。 ..no where...所以基本上我们必须让 ENDPOINT 知道它必须遵循我们的 BINDING CONFIG

      <endpoint address="" 
       binding="webHttpBinding" 
       behaviorConfiguration="Your behaviour config name"
       bindingConfiguration="YOUR BINDING CONFIG NAME"
       contract="Your contract service"  />
      

      【讨论】:

        【解决方案5】:

        如果传输大数据是您的任务,您应该使用 MTOM。只需搜索“MTOM WCF”即可。

        【讨论】:

        • 任何带有完整源代码示例的最终解决方案?使用 MTOM 和大文件时出现错误
        【解决方案6】:

        Chandrachur 是正确的,无论您在&lt;binding/&gt;&lt;readerQuotas/&gt; 中指定什么,都需要在&lt;endpoint/&gt; 中添加"bindingConfiguration="my binding config name"。否则即使您的绑定配置正确,它也不会起作用。您需要确保您的配置应用于您的端点。为此,您需要正确设置"bindingConfiguration"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-05
          • 1970-01-01
          • 2016-06-06
          • 2011-07-26
          • 1970-01-01
          相关资源
          最近更新 更多