return 数据时出现错误 wcf?

get error wcf when return data?

[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 报错

调试 得到 return 数据错误

根据您屏幕上的堆栈跟踪

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]
public List<Drug> GetAll_Drug()
{
    List<Drug> obj_Lst_t;
   var ctx = new EpriscriptionContext();

        ctx.Configuration.ProxyCreationEnabled = false; 
        obj_Lst_t = ctx.Drug.ToList();
}

当 return 数据 ctx 处理时, 然后 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;
    }

添加如下代码报错

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