<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      WCF閑談:如何在流模式下傳遞參數

         正好在園子中看到一篇博文在流模式下保持服務實例的狀態的兩種設計方式,細心的看了看,發現博主對WCF下流傳輸做了很深入的研究,但在程序的實現上頗顯復雜,沒有充分并且靈活的運用WCF的特性,在博主的那篇文章中要實現的目的就是將本地一個文件用流形式傳遞給遠程,并且要求遠程和本地的文件名稱一致。樓主的實現中,在PerCall模式下一次調用完不成一次傳輸,需要在調用的過程中,用靜態變量保持會話,這樣顯然過于復雜。其實這個實現非常簡單,只需要運用MessageHeader就能輕松解決,我前面的文章WCF 進階:為每個操作附加身份信息中也提到了,使用MessageHeader能附加用戶身份信息,那么附件任何其他信息,行不行,肯定是沒問題的。所以。。。。

        服務:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Runtime.Serialization;
      using System.ServiceModel;
      using System.Text;
      using System.IO;
      
      namespace Robin_Wcf_Stream_SvcLib
      {
          // 注意: 如果更改此處的類名“IService1”,也必須更新 App.config 中對“IService1”的引用。
          public class Service1 : IService1
          {
              public string GetData(int value)
              {
                  return string.Format("You entered: {0}", value);
              }
      
              public CompositeType GetDataUsingDataContract(CompositeType composite)
              {
                  if (composite.BoolValue)
                  {
                      composite.StringValue += "Suffix";
                  }
                  return composite;
              }
      
              public string StreamOperation(Stream stream)
              {
                  System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("utf-8");
      
                  string name = ""; 
      
                  int index = OperationContext.Current.IncomingMessageHeaders.FindHeader("filename", "http://tempuri.org");
      
                  if (index >= 0)
                  { 
                      name = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index).ToString(); 
                  }
                  byte[] buffer = RetrieveBytesFromStream(stream);
                  string text = System.Text.Encoding.UTF8.GetString(buffer);
                  Console.WriteLine("哈哈哈,我已經收到文件名稱:"+name+",并且字節流中的數據為:"+text);
                  return name;
              }
      
              public static byte[] RetrieveBytesFromStream(Stream stream)
              {
      
                  List<byte> lst = new List<byte>();
                  byte[] data = new byte[1024];
                  int totalCount = 0;
                  while (true)
                  {
                      int bytesRead = stream.Read(data, 0, data.Length);
                      if (bytesRead == 0)
                      {
                          break;
                      }
                      byte[] buffers = new byte[bytesRead];
                      Array.Copy(data, buffers, bytesRead);
                      lst.AddRange(buffers);
                      totalCount += bytesRead;
                  }
                  return lst.ToArray();
              }
          }
      }

      宿主:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.ServiceModel;
      using Robin_Wcf_Stream_SvcLib;
      
      namespace Robin_Wcf_Stream_Host
      {
          class Program
          {
              static void Main(string[] args)
              {
                  //服務地址
                  Uri baseAddress = new Uri("http://127.0.0.1:8081/");
                  ServiceHost host = new ServiceHost(typeof(Service1), new Uri[] { baseAddress });
                  //服務綁定
                  BasicHttpBinding bind = new BasicHttpBinding();
                  host.AddServiceEndpoint(typeof(IService1), bind, "");
                  if (host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceMetadataBehavior>() == null)
                  {
                      System.ServiceModel.Description.ServiceMetadataBehavior svcMetaBehavior = new System.ServiceModel.Description.ServiceMetadataBehavior();
                      svcMetaBehavior.HttpGetEnabled = true;
                      svcMetaBehavior.HttpGetUrl = new Uri("http://127.0.0.1:8001/Mex");
                      host.Description.Behaviors.Add(svcMetaBehavior);
                  }
                  host.Opened += new EventHandler(delegate(object obj, EventArgs e)
                  {
                      Console.WriteLine("服務已經啟動!");
                  });
                  host.Open();
                  Console.Read();
              }
          }
      }

      客戶端:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using Robin_Wcf_Stream_Client.ServiceReference1;
      using System.ServiceModel;
      using System.ServiceModel.Channels;
      
      namespace Robin_Wcf_Stream_Client
      {
          class Program
          {
              static void Main(string[] args)
              {
      
                  System.Threading.Thread.Sleep(4000);
                  Service1Client svc = new Service1Client();
      
                  using (OperationContextScope scope = new OperationContextScope(svc.InnerChannel))
                  {
      
                      MessageHeader header = MessageHeader.CreateHeader("filename", "http://tempuri.org", "robinzhang");
      
                      OperationContext.Current.OutgoingMessageHeaders.Add(header);
      
                      System.IO.MemoryStream ms = new System.IO.MemoryStream();
      
                      byte[] buffer = System.Text.Encoding.UTF8.GetBytes("您好,尊敬的用戶!");
                      ms.Write(buffer, 0, buffer.Length);
                      ms.Seek(0, System.IO.SeekOrigin.Begin);
                      string res = svc.StreamOperation(ms);
                      Console.WriteLine(res);
                      Console.Read();
      
                  }
              }
          }
      }

      一次調用搞定!

      效果圖如下:

      服務端:

      image

      客戶端:

      image

      項目文件:點擊下載

      posted @ 2010-04-13 19:00  Robin Zhang  閱讀(5055)  評論(14)    收藏  舉報
      主站蜘蛛池模板: 一区二区三区四区亚洲自拍| 国内自拍偷拍一区二区三区| 国产在线精品国偷产拍| 国产激情免费视频在线观看| 国产真人无遮挡免费视频| 国产资源精品中文字幕| 又黄又爽又色的免费网站| 国产成人久久精品二三区| 元氏县| 春菜花亚洲一区二区三区| 欧美乱妇狂野欧美在线视频| 亚洲欧洲精品一区二区| 69人妻精品中文字幕| 丁香花成人电影| 少妇愉情理伦片丰满丰满午夜| 99精品视频在线观看免费蜜桃| 国产高跟黑色丝袜在线| 国产激情艳情在线看视频| 亚洲青青草视频在线播放| 精品久久久久久无码中文字幕| 国产精品中文字幕免费| 精品人妻伦一二三区久久aaa片| 亚洲 欧洲 无码 在线观看| 革吉县| 欧美精品在线观看视频| 久久99久久99精品免观看| 亚洲日本精品一区二区| 连州市| 国产免费午夜福利片在线| 波多野结衣乳喷高潮视频| 中文字幕国产精品一区二| 免费国产午夜理论片不卡 | 国产性生大片免费观看性| 久久精品蜜芽亚洲国产AV| 玩弄放荡人妻少妇系列| 久久精品国产www456c0m| 久久99日本免费国产精品| 国产成人无码精品久久久露脸| 亚洲成a人无码av波多野| 国产乱女乱子视频在线播放| 日本一区二区三区免费播放视频站|