0

I have a WCF service which returns a DataSet and it works just fine. I actually wanted to return a DataTable instead of a DataSet. When I changed the code to return a DataTable and started the service(self-hosted on a console application), it ran and I could see the WSDL document for the servive. However, while consuming the service, I got an error which said:

An error occurred while receiving the HTTP response (along with the service url).
This could be due to the service endpoint binding not using the HTTP protocol. 
This could also be due to an HTTP request context being aborted by the server(possibly due to the service shutting down). 
See server logs for more details.

Now, I read somewhere that some other developer had a similar issue on returning a DataTable, which was solved on returning a DataSet. I did the same and was able to solve the issue.

My Question:

However, it does intrigue me as to why did such an issue occur on returning a DataTable? Is there something important related to the basics that I have failed to see ?

The WCF service(with DataTable) has the following code:

 public DataTable ValidateLogin(string UserName, string Password)
        {
           DataTable dt = new DataTable();
           LINQHelperDataContext valLog = new LINQHelperDataContext();
           var result = from user in valLog.GetTable<USER_DETAILS_T>()
                        join acctype in valLog.GetTable<Account_type_t>()
                        on user.UserAccountType equals acctype.AccountTypeId
                        where user.UserId == UserName
                        where user.UserPassword == Password
                        select new {user.UserFirstName,acctype.AccountTypeName };
                    //     select user.UserFirstName;

            int rowCount=result.Count();
            dt.Columns.Add();
            dt.Columns.Add();
            if (rowCount == 1)
            {

                foreach (var res in result)
                {
                    dt.Rows.Add(res.UserFirstName,res.AccountTypeName);
                    break;
                }

                return dt;
            }
            else
            {
                dt.Rows.Add("Not Found", "Not Found");

                return dt;
            }

        }
    }

Following is the code(with DataSet):

 public DataSet ValidateLogin(string UserName, string Password)
        {
           DataSet ds = new DataSet();
           DataTable dt = new DataTable();
           LINQHelperDataContext valLog = new LINQHelperDataContext();
           var result = from user in valLog.GetTable<USER_DETAILS_T>()
                        join acctype in valLog.GetTable<Account_type_t>()
                        on user.UserAccountType equals acctype.AccountTypeId
                        where user.UserId == UserName
                        where user.UserPassword == Password
                        select new {user.UserFirstName,acctype.AccountTypeName };
                    //     select user.UserFirstName;

            int rowCount=result.Count();
            dt.Columns.Add();
            dt.Columns.Add();
            if (rowCount == 1)
            {

                foreach (var res in result)
                {
                    dt.Rows.Add(res.UserFirstName,res.AccountTypeName);
                    break;
                }
                ds.Tables.Add(dt);
                return ds;
            }
            else
            {
                dt.Rows.Add("Not Found", "Not Found");
                ds.Tables.Add(dt);
                return ds;
            }

        }
    }
AnkitMittal
  • 166
  • 2
  • 18
  • Try to set name for a datatable like explained here http://stackoverflow.com/questions/12702/net-returning-datatables-in-wcf – Rand Random Apr 18 '14 at 21:51

1 Answers1

2

Well I certainly had this issue but I am not sure what your exception exactly says.

So I would suggest you to add a trace listener in your WCF service so as to get detailed information about your exception.

you can add a tracelistener by copying the below code in your web.config file.

<system.diagnostics>
<sources>
  <source name="System.ServiceModel"
  switchValue="Information, ActivityTracing"
  propagateActivity="true">
    <listeners>
      <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener"
      initializeData="c:\log\Traces.svclog" />
    </listeners>
  </source>
</sources>

Note:Please Add "log" folder in your c:\ drive.

Now when you have done this you can certainly track your service. Now run your program and the let exception be thrown.

After the exception is thrown move to C:\log\Traces and open it. Open it using Microsoft Service Trace Viewer.

Now find the Exception following at the link given image below.

https://i.stack.imgur.com/nfK9l.png

Now if I were to assume that the you got

There was an error while trying to serialize parameter http://tempuri.org/:LoginResult. The InnerException message was 'Type 'System.Data.Entity.DynamicProxies. 

Then I would suggest you to turn off your proxies.

LINQHelperDataContext valLog = new LINQHelperDataContext();
valLog.Configuration.ProxyCreationEnabled=false;

And then run your program again.

2.If the exception is of the following type

{"There was an error while trying to serialize parameter     http://tempuri.org/:myEntity. The InnerException message was 'Type 'System.String[]' with data contract name' ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected}

That is something which is not related to dynamic proxies then i would suggest you to Decorate your Model class with KnownTypeAttribute.

[DataContract]
[KnownType(typeof(string[]))] //This is because at times our Service Doesn't recognize as a valid type to send over the server and hence we add the knowntype attribute
public class ClassName
{
   [DataMember]
   string[] PropertyName{get;set;}
}

and hopefully that should solve your problem. But then it would had been better if you could have added your question with Model classes and other details.

Anyways Goodluck!!!

Nishant Jain
  • 197
  • 1
  • 9