gpt4 book ai didi

c# - 异常 : Cannot find the assembly Mynamespace , Version=0.0.0.0,Culture=neutral,PublicKeyToken=null

转载 作者:行者123 更新时间:2023-11-30 22:23:33 24 4
gpt4 key购买 nike

我尝试通过@TCP 将图像对象作为序列化文件从客户端发送到服务器并得到这个异常

 image exception

服务器代码

 namespace Receiver
{
[Serializable()]
public class ImageSerial : ISerializable
{
public Image img = null;

public ImageSerial(SerializationInfo info, StreamingContext ctxt)
{
img = (Image)info.GetValue("OMG", typeof(Image));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("OMG", img);
}
}

public class ObjSerial
{
private Stream stream;
private BinaryFormatter bformatter;
private string FILENAME = "m.bin";
ImageSerial mp = new ImageSerial();

public Image getImgFromBin()
{

stream = File.Open(FILENAME, FileMode.Open);
bformatter = new BinaryFormatter();

mp = (ImageSerial)bformatter.Deserialize(stream);
stream.Close();
return mp.img;
}

客户端代码

    namespace WindowsFormsApplication5
{
[Serializable()]
class ImageSerial :ISerializable
{
public Image img = null;
public ImageSerial() { }

public ImageSerial(SerializationInfo info, StreamingContext ctxt)
{
img = (Image)info.GetValue("OMG", typeof(Image));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("OMG",img);
}
}

public class ObjSerial
{
private string FILENAME = "m.bin";
private TcpClient tcpClient;
private FileStream fstFile;
private NetworkStream strRemote;
private string SERVERIP = "10.102.239.207";
private int SERVERPort = 5051;

public void start(Image ims)
{

ImageSerial mp = new ImageSerial();
mp.img = ims;

Stream stream = File.Open(FILENAME, FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();

bformatter.Serialize(stream, mp);
stream.Close();

//Clear mp for further usage.
sendFile();



}
private void ConnectToServer(string ServerIP, int ServerPort)
{
tcpClient = new TcpClient();
try
{
tcpClient.Connect(ServerIP, ServerPort);
}
catch (Exception exMessage)
{
// Display any possible error
}
}

private void sendFile()
{

ConnectToServer(SERVERIP, SERVERPort);
if (tcpClient.Connected == false)
{
ConnectToServer(SERVERIP, SERVERPort);
}
strRemote = tcpClient.GetStream();
fstFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);

int bytesSize = 0;
byte[] downBuffer = new byte[2048];

while ((bytesSize = fstFile.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strRemote.Write(downBuffer, 0, bytesSize);
}
tcpClient.Close();
strRemote.Close();
fstFile.Close();


}
}

我已经阅读了很多关于这个异常的话题,他们都在谈论两种解决方案

  • 格式化程序.Binder
  • AppDomain.CurrentDomain.AssemblyResolve

还是不行

最佳答案

服务器端当然不会找到客户端程序集。它不应该。您的客户端代码不能存在于服务器端。这里的问题是您定义了两次 ImageSerial 类,一次在服务器中,一次在客户端中。如果你同时控制双方,那就完全错了。创建一个由客户端和服务器引用的公共(public)程序集,并将您的公共(public)类放在那里。

此外,删除从服务器到客户端的所有引用。如果您愿意,它应该以另一种方式绑定(bind),或者使用 WCF 等中间服务层。

关于c# - 异常 : Cannot find the assembly Mynamespace , Version=0.0.0.0,Culture=neutral,PublicKeyToken=null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13316557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com