Posts

Showing posts from May, 2012

Tibco Rendezvous (tibrv) C# .Net example

This is an example of broadcast messaging using TIBCO Rendezvous . You can find more information about queue, dispatcher, transport and other rendezvous concepts in the TIBCO Rendezvous® Concepts pdf documentation. Command Line: Receiving messages tibrvlisten –service <port> -network "<ip-address>" –daemon <port> "<subject>" tibrvlisten –service <port> -network ";<multicast-ip-address>" –daemon <port> "<subject>" tibrvlisten -service 7500 -network "192.168.xx.xxx" -daemon 7500 "ME.TEST" tibrvlisten -service 7500 -network "192.168.xx.xxx" -daemon 7500 "ME.>" Publishing messages tibrvsend –service <port> -network "<ip-address>" –daemon <port> "<subject>" "<your-message>" tibrvsend -service 7500 -network "192.168.xx.xxx" -daemon 7500 "ME.TEST" "He

Using Task and WebRequest to asynchronously call RESTful service

Threading in C# is a good website describing Task Parallelism and other aspects of C# Threading. [Test] //For GET/POST/PUT/DELETE calls public void TestWebRequest() { Task< string > task = Task.Factory.StartNew< string >(() => // Begin task { var req = (HttpWebRequest)WebRequest.Create(" http://localhost:52292/Api/Product/31a3653e-2db0-429e-8cfb-8842bd984b69 "); req.Method = " GET "; //POST/PUT/DELETE using (var response = (HttpWebResponse)req.GetResponse()) { if (response.ContentLength > 0) { var reader = new StreamReader(response.GetResponseStream()); return reader.ReadToEnd(); } } return null ; }); Console.WriteLine(" Do something else.. "); //wait for Task Result string result = task.Result; Console.WriteLine(" Completed.. "); Console.WriteLine(result); } [Test] /

C# reflection based simple ObjectMapper

public class ObjectMapper { public static object Map( object from, Func<PropertyInfo, bool > ignore) { return Map(from, null , ignore); } public static object Map( object from, object to, Func<PropertyInfo, bool > ignore) { if (from== null ) throw new ArgumentException(" NULL value "," from "); if (to == null ) { to = Activator.CreateInstance(from.GetType()); } Type type = from.GetType(); var properties = type.GetProperties(); foreach (var property in properties) { if (ignore== null || !ignore(property)) { if (property.GetSetMethod()!= null ) property.SetValue(to, property.GetValue(from, null ), null ); } } return to; } } And an example of ignore method implementation to not map properties annotated with Association attribute (from Linq2Sql

C# JSON Serializer

Add Reference to System.Web.Extensions [Test] public void Test() { var serializer = new JavaScriptSerializer(); var input = new SerializationTest() { Id = 1 }; var text = serializer.Serialize(input); Console.WriteLine(text); var output = serializer.Deserialize<SerializationTest>(text); Console.WriteLine(" Id: {0} ", output.Id); } class SerializationTest { public int Id { get ; set ; } }

C# Addin framework, MEF Example (Managed Extensibility Framework)

Image
Example of using MEF to dynamically load calculator object. Create .Net 4.0 solution with the following projects (MEFExample is Console others are Libraries) Add Reference to System.ComponentModel.Composition to Library projects. public interface ICalculator { string Name { get ; } int Calculate( int a, int b); } public class CalculatorManager { [ImportMany( typeof (ICalculator))] private IEnumerable<ICalculator> _calculators; public IEnumerable<ICalculator> GetCalculators() { var catalog = new AggregateCatalog(); catalog.Catalogs.Add( new DirectoryCatalog(@" .\extensions\ ")); var container = new CompositionContainer(catalog); container.ComposeParts( this ); Console.WriteLine(" Found {0} Calculators ", _calculators.Count()); return _calculators; } } [Export( typeof (ICalculator))] public class CalculatorAdd : ICalculator { publi