【问题标题】:Error occurred while receiving HTTP response from WCF service从 WCF 服务接收 HTTP 响应时出错
【发布时间】:2014-06-03 10:16:43
【问题描述】:

我有一个 WCF 服务,它返回一个 DataSet 并且它工作得很好。我实际上想返回一个 DataTable 而不是 DataSet。当我更改代码以返回 DataTable 并启动服务(自托管在控制台应用程序上)时,它运行并且我可以看到服务的 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;
            }

        }
    }

【问题讨论】:

标签: .net wcf entity-framework datatable dataset


【解决方案1】:

嗯,我当然有这个问题,但我不确定你的例外到底是什么意思。

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

您可以通过在 web.config 文件中复制以下代码来添加跟踪侦听器。

<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:\ 驱动器中添加“log”文件夹。

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

抛出异常后移动到 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 装饰您的模型类。

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

希望这能解决您的问题。 但是,如果您可以使用模型类和其他详细信息添加您的问题,那就更好了。

祝你好运!!!

【讨论】:

  • 感谢您的回答。我一定会试试的。
猜你喜欢
  • 2018-03-22
  • 1970-01-01
  • 2013-02-26
  • 2023-01-30
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多