Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF服务(413)中的文件上载请求实体太大_C#_Asp.net_Wcf - Fatal编程技术网

C# WCF服务(413)中的文件上载请求实体太大

C# WCF服务(413)中的文件上载请求实体太大,c#,asp.net,wcf,C#,Asp.net,Wcf,我试图在asp.NETC中实现文件上传到我的WCF服务# 以下是WCF服务器中用于文件上载的代码 public void FileUpload(string fileName, Stream fileStream) { FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create); // byte[] bytearray

我试图在asp.NETC中实现文件上传到我的WCF服务#

以下是WCF服务器中用于文件上载的代码

 public void FileUpload(string fileName, Stream fileStream)
       {
           FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);

        //   byte[] bytearray = new byte[10000];
           byte[] bytearray = new byte[1000];
           int bytesRead, totalBytesRead = 0;
           do
           {
               bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
               totalBytesRead += bytesRead;
               if(bytesRead > 0)
               fileToupload.Write(bytearray, 0, bytearray.Length);

           } while (bytesRead > 0);

         //  fileToupload.Write(bytearray, 0, bytearray.Length);
           fileToupload.Close();
           fileToupload.Dispose();



       }
以下是客户端上载文件的代码:(固定字节数组大小)

但在阅读回复时,我得到了一个例外 远程服务器返回错误:(413)请求实体太大。

在请求流中多次写入是否正确

我使用了一个大小为22.4KB的文件,它是使用第一个代码(固定大小数组)成功上传的 如果我将文件大小拆分为1024字节的倍数并尝试发送,则会出现问题。

Web.config文件

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfServiceApp.Service1" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:50327/Service1.svc"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <!--<protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>-->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/>
      </customHeaders>
    </httpProtocol>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>


在服务绑定中,增加读卡器配额。请在此处阅读有关每个设置的更多信息。当您尝试传输具有某种结构的大型序列化对象时,请查看此问题,此问题通常是由配置中的
maxItemsInObjectGraph
泄漏引起的。看我的

但在您的情况下您尝试将简单数据文件作为字节流传输。要通过
webHttpBinding
实现这一点,您应该指定适当的服务契约,它只接受流消息作为输入。所有额外的东西,比如您可以在消息契约中指定为头的文件名(实际上,也许您使用filename作为URI的参数也可以)。然后必须为绑定设置
TransferMode=TransferMode.Streamed
。再来一个


额外谷歌搜索的关键词是webhttpbinding流媒体

,谢谢你的评论。我使用了一个大小为22.4KB的文件,它是使用第一个代码(固定大小数组)成功上载的。如果我将文件大小拆分为1024字节的倍数,并尝试发送,则会出现问题。您似乎没有将数据分块发送到服务-您正在分块构建流,但您将其作为一个请求发送。您可以发布服务的绑定配置(最好是配置的整个
部分)。您可能没有创建特定的绑定,或者没有将所述绑定分配给显式端点。@Tim我正在使用while循环和requestStream.Write(缓冲区,0,字节数)发送数据块;我在主要问题中编辑/更新了web.config文件。感谢您的评论我认为问题在于发送数据块,1024字节的倍数。我已经更新了这个问题。我添加了webhttpbinding,它成功了。谢谢你的链接
        request.Method = "POST";
        request.ContentType = "text/plain";
       // Stream serverStream = request.GetRequestStream();

        if (FileUpload1.HasFile)
        {
            fileName = FileUpload1.FileName;
            stream = FileUpload1.FileContent;
            stream.Seek(0, SeekOrigin.Begin);
            bytearray = new byte[1024];//stream.Length];


        }
        int TbyteCount = 0;
        Stream requestStream = request.GetRequestStream();

            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int byteCount = 0;
            while ((byteCount = stream.Read(buffer, 0, bufferSize)) > 0)
            {
                TbyteCount = TbyteCount + byteCount; 
                requestStream.Write(buffer, 0, byteCount);
            }

            requestStream.Close();


        try
        {
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                int statusCode = (int)response.StatusCode;
                System.Diagnostics.Debug.WriteLine("statusCode: " + statusCode);

                StreamReader reader = new StreamReader(response.GetResponseStream());
                System.Diagnostics.Debug.WriteLine("reader: " + reader.ToString());

            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("--- EXCEPTION ---");
            ex.ToString();
        }
    }
<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfServiceApp.Service1" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:50327/Service1.svc"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <!--<protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>-->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/>
      </customHeaders>
    </httpProtocol>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>