Friday, September 15, 2006

Sql Server 2005 Notification Services and Remoted Service calls

I've recently have been working on a custom delivery protocol for notification services. I needed a way for sql server 2005 to call a remoted service when a trigger fired, to try and avoid polling the database for changes or using a SQLDependency that would need to be wired back up after each firing.

The delivery protocol takes 2 parameters from the instance config file: Service Contract Type (IServiceContract) and MethodName (The method to call).

On intialization I load the contract assembly in and reflect on the type to get the MethodInfo for later usage. It currently does not support overloaded methods and will only grab the first method match.

if (channelArgs.Count == 2)
{
string service = channelArgs["ServiceType"];
methodName = channelArgs["MethodName"];
Type t = TypeLoader.LoadType(service);

//I'm using windows security on my remoted object hosted in IIS, so setting the current principal is necessary.
Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
MethodInfo[] myMethodInfos = t.GetMethods();
foreach (MethodInfo Mi in myMethodInfos)
{
if (Mi.Name == methodName)
{
myMethodInfo = Mi;
//Store the parameters for later usage.
info = myMethodInfo.GetParameters();
break;
}
}
}
else
{
throw new ArgumentException(
"Inadequate number of arguments supplied.");
}

When a delivery notification comes in, I take the list of ProtocolFields and match them to the method parameters and invoke the method.

bool successfulDelivery = false;
Exception failureException = null;

try
{
foreach (NotificationHeaders header in headersList)
{
object[] param = null;
// Set the method parameters.
if (null != header.ProtocolFields)
{
param = new object[header.ProtocolFields.Count];
int idx = 0;
foreach (string protocolField in header.ProtocolFields.Values)
{
Type tp = info[idx].ParameterType;
object o = Convert.ChangeType(protocolField, tp);
param[idx] = o;
idx++;
}
}

object obj = ServiceFactory.CreateInstance(t, service);
myMethodInfo.Invoke(obj, param);
}
}
catch (Exception ex)
{
failureException = ex;
// Handle any exceptions here; for instance,
// write exception information to the event log.
}
finally
{
SendStatus(headersList, successfulDelivery,
body, failureException);
}

You will need to make sure RemotingServices.Configure has been called before the method is invoked. Also the ServiceFactory is a helper to extract the particulars of creating the object away from any client code to allow for future support of Windows Communication Foundation (WCF) or any other communication protocol. SendStatus returns the message status to Notification Services.

There is certainly some cleanup that needs to occur but the jist is there. This allows Sql Server 2005 to call back to any service when an event occurs in the database. Next I'll post the application config files to setup the service.

18 comments:

Anonymous said...

Deer hunter 2014 is definitely an incredible IOS
as well as Android mobile phone hunting activity
that will delivers actuality searching experience
to your mobile flatform. Deer Hunter 2014 seeks (pun intended) to help keep a person looking online game actually from the off-season.

You get an opportunity to personal weapons you might solely dream of and
can go destinations around the world.
This specific shopping simulator has remarkable artwork
which search great for the ios/android. The
vistas and also animals are incredibly very well made and
also the movements is water in addition to rapidly, specially for the more difficult quantities.
I perhaps enjoyed that on the ios/android along with overall performance
has been nonetheless very good.
http://carlislechief.com/deer-hunter-2014-cheats/

Also visit my homepage ... Deer hunter 2014 Cheats

Anonymous said...

I'm really inspired together with your writing talents and also with the
format to your blog. Is that this a paid topic or did you customize
it your self? Anyway keep up the excellent high quality writing, it is uncommon to see a great blog like this one nowadays..


Look at my blog post; download counter strike 1.6

Anonymous said...

celine luggage bags
I could not resist commenting. Perfectly written!

Anonymous said...

The Nike Free 5.0 imitates being barefoot.
The outsole is grooved so that each inch of the foot can bend as it naturally wants to do.
The outcome is a shoe which is a delight to use,
but one you are urged to ease into wearing. But being versatile doesn't suggest being sloppy.


Feel free to visit my blog post :: nike free run 2 cheap

Anonymous said...

Therefore he is risking everything he has worked so hard for by taking on the former champion. ufc 174 online
John Howard- Round One-Alves lands punches and kicks when the men are on their
feet. He brings strikes from all angles and moves in and out of his opponents
range well.

Anonymous said...

I do accept as true with all the ideas you have introduced on your post.
They are really convincing and can certainly work.
Nonetheless, the posts are very quick for starters.
May just you please lengthen them a little from next time?
Thank you for the post.

Also visit my web page shoes unique

Anonymous said...

This isn't economical promoting due to the connection to adwords sso where does that leave
us and what does thee future hold. Anoter way to adverrise foor free is
by message boards on the Internet that are public. If you're like many
entrepreneurs, your favorite answer too this
question is to advertise for free.

Check out my web page; free text marketing

Anonymous said...

celine bags for cheap

Greetings from California! I'm bored to tears at work so I decided to browse your site
on my iphone during lunch break. I enjoy the information you provide here and can't wait to take
a look when I get home. I'm shocked at how quick your blog loaded on my cell
phone .. I'm not even using WIFI, just 3G .. Anyhow, very good blog!

Anonymous said...

Have you ever thought about adding a little bit more
than just your articles? I mean, what you say is valuable and everything.
But just imagine if you added some great visuals or video clips to give your posts more, "pop"!
Your content is excellent but with pics and clips, this
website could certainly be one of the most beneficial
in its field. Excellent blog!

Also visit my weblog ... simpoints code generator

Anonymous said...

Hi there, You have done a great job. I will certainly digg it and
personally suggest to my friends. I am confident they will be benefited from this
web site.

Feel free to visit my website :: smooth muscle cells

Anonymous said...

I'm really impressed with your writing skills as
well as with the layout on your weblog. Is this a paid theme or did you customize it yourself?
Either way keep up the nice quality writing, it's rare to see a great blog like this
one today.

My homepage :: farmville 2 country escape hack (farmville2countryescapehack.tumblr.com)

Anonymous said...

Assoiffé,désuet Chine Contrairement à gronder au milieu et
comment pirater un compte facebook gratuitement -
facebook pirater (2013). promettre avec normalnose anje compte facebook bloqué téléphone.

Anonymous said...

Hey I know this is off topic but I was wondering if you knew of any widgets
I could add to my blog that automatically tweet my newest twitter updates.
I've been looking for a plug-in like this for quite
some time and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading your blog and I look
forward to your new updates.

Also visit my website minecraft games

Anonymous said...

Hello there, You've done an excellent job. I will certainly digg it
and personally recommend to my friends. I'm confident they will be
benefited from this website.

Also visit my blog post: tv series (tinyurl.com)

Anonymous said...

excellent puut up, very informative. I wonder why the opposite experts of this
sector don't notice this. You should profeed your writing. I'm sure, you've
a great readers' base already!

My web page: weight issues in youth

Anonymous said...

І dο not еven кnow the wayy ӏ finished սp heге, however I
thought this publsh wɑs gooɗ. I don't recognise who
yyou aгe hoաeveг certainly yoս ɑre goіng too a famous blogger in the event you
arеn't аlready. Cheers!

Look intο myy homepage; keurig coffee Pods printable coupons

Anonymous said...

I am truly glad to read this blog posts which consists of lots of valuable data, thanks for providing such statistics.


Feel free to visit my weblog - leather travel wallets, ,

Anonymous said...

goyard
kyrie 8
supreme hoodie
supreme outlet
bapesta shoes