Advertisement
Guest User

NamedPipe WCF transformer test

a guest
Aug 24th, 2010
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. namespace NPB
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Diagnostics;
  6.     using System.Runtime.Serialization;
  7.     using System.ServiceModel;
  8.     using System.ServiceModel.Channels;
  9.     using System.ServiceModel.Description;
  10.     using System.ServiceModel.Dispatcher;
  11.     using System.Xml;
  12.     using Newtonsoft.Json.Linq;
  13.  
  14.     [ServiceContract]
  15.     public interface ITransformer
  16.     {
  17.         [OperationContract]
  18.         [NetDataContractFormatAttribute]
  19.         JObject Transform(JObject o);
  20.     }
  21.  
  22.     [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
  23.     public class Transformer : ITransformer
  24.     {
  25.         public JObject Transform(JObject o)
  26.         {
  27.             o["Modified"] = new JValue(true);
  28.             return o;
  29.         }
  30.     }
  31.  
  32.     public class NetDataContractFormatAttribute : Attribute, IOperationBehavior
  33.     {
  34.         public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
  35.         {
  36.         }
  37.  
  38.         public void ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
  39.         {
  40.             ReplaceDataContractSerializerOperationBehavior(description);
  41.         }
  42.  
  43.         public void ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
  44.         {
  45.             ReplaceDataContractSerializerOperationBehavior(description);
  46.         }
  47.  
  48.         public void Validate(OperationDescription description)
  49.         {
  50.         }
  51.  
  52.         private static void ReplaceDataContractSerializerOperationBehavior(OperationDescription description)
  53.         {
  54.             var dcs = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
  55.  
  56.             if (dcs != null)
  57.                 description.Behaviors.Remove(dcs);
  58.  
  59.             description.Behaviors.Add(new NetDataContractSerializerOperationBehavior(description));
  60.         }
  61.  
  62.         public class NetDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior
  63.         {
  64.             private static readonly NetDataContractSerializer Serializer = new NetDataContractSerializer();
  65.  
  66.             public NetDataContractSerializerOperationBehavior(OperationDescription operationDescription)
  67.                 : base(operationDescription)
  68.             {
  69.             }
  70.  
  71.             public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns,
  72.                                                                  IList<Type> knownTypes)
  73.             {
  74.                 return Serializer;
  75.             }
  76.  
  77.             public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name,
  78.                                                                  XmlDictionaryString ns, IList<Type> knownTypes)
  79.             {
  80.                 return Serializer;
  81.             }
  82.         }
  83.     }
  84.  
  85.     public class RemoteTransformer : MarshalByRefObject
  86.     {
  87.         private readonly Transformer _transfomer = new Transformer();
  88.  
  89.         public JObject Transform(JObject o)
  90.         {
  91.             return _transfomer.Transform(o);
  92.         }
  93.     }
  94.  
  95.  
  96.     internal class Program
  97.     {
  98.         private static void Main(string[] args)
  99.         {
  100.             var t = new RemoteTransformer();
  101.  
  102.             var stopwatch1 = Stopwatch.StartNew();
  103.             for (int i = 0; i < 100000; i++)
  104.             {
  105.                 var jobj = new JObject(new JProperty("Hello", "There"));
  106.  
  107.                 t.Transform(jobj);
  108.             }
  109.  
  110.             Console.WriteLine(stopwatch1.ElapsedMilliseconds);
  111.  
  112.             var basePipeAddress = new Uri("net.pipe://localhost/TestService");
  113.  
  114.             var host = new ServiceHost(typeof (Transformer), basePipeAddress);
  115.             var pipeBinding = new NetNamedPipeBinding();
  116.  
  117.  
  118.             host.AddServiceEndpoint(typeof (ITransformer), pipeBinding, basePipeAddress);
  119.             host.Open();
  120.  
  121.             Console.WriteLine("Service is ready");
  122.  
  123.             Console.WriteLine("Creating client");
  124.  
  125.             var fact = new ChannelFactory<ITransformer>(new NetNamedPipeBinding(), basePipeAddress.ToString());
  126.             var serviceInstance = fact.CreateChannel();
  127.  
  128.             using (serviceInstance as IDisposable)
  129.             {
  130.                 Console.WriteLine("Running transform loop");
  131.                 var startNew = Stopwatch.StartNew();
  132.  
  133.                 for (int i = 0; i < 100000; i++)
  134.                 {
  135.                     var jobj = new JObject(new JProperty("Hello", "There"));
  136.  
  137.                     serviceInstance.Transform(jobj);
  138.                 }
  139.                 Console.WriteLine(startNew.ElapsedMilliseconds);
  140.             }
  141.  
  142.             Console.ReadLine();
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement