【问题标题】:get error wcf when return data?返回数据时得到错误wcf?
【发布时间】:2015-07-16 03:40:57
【问题描述】:
[OperationContract]
public List<Drug> GetAll_Drug()
{
   List<Drug> obj_Lst_t;
   using (var ctx = new EpriscriptionContext())
  {
      obj_Lst_t = ctx.Drug.ToList();
  }
    return obj_Lst_t;
  }

得到答案 ------ 但是添加 OperationContract 会出错

调试 获取返回数据错误

【问题讨论】:

  • 有什么问题?它发生在哪里? ...你想让我们怎么做?

标签: c# wcf silverlight


【解决方案1】:

根据您的堆栈跟踪屏幕

System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException

所以我相信这可能是由于延迟加载或 EF 代理序列化。

尝试禁用代理。

[OperationContract]
public List<Drug> GetAll_Drug()
{
    List<Drug> obj_Lst_t;
    using (var ctx = new EpriscriptionContext())
    {
        ctx.Configuration.ProxyCreationEnabled = false; // disable proxy creation here.
        obj_Lst_t = ctx.Drug.ToList();
    }
    return obj_Lst_t;
}

【讨论】:

  • 坦克。为什么从 OperationContract GetAll_Drug 没有错误?
  • @user1039134 对不起!没有得到您所询问的内容?
【解决方案2】:

当返回 Data ctx dispose 时, 然后 obj_Lst_t 值转换为 null, 或使用:

[OperationContract]
public List<Drug> GetAll_Drug()
{
    List<Drug> obj_Lst_t;
    using (var ctx = new EpriscriptionContext())
    {
        ctx.Configuration.ProxyCreationEnabled = false; 

foreach(var data in ctx.Drug)
       { obj_Lst_t.add(data);}
    }
    return obj_Lst_t;
}

或使用:

 [OperationContract]
    public List<Drug> GetAll_Drug()
    {
        List<Drug> obj_Lst_t;
        using (var ctx = new EpriscriptionContext())
        {
            ctx.Configuration.ProxyCreationEnabled = false; 

    foreach(var data in ctx.Drug)
           { obj_Lst_t.add(new Drug{...});}
        }
        return obj_Lst_t;
    }

【讨论】:

    【解决方案3】:

    通过添加以下代码错误

    [OperationContract]
    public List<Patients> GetAll_Patients()
    {
    List<Patients> obj_Lst_t;
    using (var ctx = new EpriscriptionContext())
    {
    ctx.Configuration.ProxyCreationEnabled = false;
    obj_Lst_t = ctx.Patients.ToList();
    }
    return obj_Lst_t;
    }
    

    【讨论】:

      【解决方案4】:

      你必须删除使用,

      [OperationContract]
      public List<Drug> GetAll_Drug()
      {
          List<Drug> obj_Lst_t;
         var ctx = new EpriscriptionContext();
      
              ctx.Configuration.ProxyCreationEnabled = false; 
              obj_Lst_t = ctx.Drug.ToList();
      }
      

      【讨论】:

      • 这不是一个好的答案,在不明确处理的情况下“仅删除使用”绝对不是最佳做法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 2012-08-11
      • 2012-06-30
      • 1970-01-01
      相关资源
      最近更新 更多