none
Returning Null DataSet and NetTcpbinding Error RRS feed

  • Question

  • Hello,

    I hope u might help this. 

    I am facing some problem while migrating basichttpbinding to nettcpbinding.

    The problem is when i return null dataset from service to client, I am getting System.NullReferenceException: Object reference not set to an instance of an object. error. 

    Is there any scenarios difference between basicHttpbinding and netTcpbinding according to that issue?

    Thanks and regards. 


    Nilar

    Wednesday, January 23, 2013 6:28 AM

All replies

  • Hi,

    The Problem is with Dataset Code, not with Bindings.


    PS.Shakeer Hussain


    Wednesday, January 23, 2013 6:43 AM
  • Hi thanks Syed, 

    What do u mean by "DataSet Code". Can u explain me more detail?

    Anyhow it is obvious for me while i was using basichttpbinding i didn't get that error. 

    Thanks.


    Nilar


    • Edited by Nilar Ko Wednesday, January 23, 2013 6:45 AM
    Wednesday, January 23, 2013 6:44 AM
  • Means your c# code where you found error.

    PS.Shakeer Hussain

    Wednesday, January 23, 2013 6:45 AM
  • Hi,
    why you need to return DataSet from WCF service?
    Does DatasSet supposed to be present in all possible clients? Absolutely no. I reccomend not to return DataSet from WCF services.
    Please follow the link .

    Hope this helps.


    One good question is equivalent to ten best answers.


    • Edited by Shyam Kr Wednesday, January 23, 2013 6:47 AM
    Wednesday, January 23, 2013 6:45 AM
  • Hi,
    obviously you want to send a set Data to some client, then wht don;t you use XML? Write a custom method that extracts information from your DataSet to create a XML format string object and return that string object across WCF services. And one importand thing, while creating XML format string use StringBuilder, that will improve your performance.
    Hope this helps.

    One good question is equivalent to ten best answers.

    Wednesday, January 23, 2013 6:53 AM
  • Hi thanks Shyam, 

    Anyway I will consider not to use DataSet while returning from WCF Service. Can u give me some useful link for detail about disadvantages of returning DataSet. 

    But for the time being, i need to solve my problem first. 

    Thanks Syed, 

    Here is my coding from service side,                                         

    Public Function GetExecSQLDataSet(ByRef refparam As ByRefParam, ByVal cmdparam As CommandParam, ByVal strSQL As String, ByVal MaxRetrieveMsgFlg As Boolean) As DataSet Implements IService.GetExecSQLDataSet
            Dim datDt As DataTable = Nothing
            Dim dbutil As New DBUtil()
            Dim DatDs As New DataSet

            StringLengthCheckPattern = cmdparam.StringLengthCheckPattern

            Try
          If MaxRetrieveMsgFlg = True Then

                    Dim nMaxRetRow As Integer = cmdparam.MaxRetrieveCount
                    If nMaxRetRow = 999999 Then
                        nMaxRetRow = 0
                    End If

                    Dim intCount As Integer = dbutil.Get_DataCount(strSQL)
                    refparam.RetrieveCount = intCount

                    If nMaxRetRow <> 0 AndAlso refparam.MaxRetrieveCountOverFlg(0) = False AndAlso intCount > 0 AndAlso nMaxRetRow < intCount Then
                        refparam.MaxRetrieveCountOverFlg = New Boolean() {True}
                        Return DatDs
                    End If
                End If

                'Fill(テーブル名はGetExecSQLDataTableで固定にしています)
                dbutil.FillDB("GetExecSQLDataTable", strSQL, DatDs)
                datDt = DatDs.Tables("GetExecSQLDataTable")

     DatDs.RemotingFormat = SerializationFormat.Binary
               refparam.ExecSQLString = strSQL
                refparam.ExecSQLArgument = Nothing

                Return DatDs
            Catch ex As Exception
                refparam.RetErrorMsg = ex.Message
                refparam.RetErrorKbn = 2

                Dim dtErr As New DataTable
                DatDs.Tables.Add(dtErr)
                DatDs.RemotingFormat = SerializationFormat.Binary
                Return DatDs
            End Try
        End Function

    This is my client side coding

    webservice.open()

     Dim retDs As DataSet = webservice.GetExecSQLDataSet(clsPublicInstance.Instance.RefParam, cmdparam, strSQL, MaxRetrieveMsgFlg)

    webservice.close()

    I hope u understand this, 

    Thanks again.


    Nilar

    Wednesday, January 23, 2013 6:56 AM
  • Hi,

    To avoid Error you have to check dataSet is Null or not?

    example

    DataSet ds=new DataSet();
    
    da.Fill(ds);
    
    if(ds!=Null)
    
    {
    
       if(ds.Tables.Count>0)
    
       {
    
            if(ds.Tables.Rows.Count>0)
    
           {
    
                    /// Here Wirte your Code
    
           }
    
       }
    
    }
    


    PS.Shakeer Hussain

    Wednesday, January 23, 2013 6:57 AM
  • Hi Nilar,

    Sample code in VB.Net

    Dim ds As New DataSet()
    
    da.Fill(ds)
    
    If ds <> Null Then
    
    
      If ds.Tables.Count > 0 Then
    
    
         If ds.Tables.Rows.Count > 0 Then
    
          ''' Here Wirte your Code
    
         End If
    
      End If
    End If
    


    PS.Shakeer Hussain

    Wednesday, January 23, 2013 7:15 AM
  • Hi,
    the link i provided above in my post is sufficient to say that we should not return DataSet from WCF services.
    Anyway, i did'nt find where you are returning Null DataSet from service?Where exactly error is occuring, at service side or at client side?

    This link may help :
    http://www.codeproject.com/Questions/441545/How-to-return-dataset-in-wcf


    One good question is equivalent to ten best answers.

    Wednesday, January 23, 2013 7:47 AM
  • Hi thanks Shyam, 

    The error is occurring from client side, when service return dataset. 

    Anyway, i did'nt find where you are returning Null DataSet from service?

    For clearance, I will provide one more screen shot which has some data in dataset which has no error. Can u please compare above screen shot and current screen shot. 


    And thanks for Syed, 

    I still don't know where i need to apply your code, client side or server side according to my coding. 

    And anyway i need to return that null dataset to client. 

    Thanks for your supporting, 


    Nilar

    Wednesday, January 23, 2013 7:58 AM
  • So the above screen shot is from that client that used basicHttpbinding.
    and exactly same code causes error when netTcpBinding is used.
    If i am not wrong is this code causing error :

     Dim retDs As DataSet = webservice.GetExecSQLDataSet(clsPublicInstance.Instance.RefParam, cmdparam, strSQL, MaxRetrieveMsgFlg)


    One good question is equivalent to ten best answers.

    Wednesday, January 23, 2013 8:15 AM
  • Hi Shyam, 

    I am so sorry for leading for misunderstanding reply.

    I just wanted to explain that i don't get any error when i return data as screen shot 2. 

    But it cause me an error when i return nothing as screen shot 1. 

    And I have those parameters (clsPublicInstance.Instance.RefParam, cmdparam, strSQL, MaxRetrieveMsgFlg) and there is a value when client send to service side, but when an error occurs those parameter values are lost when returning from service side. 

    Both are testing same NetTcpbinding. In Httpbinding there is no problem at all.

    Thanks for your support. 


    Nilar


    • Edited by Nilar Ko Wednesday, January 23, 2013 8:24 AM
    Wednesday, January 23, 2013 8:22 AM
  • Hi,

    can you provide service config and client config both when using basicHttpBinding and netTcpBinding  respectively.


    One good question is equivalent to ten best answers.

    Wednesday, January 23, 2013 1:15 PM
  • Sure, 

    NetTcpBinding (Service config)        

    <bindings>
          <netTcpBinding>
            <binding name="NetTcpBinding" closeTimeout="00:05:00" openTimeout="00:05:00"
              receiveTimeout="00:10:00" sendTimeout="00:05:00" transactionFlow="false"
              transferMode="Streamed" listenBacklog="10" maxBufferPoolSize="10485760"
              maxBufferSize="10485760" maxReceivedMessageSize="10485760" portSharingEnabled="true">
              <readerQuotas maxDepth="32" maxStringContentLength="10485760"
                maxArrayLength="10485760" maxBytesPerRead="1048576" maxNameTableCharCount="16384" />
              <reliableSession ordered="false" enabled="false" />
              <security mode="None">
                <transport clientCredentialType="None" protectionLevel="EncryptAndSign" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
            </binding>
          </netTcpBinding>
          <mexTcpBinding>
            <binding name="MexTcpBinding" />
          </mexTcpBinding>
        </bindings>

        <services>
          <service behaviorConfiguration="WS.ServiceBehavior" name="WS.Service">
            <endpoint address="" behaviorConfiguration="" binding="netTcpBinding"
              bindingConfiguration="NetTcpBinding" name="NetTcpBinding" contract="WS.IService"
              kind="" endpointConfiguration="" />
            <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="MexTcpBinding"
              name="MexTcpBinding" contract="IMetadataExchange" kind="mexEndpoint" endpointConfiguration="" />
            <host>
              <baseAddresses>
                <add baseAddress="net.tcp://myipaddress/WS.Service" />
              </baseAddresses>
              <timeouts closeTimeout="00:00:10" openTimeout="00:01:00" />
            </host>
          </service>
        </services>

        <behaviors>
          <serviceBehaviors>
            <behavior name="WS.ServiceBehavior">
              <serviceMetadata httpGetEnabled="false " />
              <serviceDebug includeExceptionDetailInFaults="true" />
              <dataContractSerializer maxItemsInObjectGraph="10485760" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>

    NetTcpBinding (Client config)

    <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="NetTcpBinding" closeTimeout="00:05:00" openTimeout="00:05:00"
              receiveTimeout="00:10:00" sendTimeout="00:05:00" transactionFlow="false"
              transferMode="Buffered" transactionProtocol="OleTransactions"
              hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="10485760"
              maxConnections="10" maxReceivedMessageSize="10485760">
              <reliableSession inactivityTimeout="00:10:00" enabled="false" />
              <security mode="None">
                <transport clientCredentialType="None" protectionLevel="EncryptAndSign" />
                <message clientCredentialType="UserName" />
              </security>
            </binding>
          </netTcpBinding>
        </bindings>
        <client>
          <endpoint address="net.tcp://myipaddress/WSCopy/Service.svc" binding="netTcpBinding"
            bindingConfiguration="NetTcpBinding" contract="ServiceReference1.IService"
            name="NetTcpBinding" />
        </client>
      </system.serviceModel>

    BasicHttpBinding (Service config)

    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="LargeMessageBinding" maxReceivedMessageSize="10485760" maxBufferSize="10485760" maxBufferPoolSize="10485760">
              <readerQuotas maxDepth="32" maxStringContentLength="10485760" maxArrayLength="10485760" maxBytesPerRead="1048576" maxNameTableCharCount="16384"/>
            </binding>
          </basicHttpBinding>
        </bindings>
        <services>
          <service name="WS.Service" behaviorConfiguration="WS.ServiceBehavior">
            <!-- Service Endpoints -->
            <endpoint address="" binding="basicHttpBinding" contract="WS.IService" bindingConfiguration="LargeMessageBinding">
                      <identity>
                <dns value="localhost"/>
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="WS.ServiceBehavior">
                       <serviceMetadata httpGetEnabled="true"/>
                     <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>

        </behaviors>
      </system.serviceModel>

    Basichttpbinding (client config)

     <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IService" closeTimeout="00:05:00"
              openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00"
              allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
              maxBufferSize="104857600" maxBufferPoolSize="104857600" maxReceivedMessageSize="104857600"
              messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
              useDefaultWebProxy="true">
              <readerQuotas maxDepth="32" maxStringContentLength="10485760"
                maxArrayLength="10485760" maxBytesPerRead="10485760" maxNameTableCharCount="163840" />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None"
                  realm="" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
            </binding>
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://localhost:1180/Service.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
            name="BasicHttpBinding_IService" />
        </client>
      </system.serviceModel>


    Nilar

    Wednesday, January 23, 2013 2:15 PM
  • Hi Shyam, 

    I am so sorry for leading for misunderstanding reply.

    I just wanted to explain that i don't get any error when i return data as screen shot 2. 

    But it cause me an error when i return nothing as screen shot 1. 


    Nilar


    Are you getting "No corresponding start element is open." exception ?

    One good question is equivalent to ten best answers.

    Wednesday, January 23, 2013 2:50 PM
  • Hi,

    I am not getting that error. 

    I am getting NullException error, because my service doesn't return parameters. 

    Thanks. 


    Nilar

    Wednesday, January 23, 2013 2:52 PM
  • Ok,
    one thing i see  that in Catch block you are doing some more logical tasks :

    Catch ex As Exception
                 refparam.RetErrorMsg = ex.Message
                 refparam.RetErrorKbn = 2
     
                Dim dtErr As New DataTable
                 DatDs.Tables.Add(dtErr)
                 DatDs.RemotingFormat = SerializationFormat.Binary
                 Return DatDs
             End Try

    Try Replace :

    Dim dtErr As New DataTable
                 DatDs.Tables.Add(dtErr)
                 DatDs.RemotingFormat = SerializationFormat.Binary

    By below code :

    Dim dtErr As New DataTable
    Dim  DatDs1 As DataSet = New DataSet()
    DatDs1.Tables.Add(dtErr)
    DatDs1.RemotingFormat = SerializationFormat.Binary

    Insteed of using the same DataSet create a New one and Use that.


    One good question is equivalent to ten best answers.


    • Edited by Shyam Kr Wednesday, January 23, 2013 3:10 PM
    Wednesday, January 23, 2013 3:09 PM
  • Or you just create a New DataSet object with the same reference variable like this:

    Dim dtErr As New DataTable
    DatDs = New DataSet()
                 DatDs.Tables.Add(dtErr)
                 DatDs.RemotingFormat = SerializationFormat.Binary


    One good question is equivalent to ten best answers.



    • Edited by Shyam Kr Wednesday, January 23, 2013 3:13 PM
    Wednesday, January 23, 2013 3:12 PM
  • If you are returning DataSet as Nothing when exception occurs then in client you will get 'No Corresponding Start Element Open' exception.

    http://instantdevelopment.blogspot.com/2008/03/wcf-client-says-no-corresponding-start.html

    I am able to reproduce the same situation here.
    I think this exception must have raised on your side also, but may have been supressed by another one that you said "NullException" Can you show your Exception stack trace?


    One good question is equivalent to ten best answers.

    Wednesday, January 23, 2013 3:21 PM
  • Hi Shyam,

    It doesn't hit my catch block, it returns as it is dataset. 

    But when returning my parameters (refParam) values are lost. 

    Then in client side in some code generation above parameter get an error. 

    Thanks.


    Nilar


    • Edited by Nilar Ko Wednesday, January 23, 2013 3:24 PM
    Wednesday, January 23, 2013 3:21 PM
  • Hi,

    but from my side that ref parameter's value is not lost. Ok can you create a separate project with client and service showing same error with demo data and send it to me so that i get exactly what you are getting.


    One good question is equivalent to ten best answers.

    Wednesday, January 23, 2013 3:39 PM
  • Hi,
    you can take help of wcf trace to get inner insight of what may have been happening.
    Modify your web.config under configuration with this and add servicetrace.svclog file in c:\logs folder :

    <system.diagnostics >
            <sharedListeners>
                <add name="sharedListener" 
                     type="System.Diagnostics.XmlWriterTraceListener"
                     initializeData="c:\logs\servicetrace.svclog" />
            </sharedListeners>
            <sources>
                <source name="System.ServiceModel" switchValue="Verbose, ActivityTracing" >
                    <listeners>
                        <add name="sharedListener" />
                    </listeners>
                </source>
                <source name="System.ServiceModel.MessageLogging" switchValue="Verbose">
                    <listeners>
                        <add name="sharedListener" />
                    </listeners>
                </source>
            </sources>
        </system.diagnostics>

    After one request-responce cycle your log file is ready to analyze.Go through it. That may help.

    One good question is equivalent to ten best answers.

    Thursday, January 24, 2013 8:42 AM
  • Hi Shyam, 

    I have sent a sample application that produce an error. 

    I hope you understand that. 

    Thanks 


    Nilar

    Thursday, January 24, 2013 3:56 PM