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

时间:2011-06-11 18:39:16

标签: wcf

我正在尝试将大文件上传到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>

6 个答案:

答案 0 :(得分:23)

wcf的大数据传输问题:

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

  1. 错误请求400
  2. 在IIS 7日志中找不到404 13文件
  3. 听取服务“myservice url”
  4. 没有终点

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

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

    在我抓了一个星期并阅读所有文章和博客后,我想到了

    <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" />
    

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

答案 1 :(得分:2)

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

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

是的,你试过增加maxRequestLength吗? http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength.aspx

HTH, 阿米特巴蒂亚

答案 2 :(得分:2)

修复404大小的邮件对我来说太大的问题是WaseemM的一个例子:

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

答案 3 :(得分:1)

请参阅......我们所有面临 64KB 上传问题的人都错过了最基本的观点。我们为maxAllowedContentLength,maxReceivedMessageSize设置了高值... blah blah但仍然无效:)。我们都错过了最基本的观点(......至少我是这样)无论我们设置什么样的绑定配置,我们是否提到我们的 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"  />

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

相关问题