我正在使用Sql服务器文件流类型将大型文件存储在后端。我正在尝试使用WCf将文件传输到客户端。如何使用SQL文件流式传输win32 API并支持WCF流式传输
我能够使用SQLFileStream(API)获取文件的句柄。然后我尝试返回这个流。我在客户端实现了数据分块以从流中检索数据。
我可以为常规文件流和内存流做到这一点。此外,如果我转换然后sqlfilestream到内存也是可行的。唯一不行的是当我尝试返回sqlfilestream时。我究竟做错了什么。
我已经尝试nettcpbinding与流启用和http绑定与MTOM编码。
这是我的错误信息获取:
Socket连接被中止。这可能是由于处理你的消息或远程主机超出接收超时的错误或底层网络问题引起的。本地套接字timneout为00:09:59 ....
这是我的示例代码
RemoteFileInfo info = new RemoteFileInfo();
info.FileName = "SampleXMLFileService.xml";
string pathName = DataAccess.GetDataSnapshotPath("DataSnapshot1");
SqlConnection connection = DataAccess.GetConnection();
SqlTransaction sqlTransaction = connection.BeginTransaction("SQLSileStreamingTrans");
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.Transaction = sqlTransaction;
command.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()";
byte[] transcationContext = command.ExecuteScalar() as byte[];
SqlFileStream stream = new SqlFileStream(pathName, transcationContext, FileAccess.Read);
// byte [] bytes = new byte [stream.Length]; // stream.Read(bytes,0,(int)stream.Length);
// Stream reeturnStream = stream; // MemoryStream memoryStream = new MemoryStream(bytes);
info.FileByteStream = stream;
info.Length = info.FileByteStream.Length;
connection.Close();
return info;
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
任何帮助表示赞赏
解决了我的问题!只是为了让任何人都不清楚,拔出所有使用WCF流媒体示例的使用语句(以确保正确处理对象),并将它们处理到服务处理函数(实现IDisposable。 – Sam 2013-11-22 14:21:03
此VB.Net示例执行什么操作你需要 - http://petermeinl.wordpress.com/2012/02/20/managing-blobs-using-sql-server-filestream-via-ef-and-wcf-streaming/,很容易转换为C#,因为我有,如果任何人需要帮助,只需给我发一条消息/评论,因为我知道服务错误可以修复多么令人沮丧! – Sam 2013-11-22 14:22:16