【发布时间】:2011-09-22 15:31:36
【问题描述】:
我正在尝试构建一个 WCF 服务,该服务将允许我的 WPF 桌面客户端将文件上传到服务器。
我改编了来自 The Code Project (WCF Streaming: Upload/Download Files Over HTTP) 的代码示例,并且我也查看了几个 SO 帖子,但似乎无法使其正常工作。
当我执行代码时,它在服务器尝试读取已通过接口传递的流时失败并出现空引用异常。
在这一点上,我很迷茫,不知道如何解决这个问题。任何建议表示赞赏。
代码示例如下:
CustomerDocumentModel是我通过WCF接口用流读取客户端文件的数据元素:
[DataContract]
[KnownType(typeof(System.IO.FileStream))]
public class CustomerDocumentModel : IDisposable
{
public CustomerDocumentModel()
{
}
public CustomerDocumentModel(string documentName, string path)
{
DocumentName = documentName;
Path = path;
}
[DataMember]
public string DocumentName;
[DataMember]
public string Path;
[DataMember]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
IBillingService 是我的 WCF 服务的接口定义:
[ServiceContract]
public interface IBillingService
{
// other methods redacted...
[OperationContract]
void UploadCustomerDocument(CustomerDocumentModel model);
}
BillingService 类实现 WCF 服务:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class BillingService : IBillingService
{
// Other methods redacted ...
public void UploadCustomerDocument(CustomerDocumentModel model)
{
string path = HttpContext.Current.Server.MapPath(
String.Format("/Documents/{1}",
model.DocumentName));
using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
const int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
int size = 0;
try
{
// The following Read() fails with a NullReferenceException
while ((size = model.FileByteStream.Read(buffer, 0, bufferSize)) > 0)
{
stream.Write(buffer, 0, size);
}
}
catch
{
throw;
}
finally
{
stream.Close();
model.FileByteStream.Close();
}
}
}
}
我的 WCF Web 服务器上的 web.config 中的一些相关位:
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="2097151" useFullyQualifiedRedirectUrl="true" executionTimeout="360"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment
aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="userHttps" transferMode="Streamed" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="">
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
客户端是一个 WPF/MVVM 应用程序,它创建一个 CustomerDocumentModel 模型,使用 OpenFileDialog 来 Open() 文件流,然后将模型传递给 WCF 服务上的 UploadCustomerDocument 方法。
如果我遗漏任何相关细节,请询问。
【问题讨论】:
-
注意:解决方案不必是 WCF。我也会考虑其他 C#/ASP.NET 解决方案。
标签: wcf streaming basichttpbinding