Distributing data to other systems and in this instance I used MSMQ

I remember last year I designed a asp.net application where I had couple of Webforms to collect data and I was storing in the database.

Ie. Webform facilitates insert, Update and Delete of Person and allow to set reminders for the person, which is later on send to him via Email, SMS and other ways…

This year the client wanted one of their legacy application to pickup the reminders and display on is own calendar…. but the catch is the the legacy app will use its own database structure and its own programming language etc…

So client wants to have data distributed to the legacy app when Person is created or Reminders are added. Now onCreate of a Person or Reminder if I go and write codes to put data on the legacy app my application will become bulky and will become slower (its a asp.net application), Also in near future someone else might come and tell to distribute data to their system as well…..

What I did is I created couple of Queue s in MSMQ and started passing Messages.

I wrote another Windows Service which now reads Messages from Queues and distributes data to the Legacy System.

Queuing Message in MSMQ is pretty easy in .Net… 

public  void QueueJob(Job job, string destinationQueue, string messageLabel)

{
try
{
// open the queue
MessageQueue mq = new MessageQueue(destinationQueue);
// set the message to durable.
mq.DefaultPropertiesToSend.Recoverable = true;
// set the formatter to Binary, default is XML
//mq.Formatter = new BinaryMessageFormatter();
// send the job object
mq.Send(job, messageLabel);
mq.Close();
}
catch (Exception e)
{
throw e;
}

}

In my web application I Queued jobs like this:

//Passing the person object in the ADDPERSON_QUEUE
QueueJob(person,ConfigurationManager.AppSettings.Get(ADDPERSON_QUEUE), messageLabel);
Also in the appconfig I added this:
<appSettings>
<!--EnableQueueing values: true, false-->
<add key="EnableQueueing" value="true"/>...
I check before Queuing whether the Queuing is enabled....


So this gives me enough flexibility to keep my system independent and not to worry about 
any legacy systems or any other systems that will use data from this application.
It simply Queues the task/job and do not worry about what will be done with the messages.
I also did not write any codes related to the Legacy application or its datastructure in my 
webapplication and kept it independent.



Web performance is reduced a bit.. because of Queuing but its better than pumping data on legacy
systems directly.
The app config also gives flexibility to stop queuing any time.

For QueueJob Method Provider pattern is also be used for flexibility on deciding storage of messages. Who knows
in the future something great can come up... and we can simply plug in a provider.... or probably someone
would not like to use MSMQ instead use SQL Server 2005 Messaging or something else...

 

Links to Microsoft Visual Studio Documentation and Related Resources….


Source MSDN Blogs: http://blogs.msdn.com/vsdocs/archive/2007/01/22/links-to-microsoft-visual-studio-documentation-and-related-resources.aspx

 

Links to Microsoft Visual Studio Documentation and Related Resources

Note: These links are subject to change at any time. 

Official Documentation for Visual Studio

MSDN Web Portals Related to Visual Studio

MSDN Forums Related to Visual Studio

Improving Reflection Performance

Some tips to keep in mind when using reflection….
Source MSDN: http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/default.aspx?loc=&side=true#a

Improving Reflection Performance

There are various reasons why an application can’t define a static extensibility contract, but it’s usually a result of the flexibility the architect built in. Here are some ways to reduce some of the cost of reflection and of the MemberInfo cache.

Avoid using Type.InvokeMember to call late-bound methods  Type.InvokeMember is the slowest way to invoke a member via reflection. If your scenario doesn’t involve COM interop invocation (something which Type.InvokeMember makes easier), consider other alternatives for setting up calls to members obtained through late-bound methods.

Avoid case-insensitive member lookups  When calling both the plural and non-plural GetXX APIs, avoid specifying BindingFlags.IgnoreCase as a binding flag argument as it has a significant invocation and working set costs. Metadata is case sensitive and to perform an “ignore case” operation, reflection needs to obtain the metadata string names for all members of a type and then do a case-insensitive comparison. In addition, due to the design of the reflection cache, a second MemberInfo cache is created for the case-insensitive members, which basically doubles an application’s working set.

