UploadFile:WCF测试客户端不支持此操作,因为它使用类型ClientFileInfo



我对WCF服务还很陌生,希望能提供一些帮助。我正在尝试将WCF作为一项服务运行,并让另一台计算机上的ASP.net客户端能够通过连接到WCF服务将文件上传到它。

我正在用一个简单的上传设置(从这里开始)测试它,如果我只是将WCF服务引用为"dll",它会很好地工作,但如果我试图将其作为WCF服务运行,它会给我一个"UploadFile"方法的错误,说明它不受支持。

方法名称上带有红色X的确切消息:WCF测试客户端不支持此操作,因为它使用类型FileUploadMessage。

我首先在Visual Studio 2012中创建一个WCF服务应用程序,并在我的界面(IUploadService.cs)中包含以下内容:

[ServiceContract]
public interface IUploadService
{
[OperationContract(IsOneWay = true)]
void UploadFile(FileUploadMessage request);
}
[MessageContract]
public class FileUploadMessage
{
[MessageBodyMember(Order = 1)]
public Stream FileByteStream;
}

它是这样实现的(UploadService.svc.cs):

public void UploadFile(FileUploadMessage request)
{
Stream fileStream = null;
Stream outputStream = null;
try
{
fileStream = request.FileByteStream;
string rootPath = ConfigurationManager.AppSettings["RootPath"].ToString();
DirectoryInfo dirInfo = new DirectoryInfo(rootPath);
if (!dirInfo.Exists)
{
dirInfo.Create();
}
// Create the file in the filesystem - change the extension if you wish, 
// or use a passed in value from metadata ideally
string newFileName = Path.Combine(rootPath, Guid.NewGuid() + ".jpg");
outputStream = new FileInfo(newFileName).OpenWrite();
const int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead = fileStream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.Write(buffer, 0, bufferSize);
bytesRead = fileStream.Read(buffer, 0, bufferSize);
}
}
catch (IOException ex)
{
throw new FaultException<IOException>(ex, new FaultReason(ex.Message));
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
if (outputStream != null)
{
outputStream.Close();
}
}
} // end UploadFile

从外观上看,它应该可以工作,但从我通过查看几个stackoverflow和其他论坛问题所了解的情况来看,WCF似乎不支持Stream,尽管我们可以有类型流的绑定。我对这件事和我做错了什么感到困惑。

谢谢你的帮助。

在Adam的博客上与我交流代码后,我意识到我的测试方式不对。一切都实现得很正确,但使用WCF测试客户端会把事情搞砸。只需启动项目并通过将其添加为"Service.svc"的web引用来使用它,效果就很好。

相关内容

最新更新