类型“System.Threading.Tasks.Task”不能序列化
本文关键字:不能 序列化 Task Tasks System Threading 类型 | 更新日期: 2023-09-27 18:33:04
>我在WCF中创建了一个自承载的Web服务。当我尝试调用此网络时来自不同系统的服务 它会导致不同的异常,并且在某处运行良好。
我的服务如下。
网络服务:
namespace KryptonWebService
{
[ServiceContract()]
public interface ISetUpHost
{
[OperationContract()]
void EditHostFile(bool flag = true);
}
[ServiceContract]
public interface IFileTransferService
{
[OperationContract]//(IsOneWay = true)
// void UploadFile(FileUploadMessage request);
void UploadFile(Stream FileByteStream);
[OperationContract] //(IsOneWay = false)
Stream DownloadFile();
}
}
实现类:
public class KryptonService : KryptonWebService.ISetUpHost, KryptonWebService.IFileTransferService
{
//public int Add(int num1, int num2)
//{
// return num1 + num2;
//}
public void EditHostFile(bool flag = true)
{
Console.WriteLine("Do Nothing");
}
//public void UploadFile(FileUploadMessage request)
//{
// string basePath = @"C:'Users'Mahesh'Downloads'UploadedZip'";
// string serverFileName = Path.Combine(basePath, request.Filename);
// using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
// {
// const int bufferSize = 65536; // 64K
// Byte[] buffer = new Byte[bufferSize];
// int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
// while (bytesRead > 0)
// {
// outfile.Write(buffer, 0, bytesRead);
// bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
// }
// }
//}
//public FileDownloadReturnMessage DownloadFile(FileDownloadMessage request)
//{
// string localFileName = request.Filename;
// try
// {
// string basePath = @"C:'Users'Mahesh'Downloads'Zip'";
// string serverFileName = Path.Combine(basePath, request.Filename);
// Stream fs = new FileStream(serverFileName, FileMode.Open);
// return new FileDownloadReturnMessage(request.Filename, fs);
// }
// catch (IOException e)
// {
// return null;
// }
//}
public void UploadFile(Stream FileByteStream)
{
try
{
string filename = "Krypton_Uploaded.zip";
string basePath = @"C:'Users'Mahesh'Downloads'UploadedZip'";
string serverFileName = Path.Combine(basePath, filename);
using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
{
const int bufferSize = 65536; // 64K
Byte[] buffer = new Byte[bufferSize];
int bytesRead = FileByteStream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outfile.Write(buffer, 0, bytesRead);
bytesRead = FileByteStream.Read(buffer, 0, bufferSize);
}
}
}
catch (IOException e)
{
// throw new FaultException<ioexception>(e);
}
}
public Stream DownloadFile()
{
string localFileName = "Krypton1.zip";
try
{
string basePath = @"C:'Users'Mahesh'Downloads'Zip'";
string serverFileName = Path.Combine(basePath, localFileName);
Stream fs = new FileStream(serverFileName, FileMode.Open);
// return new KryptonWebService.FileDownloadReturnMessage(fs);
return fs;
}
catch (IOException e)
{
string msg = e.Message;
return null;
}
}
}
现在在客户端:
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IFileTransferService" maxReceivedMessageSize="32423432" />
<binding name="BasicHttpBinding_ISetUpHost" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://10.101.23.91:8090/KryptonService/Krypton"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransferService"
contract="IFileTransferService" name="BasicHttpBinding_IFileTransferService" />
<endpoint address="http://10.101.23.91:8090/KryptonService/Krypton"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISetUpHost"
contract="ISetUpHost" name="BasicHttpBinding_ISetUpHost" />
</client>
</system.serviceModel>
</configuration>
客户端代码:
private static void UploadFile(string uploadpath)
{
FileTransferServiceClient fileTransferClient = new FileTransferServiceClient();
string uploadfilename = "Krypton.zip";
// using (FileStream fs = new FileStream(@"C:'Users'Mahesh'Downloads'Zip'" + uploadfilename, FileMode.Open, FileAccess.Read, FileShare.Read))
using (FileStream fs = new FileStream(Path.Combine(uploadpath, uploadfilename), FileMode.Open, FileAccess.Read, FileShare.Read))
{
fileTransferClient.UploadFile(fs);
}
}
string downloadfilename = "KryptonDownloaded.zip";
Stream fileStream = null;
// FileDownloadReturnMessage fsd = new FileDownloadReturnMessage();
using (FileTransferServiceClient fileTransferClient = new FileTransferServiceClient())
{
try
{
fileStream = fileTransferClient.DownloadFile();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Stream outputStream = null;
try
{
outputStream = new FileInfo(Path.Combine(downloadPath, downloadfilename)).OpenWrite(); //new FileInfo(@"C:'Users'Mahesh'Downloads'Downloadfrom'" + downloadfilename).OpenWrite();
byte[] buffer = new byte[2048];
int bytesRead = fileStream.Read(buffer, 0, 2048);
while (bytesRead > 0)
{
outputStream.Write(buffer, 0, 2048);
bytesRead = fileStream.Read(buffer, 0, 2048);
}
}
Remove System.Threading.Tasks.Task 包含来自 FileTransferServiceClient.cs 的方法。
例如;
public interface IFTSService
{
[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IFTSService/ContractSend", ReplyAction = "http://tempuri.org/IFTSService/ContractSendResponse")]
string ContractSend(SaveContractType saveContractType);
//[System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IFTSService/ContractSend", ReplyAction = "http://tempuri.org/IFTSService/ContractSendResponse")]
//System.Threading.Tasks.Task<string> ContractSendAsync(SaveContractType saveContractType);
}