2009-01-07 74 views
4

在WCF中,合同可以切换到流模式,以传输大量消息。WCF:是否可以在双工信道中使用流模式?

阅读和测试在我看来之后,该流模式不能与全双工信道(与单向通话和一个回调接口通道)使用。

这是吗?双工和流媒体不能相互使用吗?还是有办法?

(我想一个大文件上传到服务,并使用回调汇报这一进展)

回答

5

从客户端将文件加载到服务器,你可以使用下面的代码:服务

[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<IFreeBoxServiceCallBack>(); 
     } 

     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

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <services> 
     <service name="FreeBoxServiceLib.FreeBoxService" behaviorConfiguration="MyServiceBehevior"> 
     <endpoint address="" contract="FreeBoxServiceLib.IFreeBoxService" 
        binding="netTcpBinding"> 
     </endpoint> 
     <endpoint address="MEX" binding="mexTcpBinding" contract="IMetadataExchange"/> 
     <host> 
      <baseAddresses> 
      <add baseAddress="net.tcp://localhost:8080/FreeBoxService/"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="MyServiceBehevior"> 
      <serviceMetadata /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

总是最好传递一个字节数组。我认为,不需要描述回调函数?

2

出于好奇,我正要开始对你的问题的一些测试,但谷歌随后透露给我两个样本可能会更好地回答你的问题。

This CodeProject example节目流以进度条的文件传输,而无需使用双工信道。

This sample示出了更多的相同的但具有一些不同配置之流。

而且,所有的东西真的好资源WCF相关的是iDesgin.net。主要的人有Juval Lowy,他写了一些关于WCF的最好的书。他们有几十个优秀的WCF例子可以下载(尽管他们烦恼地询问你的每个人的电子邮件地址)。更重要的是,他们还编写了一个ServiceProcessEx类,它极大地扩展了ServiceProcess的功能,特别是在双工通道方面。 (我不确定它是否与流媒体处理有很大关系,尽管它不是我已经完成的)。

希望这对你有帮助。

+4

第一个(CodeProject上)不具有参照双螺旋结合 第二两条链路被破坏 – 2012-04-19 08:43:41