Search This Blog

C#/.Net : Microsoft Messaging Queue (MSMQ) - 1

MSMQ Creation:-
 
When two applications (may be web/desktop/windows services) need to communicate with each other, we need some 'data bank' through which all applications can share messages. Microsoft solution for this is:MSMQ.
MSMQ is a technology, which helps different applications to communicate with each other in reliable, secure and guranteed way.

Two broad categories of MSMQ are :
(1) Transacational Queue (MSMQ)
(2) Non-Transaction Queue (MSMQ)

At time of queue creation, it is decided that Whether a queue is going to be transaction or non-transactional.


Security wise, there are two categories of MSMQ
(1) Public Queue
(2) Private Queue


In today's posting we are going to discuss creation of MSMQ queue, programatically. Later in tomorrow and day after tomorrow we will discuss how to share messages through MSMQ:

Microsoft provides System.Messaging namespace for MSMQ operations. But please note that this is a wrapper on COM [atleast I believe so, because while testing I used to get so many COM errors :-)   ]


MessageQueue is our class which provides different method, its static method Create is used to create a new MSMQ programatically.

MessageQueue.Create("MSMQ Queue Path", true);
/*first parameter is path of queue, where you want to create it, and second parameter:
true = Transactional Queue
false = Non-Transactional Queue*/

Simple ? :)

Now what is queue path?? (MSMQ Queue Path)
Public queue can be created on following path:
    COMPUTER_NAME\QUEUE_NAME
Wherease Private Queue can be created on following path:
    COMPUTER_NAME\PRIVATE$\QUEUE_NAME

While choosing MSMQ queue path consider following:
(1) MSMQ Queue name is NOT case-senstive, hence code4coder and CODE4CODER are considered name of same queue.
(2) MSMQ queue name (not path) should not be more than 124 unicode characters, and using more than 64 characters for a public queue name causes performance issues.
(3) Invalid characters for queue depends on MSMQ version.

e.g. Invalid characters for :
MSMQ 1.0 :   \ (=backslash)  ; (=semicolon) CR LF
MSMQ 2.0 and later versions : \ (=backslash)  ; (=semicolon) CR LF + (=plus) , (=comma) " (=double quotes)~


Comments and suggestions are welcomed....

Reflection,System.Reflection,Creating objects dynamically trhough reflection, code4coder.com,code4coder, c#,asp.net,vb.net,pl/sql,advanced features, code, coder, coders, reflection,system.reflection,System,using System.Reflection,Assembly,ConstructorInfo,Object,MethodInfo,Convert.ToString(),asp,slq,optimized code,email,Cell Phone, Mobile Phone, Coding, Code,using System, using System.Collections, using System.Net, Best practices, Problems, C#, .Net, . Net, C# .Net, C#.Net, VC, VC#,Threads,Trim(),client, server, help, HP, Application, Software Engineer,MSMQ, Queue creation, MSMQ, Microsoft Messaging Queue, Public queue, private queue, queue path, que path,

Reflection - Creating Objects Dynamically

Lets use Reflection ...

First question is why to use reflection, is there any scenario in which you need reflection ?

Okey let me try to explain from my previous project.

Scenario:-
I was working on a project; sometime back, and client needed one generic application; that could handle different messages from MSMQ (Microsoft Messaging Queue). Based on message type; totally different functionality needed to be executed. Further they did not want to touch one part when there was change in other part. Each functionality needed to be implemented in different assembly (or i would say dll in layman term)

There were many possible solutions; but the solution with which we came up finally; was use of reflection.
A single MSMQ Manager (application/windows service) was looking for new messages in MSQM, with each new message in queue, a new thread was created wwhich was calling service class(es) on the basis of configured data for that message type (all using reflection)
using C#.

Each message type data was configured in Db-Table/XML-File; So while deploying new message types, there was no need to stop the current application.

