programing

오류 - 직렬화 가능으로 표시되지 않음

iphone6s 2023. 6. 16. 21:33
반응형

오류 - 직렬화 가능으로 표시되지 않음

오류는 다음과 같습니다.

Type 'OrgPermission' in Assembly 'App_Code.ptjvczom, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 

내 코드는 다음과 같습니다.

다음 데이터 소스를 사용하는 그리드 보기가 있습니다.

 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetOrgList" 
            TypeName="Org">
    <SelectParameters>
      <asp:SessionParameter Name="orgCodes" SessionField="UserOrgs" Type="Object" />
       <asp:Parameter DefaultValue="Y" Name="active" Type="String" />
    </SelectParameters>
 </asp:ObjectDataSource>

페이지 로드에서 세션 변수를 다음과 같이 설정합니다.

User cUser = new User(userid);
//make sure the user is an Admin
List<OrgPermission> orgs = new List<OrgPermission>();
foreach(OrgPermission org in cUser.orgs)
   {
     if (org.type=='admin')
     {
        orgs.Add(org);                       
     }
   }
Session["UserOrgs"] = orgs;

내 사용자 클래스는 다음과 같습니다.

public class OrgPermission
{
    public string Org { get; set; }   
    public List<string> type { get; set; }

    public OrgPermission()
    { }    
}
public class cUser
{    
    public string userid { get; set; }
    public List<OrgPermission> orgs { get; set; }

    public clsUser(string username)
    {
      //i set everything here
    }
}

왜 고장이 났는지 이해가 안 되는데, 연재하지 않고 사용할 수 있나요?

디버깅을 시도했는데 세션 변수가 올바르게 설정되어 GetOrgList에 들어가 올바른 결과를 반환했지만 페이지가 로드되지 않고 위의 오류가 발생합니다.

다음은 GetOrgList 함수의 일부입니다.

public DataTable GetOrgList(List<OrgPermission> orgCodes, string active)
    {

        string orgList = null;

        //code to set OrgList using the parameter is here.

        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection(cCon.getConn());
        SqlCommand cmd = new SqlCommand("sp_GetOrgList", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@orgList", orgList));
        cmd.Parameters.Add(new SqlParameter("@active", active));

            conn.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter();

            sqlDA.SelectCommand = cmd;
            sqlDA.Fill(ds);

            conn.Close();
        return ds.Tables[0];
    }

다음을 추가해야 합니다.Serializable직렬화할 클래스의 속성입니다.

[Serializable]
public class OrgPermission

개체를 세션 상태로 저장하는 경우 해당 개체를 직렬화할 수 있어야 합니다.

http://www.hpenterprisesecurity.com/vulncat/en/vulncat/dotnet/asp_dotnet_bad_practices_non_serializable_object_stored_in_session.html


편집:

세션을 올바르게 직렬화하려면 응용 프로그램이 세션 특성으로 저장하는 모든 개체가 [Serializable] 특성을 선언해야 합니다.또한 개체에 사용자 지정 직렬화 방법이 필요한 경우 ISerializable 인터페이스도 구현해야 합니다.

https://vulncat.hpefod.com/en/detail?id=desc.structural.dotnet.asp_dotnet_bad_practices_non_serializable_object_stored_in_session#C%23%2fVB.NET%2fASP.NET

이 문제의 까다로운 버전이기 때문에 이 문제에 대한 구체적인 해결책은 번영을 위해 남겨둡니다.

Type 'System.Linq.Enumerable+WhereSelectArrayIterator[T...] was not marked as serializable

형식 필드가 있는 클래스로 인해IEnumerable<int>예:

[Serializable]
class MySessionData{
    public int ID;
    public IEnumerable<int> RelatedIDs; //This can be an issue
}

원래 문제 인스턴스:MySessionData구성할 수 없는 목록에서 설정되었습니다.

MySessionData instance = new MySessionData(){ 
    ID = 123,
    RelatedIDs = nonSerizableList.Select<int>(item => item.ID)
};

여기서의 원인은 구체적인 클래스입니다.Select<int>(...)직렬화할 수 없는 유형 데이터가 반환되고 ID를 새로 복사해야 합니다.List<int>그것을 해결하기 위해.

RelatedIDs = nonSerizableList.Select<int>(item => item.ID).ToList();

언급URL : https://stackoverflow.com/questions/15707872/error-is-not-marked-as-serializable

반응형