2009-01-07 78 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