Use BindingFlags.ExactMatch whenever possible  Specify this BindingFlag when calling MethodBase.Invoke if you know that the types of the arguments you are passing exactly match the types of the method’s parameters. It will allow reflection to go down an optimized, more efficient code path that does not perform argument type-coercion. If the argument types don’t match the method’s parameter types, an exception will be thrown.

Call the non-plural GetXX method if you know the name of the instance you care about (.NET Framework 2.0 only)  The MemberInfo cache is lazily populated in the .NET Framework 2.0, which means lower working set cost, and less time to retrieve the method. If you know the name of the particular method you want to obtain, use the non-plural GetXX method.

Cache only the handle to a member (.NET Framework 2.0 only)  In the .NET Framework 2.0, developers have the ability to define a cache policy themselves. The .NET Framework 2.0 introduces new APIs called the token handle resolution APIs. This set of APIs exposes some of the fundamental member identity concepts of the runtime and allows a user to set up and execute a MemberInfo caching policy on their own.


Use Reflection wisely and be aware of the list Costly functions vs the fast ones


Here is the list of Fast and Light Functions and Costly Functions. So when you write your next piece of codes be more careful to choose the right methods.

Fast and Light Functions
typeof
Object.GetType
typeof == Object.GetType
Type equivalence APIs (including typehandle operator overloads)
get_Module
get_MemberType
Some of the IsXX predicate APIs
New token/handle resolution APIs in the .NET Framework 2.0

Costly Functions
GetXX APIs (MethodInfo, PropertyInfo, FieldInfo, and so on)
GetCustomAttributes
Type.InvokeMember
Invoke APIs (MethodInfo.Invoke, FieldInfo.GetValue, and so on)
get_Name (Name property)
Activator.CreateInstance

CLS – Common Language Specification, consider making your code CLS compliant.

Making your code CLS compliant is important, as this confirms that the classes should only expose features that are common to the CLS. So anyone using your classes and writing in a different programming language do not face any issues.

To make sure, CLS compliancy is checked by the C# compiler we have to explicitly define the CLSCompliantAttribute.

Marking inside AssemblyInfo.cs class
===================================
[assembly: CLSCompliant(true)]

Marking an entire assembly as CLS compliant
=========================================
using System;
[assembly:CLSCompliant(true)]

Marking a particular method as CLS compliant
=========================================
[CLSCompliant(true)] 
public void MyMethod()

Remeber you have to be extra careful when you enable CLS compliancy else C# compiler to raise an error , here are some examples :

1. When your class methods that are identical but differ only by ‘out’ or ‘ref’.
2. When a publicly exposed member starts with an underscore (_).
3. When an abstract member is marked for non-CLS compliance within a CLS compliant class.

And we should stick to the following rules:
=======================================

1. Unsigned types should not be part of the public interface of the class. However unsigned types can be part of private members. 
2. Unsafe types like pointers should not be used with public members. However they can be used with private members. 
3. Class names and member names should not differ only based on their case. For example we cannot have two methods named TheMethod and THEMETHOD. 
4. Only properties and methods may be overloaded, Operators should not be overloaded.

Pageflakes take away feature

Like I took this sudoku flake here in this blog:




Choose your flake at www.pageflakes.com and go to edit and press Export button and this will give you a html similiar to this:
<script type=”text/javascript” src=”http://www.pageflakes.com/CFFlakeManager.js”></script><iframe allowtransparency=”true” id=”pffid29970474″ scrolling=”no” frameborder=”0″ hidefocus=”true” src=”http://www.pageflakes.com/SingleFlake.aspx?FlakeId=29970474” style=”border:none 0px #cccccc;margin:0px;width:320px;height:402px” onload=”if (typeof registerFlake == ‘function’) {registerFlake(this);}”></iframe>

Just Add this to your page/blog anywhere and you will see its working….
for further details you can check this blog post too….
http://www.pageflakes.com/Community/Help/Blog.aspx?ShowComment=1&BlogPost=34


How cool is that…..