2012-12-24 14 views
2

我正在使用WCF上傳數據庫(C#)中的文件,並且出現此錯誤。 遠程服務器返回的意外響應(413)請求實體太大。如何使用WCF服務保存文件

代碼在IService.cs

[OperationContract] 
    void UploadFile(RemoteFileInfo request); 

[MessageContract] 公共類DownloadRequest { [MessageBodyMember] 公共字符串文件名;在Service.svc.cs

public void UploadFile(RemoteFileInfo request) 
     { 
      AttachmentDTO objDTO = new AttachmentDTO(); 
      //FileStream targetStream = null; 
      Stream stream = request.FileByteStream; 
      const int bufferLen = 65000; 
      // byte[] buffer = new byte[bufferLen]; 
      // objDTO.FileData = buffer; 
      AttachmentBLL objBLL = new AttachmentBLL(); 
      try 
      { 
       byte[] readBuffer = new byte[bufferLen]; 

       int totalBytesRead = 0; 
       int bytesRead; 

       while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0) 
       { 
        totalBytesRead += bytesRead; 

        if (totalBytesRead == readBuffer.Length) 
        { 
         int nextByte = stream.ReadByte(); 
         if (nextByte != -1) 
         { 
          byte[] temp = new byte[readBuffer.Length * 2]; 
          Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length); 
          Buffer.SetByte(temp, totalBytesRead, (byte)nextByte); 
          readBuffer = temp; 
          totalBytesRead++; 
         } 
        } 
       } 

       byte[] buffer = readBuffer; 
       if (readBuffer.Length != totalBytesRead) 
       { 
        buffer = new byte[totalBytesRead]; 
        Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead); 
       } 
       objDTO.FileData = buffer; 
       objDTO.FileName = request.FileName; 
       objDTO.CreatedDate = DateTime.Now; 
       objDTO.CreatedBy = "user"; 
       objDTO.IsActive = true; 
       objDTO.FileExt = request.FileExt; 
       objBLL.AddAttachment(objDTO); 


      } 
      catch (Exception ex) 
      { 

      } 

     } 

objBLL.AddAttachment(objDTO) }

[MessageContract] 
public class RemoteFileInfo : IDisposable 
{ 
    [MessageHeader(MustUnderstand = true)] 
    public string FileName;  

    [MessageHeader(MustUnderstand = true)] 
    public int ItemID; 
    [MessageHeader(MustUnderstand = true)] 
    public string FileExt; 
    [MessageBodyMember(Order = 1)] 
    public System.IO.Stream FileByteStream; 

    public void Dispose() 
    { 
     if (FileByteStream != null) 
     { 
      FileByteStream.Close(); 
      FileByteStream = null; 
     } 
    } 

代碼;此方法位於業務邏輯文件中。 BLL可以與DAL通信,但DAL無法與WCF服務通信。

This Code is written in page.aspx.cs file. 



if (fuAttachment.HasFile) 
      { 
       string abs = fuAttachment.PostedFile.FileName; 
       System.IO.FileInfo fileInfo = new System.IO.FileInfo(fuAttachment.PostedFile.FileName); 

       MyService.RemoteFileInfo uploadRequestInfo = new MyService.RemoteFileInfo(); 

       using (System.IO.FileStream stream = new System.IO.FileStream(fuAttachment.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)) 
       { 
        uploadRequestInfo.FileName = fuAttachment.FileName; 
        uploadRequestInfo.Length = fileInfo.Length; 
        uploadRequestInfo.FileByteStream = fuAttachment.FileContent; 
        uploadRequestInfo.ItemID = itemId; 
        uploadRequestInfo.FileExt = fuAttachment.PostedFile.ContentType; 
        client.UploadFile(uploadRequestInfo.FileExt, uploadRequestInfo.FileName, uploadRequestInfo.ItemID, uploadRequestInfo.FileByteStream); 

       } 
      } 

的web.config

<binding name="WSHttpBinding_IEMRProWCFService" closeTimeout="04:01:00" 
      openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" 
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" 
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 
      <readerQuotas maxDepth="128" 
     maxStringContentLength="2147483647" maxArrayLength="2147483647" 
     maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" 
      enabled="false" /> 
      <security mode="None"> 
      <transport clientCredentialType="None" 
        proxyCredentialType="None" realm="" /> 
      <message clientCredentialType="UserName" algorithmSuite="Default" /> 
      </security> 
     </binding> 

和服務的.config

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" /> 
    <httpRuntime maxRequestLength="2147483647" /> 

    </system.web> 

    <system.serviceModel> 
    <services> 
     <service name="WCFService.EMRProWCFService"> 
     <endpoint address="" binding="wsHttpBinding" contract="WCFService.IEMRProWCFService"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 



    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="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="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    <bindings> 
     <webHttpBinding> 
     <binding name="WebConfiguration" 
       maxBufferSize="65536" 
       maxReceivedMessageSize="2147483647" 
       transferMode="Streamed"> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    <directoryBrowse enabled="true" /> 
    </system.webServer> 

</configuration> 

任何一個可以幫助我在哪裏,我犯的錯誤... 感謝

+0

我會建議使用WCF追蹤來捕捉服務器端的實際異常(或任何實際發生的地方) –

回答

1

的地方,你需要處理的是Web配置,您需要添加可設置數據大小的服務行爲。例如像這樣,

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <httpRuntime executionTimeout="4800" maxRequestLength="2097150"/> 
    <compilation debug="true"/> 
    </system.web> 
    <system.serviceModel> 
    <bindings> 
     <basicHttpBinding/> 
     <customBinding> 
     <binding name="LargeSilverlight" closeTimeout="00:21:00" openTimeout="00:20:00" receiveTimeout="00:20:00" sendTimeout="00:50:00"> 
      <textMessageEncoding maxReadPoolSize="2147483647" maxWritePoolSize="2147483647"> 
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> 
      </textMessageEncoding> 
      <httpTransport maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/> 
     </binding> 
     </customBinding> 
    </bindings> 
    <client/> 
    <!--SERVICE--> 
    <services> 
     <service name="WCFService.EMRProWCFService" behaviorConfiguration="SilverlightWCFLargeDataApplication"> 
     <endpoint address="" binding="customBinding" bindingConfiguration="LargeSilverlight" behaviorConfiguration="SilverlightWCFLargeDataApplication" contract="WCFService.IEMRProWCFService"/> 
     </service> 
    </services> 
    <!--BEHAVIOR--> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="SilverlightWCFLargeDataApplication"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="SilverlightWCFLargeDataApplication"> 
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 
    <system.webServer> 
    <security> 
     <requestFiltering> 
     <requestLimits maxAllowedContentLength="500000000"/> 
     </requestFiltering> 
    </security> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

如果這不起作用,你需要遵循一種機制來壓縮數據,並通過webservice.There發送大量的樣品,你可以找到的。

+0

Hello Sajee ..感謝您的回覆。這不起作用,但使用此配置沒有發生錯誤。如果您有任何想法,我該如何解決這個問題,請與我分享。再次感謝 – MindFresher

+0

這是什麼意思不起作用?你是否嘗試調試代碼?歡迎您 – Sajeetharan