【发布时间】:2013-09-17 02:35:38
【问题描述】:
几天前,我开始在 C# 中使用 library。当我遇到错误时,我试图发送一个自定义类,这是我的代码:
Main.cs
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Add("Sending message to server saying '" + textBox2.Text + "'");
TCPConnection connection = TCPConnection.GetConnection(new ConnectionInfo(serverIP, serverPort));
SendReceiveOptions options = connection.ConnectionDefaultSendReceiveOptions.Clone() as SendReceiveOptions;
options.DataProcessors.Add(DPSManager.GetDataProcessor<RijndaelPSKEncrypter>());
RijndaelPSKEncrypter.AddPasswordToOptions(options.Options, "Your strong PSK");
if (connection.SendReceiveObject<string>("Payload", "Ack", 1000, new PayloadFile(textBox2.Text)) != null) // <-- Giving an exception here, "Type is not expected, and no contract can be inferred: TCP_Client.PayloadFile"
{
listBox1.Items.Add("Done");
}
}
PayloadFile.cs
public class PayloadFile
{
public string FileName;
public string FileLocation;
public FileStream FileContent;
public PayloadFile(string FileToLoad)
{
if (!File.Exists(FileToLoad))
{
throw new FileNotFoundException();
}
FileInfo PayloadInfo = new FileInfo(FileToLoad);
FileName = PayloadInfo.Name;
FileLocation = PayloadInfo.DirectoryName;
FileContent = File.OpenRead(FileToLoad);
}
}
随消息抛出的异常:
Type is not expected, and no contract can be inferred: TCP_Client.PayloadFile
我怀疑这门课写错了?
【问题讨论】:
-
错误消息来自 protobuf-net,它被该库用作默认序列化程序。 networkcomms.net/custom-objects 展示了如何创建使用 protobuf-net 的类。另见code.google.com/p/protobuf-net/wiki/GettingStarted
-
谢谢@dtb...你能提供一些我可以效仿的例子吗?谢谢!
-
您看过以上链接中的示例了吗?
-
是,但还不知道如何实现...
-
你在什么时候卡住了? StackOverflow 上有789 questions tagged with 'protobuf-net'。也许其中之一有帮助?
标签: c# tcp networkcomms.net