SHARE
TWEET

Untitled

a guest Aug 24th, 2010 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.ServiceModel;
  5. using System.Text;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8.  
  9. namespace ConsoleApplication1 {
  10.     internal class Program {
  11.         private static void Main(string[] args) {
  12.             var host = new ServiceHost(new RemoteTransformer());
  13.  
  14.             var binding = new NetNamedPipeBinding {
  15.                 MaxBufferPoolSize = 1024*1024,
  16.                 MaxBufferSize = 1024*1024,
  17.                 MaxReceivedMessageSize = 1024*1024
  18.             };
  19.  
  20.             host.AddServiceEndpoint(typeof (IRemoteTransformer), binding, "net.pipe://localhost/TestService");
  21.  
  22.             host.Open();
  23.  
  24.             var t = ChannelFactory<IRemoteTransformer>.CreateChannel(binding,
  25.                 new EndpointAddress("net.pipe://localhost/TestService"));
  26.  
  27.             var startNew = Stopwatch.StartNew();
  28.  
  29.             var batch = new StringBuilder();
  30.             var sw = new StringWriter(batch);
  31.             var writer = new JsonTextWriter(sw);
  32.  
  33.             writer.WriteStartArray();
  34.             for (var i = 0; i < 100000; i++) {
  35.                 var jobj = new JObject(new JProperty("Hello", "There"));
  36.  
  37.                 jobj.WriteTo(writer);
  38.  
  39.                 if (i%200 == 0) {
  40.                     writer.WriteEndArray();
  41.                     writer.Flush();
  42.  
  43.                     t.TransformBatch(new MemoryStream(Encoding.Default.GetBytes(batch.ToString())));
  44.                     batch.Clear();
  45.                     writer.WriteStartArray();
  46.                 }
  47.             }
  48.  
  49.             if (batch.Length > 0) {
  50.                 writer.WriteEndArray();
  51.                 writer.Flush();
  52.  
  53.                 t.TransformBatch(new MemoryStream(Encoding.Default.GetBytes(batch.ToString())));
  54.             }
  55.  
  56.             Console.WriteLine(startNew.ElapsedMilliseconds);
  57.             Console.ReadLine();
  58.         }
  59.     }
  60.  
  61.     [ServiceContract]
  62.     public interface IRemoteTransformer {
  63.         [OperationContract]
  64.         byte[] TransformBatch(Stream objects);
  65.  
  66.         [OperationContract]
  67.         JObject Transform(JObject obj);
  68.     }
  69.  
  70.     [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
  71.     public class RemoteTransformer : IRemoteTransformer {
  72.         private readonly Transformer transfomer = new Transformer();
  73.  
  74.         #region IRemoteTransformer Members
  75.  
  76.         public byte[] TransformBatch(Stream objects) {
  77.             var result = new MemoryStream(1024);
  78.  
  79.             using (var sw = new StreamWriter(result))
  80.             using (var writer = new JsonTextWriter(sw))
  81.             using (var sr = new StreamReader(objects))
  82.             using (var reader = new JsonTextReader(sr)) {
  83.                 reader.Read();
  84.  
  85.                 writer.WriteStartArray();
  86.  
  87.                 while (reader.Read() && reader.TokenType == JsonToken.StartObject) {
  88.                     var jobj = JObject.Load(reader);
  89.                     this.transfomer.Transform(jobj);
  90.                     jobj.WriteTo(writer);
  91.                 }
  92.  
  93.                 writer.WriteEndArray();
  94.             }
  95.  
  96.             return result.GetBuffer();
  97.         }
  98.  
  99.         public JObject Transform(JObject obj) {
  100.             return this.transfomer.Transform(obj);
  101.         }
  102.  
  103.         #endregion
  104.     }
  105.  
  106.     public class Transformer {
  107.         public JObject Transform(JObject obj) {
  108.             obj["Modified"] = new JValue(true);
  109.             return obj;
  110.         }
  111.     }
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top