上传大文件的WCF问题,在IIS中托管

Sha*_*ath 7 wcf

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

我正在使用Restful和Streaming方法.

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

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

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

运营合同

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

执行合同

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

<httpRuntime maxRequestLength="2097151"/>
Run Code Online (Sandbox Code Playgroud)

服务托管在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);
}
Run Code Online (Sandbox Code Playgroud)

有了这么多代码我就可以上传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>
Run Code Online (Sandbox Code Playgroud)

小智 23

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" />
Run Code Online (Sandbox Code Playgroud)

所以我认为它可能会帮助某些人......


ami*_*mit 2

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

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

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

HTH,阿米特·巴蒂亚