无法序列化System.Collections.Generic.IEnumerable接口 [英] Cannot serialize interface System.Collections.Generic.IEnumerable

查看:407
本文介绍了无法序列化System.Collections.Generic.IEnumerable接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的每个身体都有我以前从未遇到过的奇怪问题,我在网络上搜索了更多但又毫无意义的
这是我的错误:
无法序列化接口System.Collections.Generic.IEnumerable`1 [[Employee,App_Code.6ohe-rkb,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null]].

我的网络服务代码是

hi every body i have astrange problem i never faced before i have searched more on web but nosense
this is my error :
Cannot serialize interface System.Collections.Generic.IEnumerable`1[[Employee, App_Code.6ohe-rkb, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].

my web services code is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Data;
using System.Data.SqlClient;
using System.Text;

/// <summary>
/// Summary description for EmployeeService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class EmployeeService : System.Web.Services.WebService {

    List<Employee> Employees = new List<Employee>();


    public EmployeeService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public DataSourceResult Read()
    {
        SqlDataReader reader = null;
        using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand("Select * from Employee", con))
            {
                try
                {
                    con.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Employee e = new Employee();
                        e.Id = (int)reader["Id"];
                        e.FirstName = (string)reader["FirstName"];
                        e.LastName = (string)reader["LastName"];
                        e.Title = (string)reader["Title"];
                        e.Salary = (decimal)reader["Salary"];
                        Employees.Add(e);


                    }
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }
            }
        }

        return new DataSourceResult
        {
            Total = Employees.Count,          //number of records
            Data = Employees                  //the data
        };
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public IEnumerable<Employee> Create(IEnumerable<Employee> newEmployees)
    {
        string stmt = string.Empty;
        StringBuilder ids = new StringBuilder();
        SqlDataReader reader = null;
        List<Employee> retrievedEmployees = new List<Employee>();
        using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand("InsertEmployee", con))
            {
                con.Open();
                foreach (Employee e in newEmployees)
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FirstName", e.FirstName);
                    cmd.Parameters.AddWithValue("@LastName", e.LastName);
                    cmd.Parameters.AddWithValue("@Title", e.Title);
                    cmd.Parameters.AddWithValue("@Salary", Convert.ToDecimal(e.Salary));
                    SqlParameter retValue = cmd.Parameters.Add("@NewId", SqlDbType.Int);
                    retValue.Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    ids.Append(Convert.ToInt16(retValue.Value)).Append(",");
                }
                //remove the last comma
                ids.Remove(ids.Length - 1, 1);
            }

            using (SqlCommand cmd = new SqlCommand(
                "Select * from Employee where id in (" + ids + ")", con))
            {
                try
                {
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Employee e = new Employee();
                        e.Id = (int)reader["Id"];
                        e.FirstName = (string)reader["FirstName"];
                        e.LastName = (string)reader["LastName"];
                        e.Title = (string)reader["Title"];
                        e.Salary = (decimal)reader["Salary"];
                        retrievedEmployees.Add(e);
                    }
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }
            }
        }
        return retrievedEmployees;
    }

    [WebMethod]
    public void Update(IEnumerable<Employee> editEmployees)
    {
        string stmt = string.Empty;
        using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand(stmt, con))
            {
                foreach (Employee e in editEmployees)
                {
                    stmt = "update Employee set FirstName = '" + e.FirstName +
                        "', LastName = '" + e.LastName +
                           "', Title = '" + e.Title + "', Salary = " +
                           e.Salary + "where Id = " + e.Id;
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }

    [WebMethod]
    public void Destroy(IEnumerable<Employee> deleteEmployees)
    {
        StringBuilder ids = new StringBuilder();
        foreach (Employee e in deleteEmployees)
        {
            ids.Append(e.Id).Append(",");
        }
        //remove the last comma
        ids.Remove(ids.Length - 1, 1);

        string stmt = "delete from Employee where id in (" + ids + ")";
        using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\alaa\Desktop\GridTest\App_Data\Employees.mdf;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand(stmt, con))
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }
    
}



这是我的DataSourceResult



and this is my DataSourceResult

using System.Collections.Generic;

/// <summary>
/// Describes the result of Kendo DataSource read operation. 
/// </summary>
public class DataSourceResult
{
    /// <summary>
    /// Represents a single page of processed data.
    /// </summary>
    public List<Employee> Data { get; set; }

    /// <summary>
    /// The total number of records available.
    /// </summary>
    public int Total { get; set; }
}



错误在哪里,我该如何解决该问题?



where is the error and how can i solve the problem ?

推荐答案

1.您的问题是由于您尝试使用IEnumerable<employee> </employee>作为参数而产生的,还可以作为返回类型输入到您的Web方法中.默认情况下,只能使用系统基本类型和这些基本类型的数组(例如int,float,double,string).

2.如果要使用自己的类作为参数,则必须使用XML定义将它们定义为复杂类型.这是与此有关的 MSDN链接. [
1.Your problem is generated by the fact that you are trying to use IEnumerable<employee> </employee>as parameter and also as return type into your Web Methods. By default only the system primitive types and arrays of these primitive types (like int, float, double, string) can be used.

2.If you want to use your own classes as parameters you have to define them as complex types by using XML definition. Here is a MSDN link about this.[^]

3.I strongly advice you, if is posible, not to use Web Service anymore, but to use WCF in place of them (Windows Communication Foundation), because WCF is a new technology and is more configurable and easy to use for all type of communications between applications.


这篇关于无法序列化System.Collections.Generic.IEnumerable接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