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 messagestibrvlisten –service <port> -network "<ip-address>" –daemon <port> "<subject>"
tibrvlisten –service <port> -network ";<multicast-ip-address>" –daemon <port> "<subject>"

tibrvlisten -service 7500 -network "" -daemon 7500 "ME.TEST"
tibrvlisten -service 7500 -network "" -daemon 7500 "ME.>"Publishing messagestibrvsend –service <port> -network "<ip-address>" –daemon <port> "<subject>" "<your-message>"tibrvsend -service 7500 -network "" -daemon 7500 "ME.TEST" "Hello World"
Local Daem…

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 callspublicvoid 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/DELETEusing (var response = (HttpWebResponse)req.GetResponse()) { if (response.ContentLength > 0) { var reader = new StreamReader(response.GetResponseStream()); return reader.ReadToEnd(); } } returnnull; }); Console.WriteLine("Do something else.."); //wait for Task Resultstring result = task.Result; Console.WriteLine("Completed.."); Console.WriteLine(result); } [Test] //this is only for GET callspublicvoid TestWebClient() { Task<string> task = T…

C# reflection based simple ObjectMapper

publicclass ObjectMapper { publicstaticobject Map(object from, Func<PropertyInfo, bool> ignore) { return Map(from, null, ignore); } publicstaticobject Map(object from, object to, Func<PropertyInfo, bool> ignore) { if(from==null) thrownew 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)publicstaticbool IgnoreAssociations(PropertyInfo property) { if (Attribute.GetCustomAttribute(property, typeof(Association…

C# JSON Serializer

Add Reference to System.Web.Extensions[Test] publicvoid 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 { publicint Id { get; set; } }

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

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.publicinterface ICalculator { string Name { get; } int Calculate(int a, int b); } publicclass 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))] publicclass CalculatorAdd : ICalculator { publicstring Name { get { return "Mul a & b"; } } pu…