从WCF服务接收HTTP响应时发生错误

时间:2014-04-18 20:42:23

标签: .net wcf entity-framework datatable dataset

我有 WCF 服务,它返回 DataSet ,它运行正常。我实际上想要返回 DataTable 而不是 DataSet 。当我更改代码以返回 DataTable 并启动服务(在控制台应用程序上自托管)时,它运行了,我可以看到servive的WSDL文档。但是,在使用该服务时,我收到了一个错误:

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.

现在,我在某处读到其他一些开发人员在返回 DataTable 方面存在类似问题,该问题在返回 DataSet 时得到解决。我做了同样的事情并且能够解决问题。

我的问题:

但是,它确实引起了我为什么在返回DataTable时出现这样的问题? 是否有一些与我未能看到的基础相关的重要内容?

WCF服务( with DataTable )具有以下代码:

 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;
            }

        }
    }

以下是代码( 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;
            }

        }
    }

1 个答案:

答案 0 :(得分:1)

我确实遇到过这个问题,但我不确定你的例外是什么。

因此,我建议您在WCF服务中添加跟踪侦听器,以获取有关异常的详细信息。

您可以通过复制web.config文件中的以下代码来添加tracelistener。

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

注意:请在c:\ drive中添加“log”文件夹。

现在,当您完成此操作后,您当然可以跟踪您的服务。现在运行你的程序并抛出let异常。

抛出异常后移到C:\ log \ Traces并打开它。使用Microsoft Service Trace Viewer打开它。

现在在下面的链接中找到以下异常。

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

现在,如果我假设你得到了

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

然后我会建议您关闭代理。

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

然后再次运行你的程序。

2.如果例外情况属于以下类型

{"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}

那是与动态代理无关的东西,然后我建议你用KnownTypeAttribute来装饰你的Model类。

[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;}
}

希望这可以解决你的问题。 但是如果你能用模型类和其他细节添加你的问题会更好。

无论如何Goodluck !!!