Sunday, February 19, 2012

HelloWorldCS Sample

Is anyone is familiar with the HelloWorldCS solution that is available for download at www.SQLServiceBroker.com?

If so, can someone tell me what this method does?
-
private void BuildCallbackMap()
{
Type t = GetType();
//this member variable is for retrieving a collection of key/value pairs
//BrokerMethodAttribute is a base class for all custom attributes
m_dispatchMap = new Dictionary<BrokerMethodAttribute, MethodInfo>();
//get all public methods of Instance type...
MethodInfo[] methodInfoArray = t.GetMethods(BindingFlags.Public | BindingFlags.Instance);

//iterate through the collection...
foreach (MethodInfo methodInfo in methodInfoArray)
{
object[] attributes = methodInfo.GetCustomAttributes(typeof(BrokerMethodAttribute), true);
foreach (BrokerMethodAttribute statefulTransition in attributes)
{
BrokerMethodAttribute statelessTransition =
new BrokerMethodAttribute(statefulTransition.Contract, statefulTransition.MessageType);
if (m_dispatchMap.ContainsKey(statefulTransition) || m_dispatchMap.ContainsKey(statelessTransition))
{
string exceptionMessage = "Method '" + methodInfo.Name +
"' redefines a handler for message type '" + statefulTransition.MessageType + "'";
if (statefulTransition.State != -1)
exceptionMessage += " in state " + statefulTransition.State;

throw new NotSupportedException(exceptionMessage);
}
m_dispatchMap[statefulTransition] = methodInfo;
}
}
}

-
Sorry about the TERRIBLE formatting.

I'm just a little confused as to what this is doing.

thanks.

Doug

Service class is a general class designed to provide all needs of service handling. If you want to use it for particular service you need to create a new class inherited from Service (as it is done in HelloWorldCS) and add handler methods that will be called upon receiving specific message type on a specific contract.
To accomplish this task (calling handler methods) Service class uses its own attribute, BrokerMethodAttribute. The attibute is designed to mark these handlers so that Service can find out what method it should call for pair (contract, message type).
So we came to your question: BuildCallbackMap() is the method that uses reflection to parse its own class (derived from Service) for methods that have BrokerMethodAttribute and builds a map (contract, message type, service state) -> (method that handles that stuff) for them so that it would be easier to find a correct handler later. If it finds two methods that are marked to handle the same thing - throws an exception.
Please note that your implementation of Service may use a service state (just a property of Service) to process messages (calling different handlers) depending on service state's value.

Hope it will help you :)
|||

Ok that sheds some light on the problem.

However, I still am confused on this bit:
-
foreach (MethodInfo methodInfo in methodInfoArray)
{
object[] attributes = methodInfo.GetCustomAttributes(typeof(BrokerMethodAttribute), true);
foreach (BrokerMethodAttribute statefulTransition in attributes)
{
BrokerMethodAttribute statelessTransition = new BrokerMethodAttribute(statefulTransition.Contract, statefulTransition.MessageType);
if (m_dispatchMap.ContainsKey(statefulTransition) || m_dispatchMap.ContainsKey(statelessTransition))
{
string exceptionMessage = "Method '" + methodInfo.Name +
"' redefines a handler for message type '" + statefulTransition.MessageType + "'";
if (statefulTransition.State != -1)
exceptionMessage += " in state " + statefulTransition.State;

throw new NotSupportedException(exceptionMessage);
}
m_dispatchMap[statefulTransition] = methodInfo;
}
}

1). I understand the foreach() statment.
2). This line is not clear:

object[] attributes = methodInfo.GetCustomAttributes(typeof(BrokerMethodAttribute), true);
3). This loop is unclear:

foreach (BrokerMethodAttribute statefulTransition in attributes)
{}

4). This line is unclear:

BrokerMethodAttribute statelessTransition = new BrokerMethodAttribute(statefulTransition.Contract, statefulTransition.MessageType);

5). And, if all of that weren't enough...This is confusing as well:

if (m_dispatchMap.ContainsKey(statefulTransition) || m_dispatchMap.ContainsKey(statelessTransition))

6). What is a stateful/stateless transition?

7). Is someone publishing some help files, tutorials or books on this subject soon?

...Suffice to say, the whole Service Broker thing is kinda kicking me in the head at this point...

Thanks for your help!

God Bless...

Doug

|||

drdexter33 wrote:

1). I understand the foreach() statment.


good :). Just to make it clear: we are walking through each public instance (not static) method (stored in variable methodInfo) of our derived-from-Service class.

drdexter33 wrote:

2). This line is not clear:
object[] attributes = methodInfo.GetCustomAttributes(typeof(BrokerMethodAttribute), true);


GetCustomAttributes returns method's attributes of the specified type, in our case we get all BrokerMethodAttribute attributes of the method: BrokerMethodAttribute allows multiple instances of itself for each method that means that single method can handle several message types/contracts/service states.

drdexter33 wrote:

3). This loop is unclear:
foreach (BrokerMethodAttribute statefulTransition in attributes)
{}


So... we got an array of BrokerMethodAttribute attributes, and we are looping through each of them (stored in variable statefulTransition).

drdexter33 wrote:

6). What is a stateful/stateless transition?


I put this question here because I guess it confuses you mostly. As I mentioned in the previous post you can use or not use the state of the service. By using the state I mean that if you need your service to react on the same message types differently depending on lets say receiving some special messages or something like that than you may find service's state useful. To react differently depending on service state you can mark you message handler methods with BrokerMethodAttribute specifying not only the message type and contract it handles, but also the state of the service for which it it should be called. So that now you can provide different handlers for the same message type and contract for different states.
So... statefulTransition means that this attibute may have a state for which it should be active, while statelessTransition declares the same message type/contract, but doesn't have a defined state for sure.

drdexter33 wrote:

4). This line is unclear:
BrokerMethodAttribute statelessTransition = new BrokerMethodAttribute(statefulTransition.Contract, statefulTransition.MessageType);


Here we just create a "copy" of statefulTransition removing information about state.

drdexter33 wrote:

5). And, if all of that weren't enough...This is confusing as well:
if (m_dispatchMap.ContainsKey(statefulTransition) || m_dispatchMap.ContainsKey(statelessTransition))


OK, finally we came to the map: one of the features we have to guarantee is that in any case there should be only one handler of the message. So this line checks for existence of other methods (already included in the map) that are marked to handle the same message type/contract/state as current method. If we have a defined state in the attribute then: map shouldn't contain an attribute with the same message type/contract/state, and also it shouldn't contain "stateless" attribute that is marked to handle all states for message type/contract. That's why the map is tested twice for statefulTransition and statelessTransition.

drdexter33 wrote:

7). Is someone publishing some help files, tutorials or books on this subject soon?


Don't know, I'm just a programmer (actually software architector :) ), and I just looked through HelloWorldCS earlier.

|||There's a lot of Service Broker information available in Books on Line. In addition, Rushi maintains a blog with tips, samples, and techniques:

http://blogs.msdn.com/rushidesai/

There are some white papers around:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/html/sql2k5_SrvBrk.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/html/sql2k5_SrvBrk.asp
There is an independant forum

http://www.sqlservicebroker.com/forums/

and there is at least one Service Broker book available:

http://www.amazon.com/gp/product/1932577203/002-7167452-4906463?v=glance&n=283155&n=507846&s=books&v=glance

No comments:

Post a Comment