WCF-problem mit dem hochladen der großen Datei, die in IIS gehostet

Ich versuche größere Dateien hochzuladen, um einen WCF-Dienst in IIS gehostet.

Ich bin mit Erholsamen und Streaming-Methode.

Aber ich bin nicht in der Lage, Dateien hochzuladen, die mehr als 64 Kb sein.

Habe ich versucht viel durch ändern der Größe der Elemente in web.config - Datei, jedoch nicht, um das Problem zu beheben.

Hier ist mein code und config, bitte lassen Sie mich wissen, wenn jemand Fragen zu finden im code und wie kann ich das beheben.

Betrieb Vertrag

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

Umsetzung Bedienung Vertrag

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>

und

<httpRuntime maxRequestLength="2097151"/>

Dienst gehostet wird, die in IIS gehostet

Client-side Code (console application)

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);
}

Mit so viel code, ich bin in der Lage, upload 64 Kb Datei, aber wenn ich versuche, eine Datei hochzuladen, der mehr als 64KB, ich bin immer Fehler wie,

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


Tat ich, was Sie erzählt hat, aber ich bin noch immer dasselbe problem, das ist, wie meine config aussieht wie jetzt, kannst du mir bitte sagen was noch fehlt hier

<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>
  • Was ist der name und namespace der Klasse?
  • eine endgültige Lösung mit full-source-code-Beispiel funktioniert es ?
InformationsquelleAutor Sharath | 2011-06-11
Schreibe einen Kommentar