1
我已經在WCF中創建了一個自託管web服務。當我試圖從不同的系統調用這個web 服務時,它會導致不同的異常,並且它運行正常。類型'System.Threading.Tasks.Task'無法序列化
我的服務如下。
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);
}
}
我認爲它工作正常.net框架工作4.5,並在我的系統只與.NET 4.0。 – Kafaltiya1205
你有例外嗎?如果是這樣,請發佈服務器和客戶端的例外情況! – Jehof
@Jehof這是我得到的異常..類型'System.Threading.Tasks.Task'不能序列化。考慮用DataContractAttribute屬性標記爲 ,並用DataMemberAttribute屬性標記所有您想要的 字符串的成員。如果類型是集合, 考慮使用CollectionDataContractAttribute標記它。有關其他支持的類型,請參閱Microsoft .NET Framework文檔。 – Kafaltiya1205