在WCF中,合同可以切换到流模式,以传输大型消息.
在阅读和测试后,在我看来,流模式不能用于双工通道(具有OneWay调用和回调接口的通道).
是这样吗?双工和流媒体不能互相使用吗?或者有办法吗?
(我正在尝试将大文件上传到服务并使用回调来报告此进度)
要将文件从客户端加载到服务器,您可以使用以下代码:service
[ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(IFreeBoxServiceCallBack))] public interface IFreeBoxService { [OperationContract(IsOneWay = true)] void sendFile(byte[] bytes, int offset, int count); [OperationContract(IsOneWay = true)] void sendFileName(string fileName); } [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)] public class FreeBoxService:IFreeBoxService { string path = "D:\\repository\\"; string fileName = ""; IFreeBoxServiceCallBack callBack = null; public FreeBoxService() { callBack = OperationContext.Current.GetCallbackChannel(); } public void sendFileName(string fileName) { this.fileName = fileName; } public void sendFile(byte[] bytes, int offset, int count) { using (FileStream fileStream = new FileStream(path + fileName, FileMode.Append, FileAccess.Write)) { fileStream.Write(bytes, offset, count); fileStream.Close(); fileStream.Dispose(); } }}
客户:
private void sendFileToServer() { FileStream fs = new FileStream(fullName,FileMode.Open,FileAccess.Read); int bytesRead = 0; bytes = new byte[15000]; proxy.sendFileName("someFile.xml"); while ((bytesRead = fs.Read(bytes, 0, 15000))>0) { proxy.sendFile(bytes, 0, bytesRead); } fs.Close(); fs.Dispose(); }
的.config
传递一个字节数组总是更好.我想,不需要描述回调函数吗?