Code: (In my favourite C#)
Lets come to real part, i.e. how to create and use objects dynamically:

(1) To create objects dynamically, first we need to load assembly
(2) Create constructor info object, and from that create instance of class
(3) Call function/sub-routine of class.


Note : We need System.Reflection namespace in our code.

//Load Assembly:-
Assembly asm = Assembly.Load(@"C:\CODE4CODER.COM\myAssembly.dll");

Type targetClassType = asm.GetType("FullyQualifiedNameOfClass"); /*Fully qualified name of class like Code4Coder.com.myClass */

Type[] argTypes = Type.EmptyTypes;
ConstructorInfo cInfo = targetClassType.GetConstructor(argTypes);

//Create instance
Object objTargetClass = cInfo.Invoke(new object(), argTypes);

//Call any method of object:
//==>Say method name is getCode4CoderMethod, no parameters in function, and it returns String:
MethodInfo mInfo = targetClassType.GetMethod("getCode4CoderMethod"); //Method Name
string s = Convert.ToString( mInfo.Invoke(objTargetClass, argTypes)); /*First parameter is object for which this method is called, and second parameter is "array of parameters passed to the method"*/


Comments/suggestions are welcomed.

Reflection,System.Reflection,Creating objects dynamically trhough reflection, code4coder.com,code4coder,c#,asp.net,vb.net,pl/sql,advanced features, code, coder, coders, reflection,system.reflection,System,using System.Reflection,Assembly,ConstructorInfo,Object,MethodInfo,Convert.ToString(),asp,slq,optimized code,email,sqlldr,Invoke,Type,Type[]

Reflection,System.Reflection,Creating objects dynamically trhough reflection, code4coder.com,code4coder, c#,asp.net,vb.net,pl/sql,advanced features, code, coder, coders, reflection,system.reflection,System,using System.Reflection,Assembly,ConstructorInfo,Object,MethodInfo,Convert.ToString(),asp,slq,optimized code,email,sqlldr,Invoke,Type,Type[],Ajax,Cell Phone, Mobile Phone, Coding, Code,using System, using System.Collections, using System.Net, Constructor, Destructor, Object oriented programming, ebook, learning, UMT, code, freak, programmer, programmer's heaven, sql, optimization query, best SQL query writing, Query SQL, Best practices, Problems, Issues, C#, .Net, . Net, C# .Net, C#.Net, VC, VC#,Threads,Multithreading,Trim(),javascript, jScript, JavaScript, jQuery, client site, server side, client, server, group by, order by, max() over, count() over, dense_rank(), rank(), dense_rank, rank, Email, SQLLDR,C# help, help, coding help, HP, Application, Software Engineer

Sending Email using ORACLE procedure/package

If developers are working on large batch-processes of Oracle, then one common issue is 'alert' in case of any discrepancy.



Sending email through Oracle is very easy as you can do it through oracle's supplied package: utl_tcp


Note: Following code is without exception handling, which mean that we are not checking status of each TCP command that we are issuing through Oracle package. For this I will write sometimes latter. Assuming that everything will go fine:


This code is pretty much self explanatory.





CREATE OR REPLACE PROCEDURE send_simple_email(
          sender VARCHAR2,
          recipient VARCHAR2,
          message VARCHAR2
)
IS
          mailhost          VARCHAR2(300) := 'smtp.code4coder.com'; --Change it with your SMTP server.

          smtp_error     EXCEPTION;
          mail_conn      utl_tcp.connection;
          val                 PLS_INTEGER;

BEGIN
          mail_conn := utl_tcp.open_connection(remote_host => mailhost, remote_port => 25, charset => 'US7ASCII');
          val := utl_tcp.write_line(mail_conn, 'HELO ' || mailhost);
          val := utl_tcp.write_line(mail_conn, 'MAIL FROM: ' || sender);
          val := utl_tcp.write_line(mail_conn, 'RCPT TO: ' || recipient);
          val := utl_tcp.write_line(mail_conn, 'DATA');
          val := utl_tcp.write_line(mail_conn, message);
          val := utl_tcp.write_line(mail_conn, 'QUIT');
          utl_tcp.close_connection(mail_conn);

EXCEPTION
          WHEN OTHERS THEN
                    utl_tcp.close_connection(mail_conn);

END send_simple_email;

/

And to Call this procedure:



DECLARE
          SENDER VARCHAR2(200);
          RECIPIENT VARCHAR2(200);
          MESSAGE VARCHAR2(200);

BEGIN
          SENDER := 'email_through_oracle@coder4coder.com';
          RECIPIENT := 'everyOne@code4coder.com';
          MESSAGE := 'On Code4Coder.Com you will find different solutions/tips/tricks for better code. Targetted areas are ASP.Net, C#, VB.Net, PL/SQL, TSQL.';
     
          PROMO.SEND_SIMPLE_EMAIL ( SENDER, RECIPIENT, MESSAGE );

          COMMIT;

END;

PS: On Code4Coder.Com, we share best practices/codes snippets which are used in daily routine. Targeted technology areas are c#, Asp.net, VB.Net, Oracle, PL/SQL