Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I'm trying to convert an excel file to byte array and passing the same to WCF service method.
I'm getting the following exception. Please help me to resolve this issue.

This is the below code chunk where I'm getting error and
SaveData(***) method is the one which calls the WCF service

C#
string strFile = ViewState[GlobalConstants.strVsfilePath].ToString();
                    DataTable dtSaveData = new DataTable();
                    dtSaveData = (DataTable)ViewState[GlobalConstants.strVsOrginal];
                    DataSet dsSaveData = new DataSet();
                    dsSaveData.DataSetName = "dsfff";
                    dsSaveData.Tables.Add(dtSaveData);
                    dsSaveData.Tables[0].TableName = "dtfff";
                    string strResult = string.Empty;
                    MemoryStream stream = new MemoryStream();
                    dtSaveData.WriteXml(stream, true);
                    stream.Seek(0, SeekOrigin.Begin);
                    StreamReader sr = new StreamReader(stream);
                    string strInputXML = sr.ReadToEnd();
                    string strModifiedXml = strInputXML.Replace("+05:30", "");
                    byte[] btData = FileToByteArray(strFile);
                    File.Delete(strFile);
                    //cByteTransfer objByteTransfer = new cByteTransfer();
                    //objByteTransfer.BTransfer = btData;
                    Stream streamData = new MemoryStream(btData);

                    adminsvc.SaveData(strModifiedXml, dsSaveData.DataSetName, dsSaveData.Tables[0].TableName, strType, strLoginName, "FIXED", btData, (string)ViewState["strWorkbookName"], (string)ViewState["SheetName"]);


Exception Detail

{System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException, ChannelBinding channelBinding)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at ImportExchangeRates.AdminService.IAdminservice.SaveData(String strModifiedXml, String strDatasetName, String strDataTableName, String strModelName, String strLoginName, String strTableType, Byte[] bytWorkbook, String strWorkbookName, String strOuptuSheet)
   at ImportExchangeRates.AdminService.AdminserviceClient.SaveData(String strModifiedXml, String strDatasetName, String strDataTableName, String strModelName, String strLoginName, String strTableType, Byte[] bytWorkbook, String strWorkbookName, String strOuptuSheet)
Posted
Updated 11-Feb-14 17:41pm
v3
Comments
Sergey Alexandrovich Kryukov 11-Feb-14 11:10am    
Will you think by yourself: what's the use to show some exception stack trace without showing the code sample and pointing out where the exception was thrown?
—SA
Wamuti 11-Feb-14 11:43am    
Could you include the code you are using to stream the file also.

1 solution

I have set the maxStringContentLength="65536000" in basichttpbinding-->binding tag and it worked.

And also added the name of the binding used under basichttpbinding-->binding here into the endpoint tag as an attribute binding="basicBinding"

Please find the configuration used as below

HTML
<system.serviceModel>
    <bindings>

      <basicHttpBinding>
       
        <binding name="basicBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647"  maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="65536000"
          maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
        </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="MY_SERVICE.cMY_Module"
             behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="http://localhost:1111/MY_Service.svc" binding="basicBinding"
             contract="MY_SERVICE.IMyService" bindingConfiguration="basicHttpBinding"/>
        <endpoint contract="IMetadataExchange"
           binding="mexHttpBinding" address="mex"/>
      </service>
    </services>
  </system.serviceModel>

Thanks & Regards,
Mathi
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900