발생한 문제 :
처리되지 않은 'System.Runtime.Serialization.SerializationException' 형식의 예외가 mscorlib.dll에서 발생했습니다.
추가 정보: 'TestClass1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 어셈블리를 찾을 수 없습니다.
상황 설명 :
서로 다른 프로그램에서 클래스 객체를 Data로 주고받는, TCP/IP 통신 프로그램 구현 중 발생함.
클라이언트 쪽 프로그램에서 Serialize 함수 사용 및, 해당 데이터를 byte [] 화 해서 서버 쪽으로 전달함.
서버 쪽에서 byte [] 형으로 받아서, Deserialize 함수를 사용하면 해당 에러 창이 발생함.
서버 쪽 구현 코드
byte[] byteAry;
TestClass tempinfo = new TestClass(1,1,1,1);
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
formatter.Serialize(memoryStream, tempinfo);
byteAry = memoryStream.ToArray();
}
send(byteAry);
클라이언트 쪽 구현 코드
byte[] byteAry = recv();
TestClass tempinfo = new TestClass();
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream(byteAry))
{
tempinfo = (TestClass)formatter.Deserialize(memoryStream);
}
필자 조치 방법 :
TestClass 클래스가 각 프로그램에 개별적으로 구현되어 있어서 발생한 문제.
클래스의 내용이 같아도, 서로 다른 cs파일로 존재하면 안 됨
(Deserialize, Serialize 함수 동작이 내부적으로 같은 어셈블리파일을 적용해야 하는 것 같음)
따라서. dll로 생성해서 같은 dll을 참조시켜 주니 문제 해결됨
1. 클래스 라이브러리 프로젝트로 생성
2. 해당 클래스 코드 작성
[Serializable]
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class TestClass
{
public int nImageWidth;
public int nImageHeight;
public int nImageBitrate;
public int nImageBandWidth;
public int nImageCount;
}
3. 빌드해서 dll파일 생성
4. 적용하고자 하는 서버, 클라이언트 프로젝의 참조 추가로 작성한 dll 추가하기
* 찾아보기에서 내가 작성한 dll 경로 찾아서 추가하기.
* 해당 dll 선택 후 확인 클릭
다시 서버, 클라이언트 프로그램 실행해서 받은 데이터 확인 하면, 오류 없이 정상적으로 작동함.
'정리 작업중' 카테고리의 다른 글
[C#] Visual Studio 2022 Community 설치 (0) | 2024.09.18 |
---|---|
[DataGridView] AllowUserToAddRows Property (0) | 2023.04.19 |
티스토리 RSS 설정, RSS란? (0) | 2023.02.09 |
티스토리 주소 설정 (기본도메인, 사이트맵주소, 포스트주소) (0) | 2023.02.09 |
Ajax 사용하기 (0) | 2023.01.16 |