2012-12-24 5 views
2

데이터베이스 (C#)에 파일을 업로드 할 때 WCF를 사용하고 있는데이 오류가 발생했습니다. 원격 서버가 예기치 않은 응답 (413) 요청 엔터티를 너무 큼 반환했습니다. IService.cs에서WCF 서비스를 사용하여 파일을 저장하는 방법

코드

[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 추적을 사용하여 서버 측에서 실제 예외를 catch하는 것이 좋습니다 (또는 실제로 발생하는 모든 것). –

답변

1

장소를 작업 할 필요가 웹 구성, 당신은 당신이 데이터의 크기를 설정할 수있는 서비스 행동을 추가해야합니다.

<?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

안녕하세요 Sajee .. 답장을 보내 주셔서 감사합니다. 이 작동하지 않지만이 구성을 사용하여 오류가 발생했습니다. 이 문제를 어떻게 해결할 수 있는지 알고 싶다면 나와 공유하십시오. 다시 한 번 고마워. – MindFresher

+0

이것이 작동하지 않는다는 것은 무엇을 의미합니까? 코드를 디버깅하려고 했습니까? 너는 환영 받는다. – Sajeetharan

관련 문제