473,395 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Technique for centralized web service authentication, logging, etc.

Hi everyone,

I would like to get your opinion on a technique I came up with when
faced with the problem of redundant code in every web method
(authentication, logging, exception handling). Normally, my web methods
would look something like this:

[WebMethod][SoapHeader("AuthHeader")]
public ReturnType GetSomeData(SomeType param1)
{
try
{
// Authentication

// Implementation
}
catch(Exception ex)
{
// Exception handling and logging
}
finally
{
// Request logging
}
}

[WebMethod][SoapHeader("AuthHeader")]
public ReturnTypeArray GetMoreData(Type1 param1, Type2 param2, Type3
param3)
{
try
{
// Authentication

// Implementation
}
catch(Exception ex)
{
// Exception handling and logging
}
finally
{
// Request logging
}
}

It gets rather tiresome to copy-paste all this non-implementation code,
never mind the fact that the DRY principle (Don't Repeat Yourself) is
violated. The only suggestions to avoid this that I have seen include
writing HTTP handlers or SOAP extensions. Yikes - very ugly, IMHO.

I have come up with a method that uses delegates coupled with a
centralized ExecuteRequest function that takes care of this
non-implementation functionality and works with any number of
parameters (FYI - in my case, web methods always return a value, but
I'm sure it wouldn't be too hard to make void methods work).

It reminds me of the Template Method pattern, except instead of
overridden methods, we have delegates to perform the work inside of a
method that provides the code template. Have a look:

private delegate ReturnType GetSomeDataImplDelegate(SomeType param1);
private delegate ReturnTypeArray GetMoreDataImplDelegate(Type1 param1,
Type2 param2, Type3 param3);
private MulticastDelegate _func;

[WebMethod][SoapHeader("AuthHeader")]
public ReturnType GetSomeData(SomeType param1)
{
_func = new GetSomeDataImplDelegate(GetSomeDataImpl);
return (ReturnType)ExecuteRequest(param1);
}

[WebMethod][SoapHeader("AuthHeader")]
public ReturnTypeArray GetMoreData(Type1 param1, Type2 param2, Type3
param3)
{
_func = new GetMoreDataImplDelegate(GetMoreDataImpl);
return (ReturnTypeArray)ExecuteRequest(param1, param2, param3);
}

private ReturnType GetSomeDataImpl(SomeType param1)
{
// Implementation
}

private ReturnTypeArray GetMoreDataImpl(Type1 param1, Type2 param2,
Type3 param3)
{
// Implementation
}

private object ExecuteRequest(params object[] parameters)
{
try
{
// Authentication

// Invoke the implementation function
return _func.DynamicInvoke(parameters);
}
catch(Exception ex)
{
// Exceptions thrown withing the implementation function are
// wrapped in TargetInvocationException. Unwrap before logging.
if (ex is System.Reflection.TargetInvocationException)
ex = ex.InnerException;

// Exception handling and logging
}
finally
{
// Request logging
}
}
What do you think? I don't believe I've seen this technique anywhere
else...

Aug 22 '06 #1
0 1334

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: michaeljohnsmith | last post by:
I am running Win XP SP2 and MSDE 2000 SP3a. The user is logging on as a POWERUSER. When the user logs on the MSSQLSERVER service starts but the SQL Server Service Manager, that runs in the system...
1
by: .net user | last post by:
can some one point me what i'm doing wrong? I have spent half a day figuring out and totally stuck now. Here's what I'm trying to accomplish: I am writing a web appl - an intranet portal site...
5
by: jqpdev | last post by:
Hello all... I'm coming from a Borland Delphi background. Delphi has a specific component called a Data Module. In the designer the Data Module behaves like a windows form. A developer can...
2
by: Kumar | last post by:
I have a VB 6 COM object that uses ADO 2.6 to connect to a database. I have a Web Service that references this COM object and calls a method in this objects that uses ADO 2.6 to connect. The Web...
4
by: Andy Baker | last post by:
I have an Windows forms application written in VB.NET that uses a SQL Server 2000 back end database with Windows authentication. There is no problem accessing the database from my application, or...
9
by: dana lees | last post by:
Hello, I am developing a C# asp.net application. I am using the authentication and authorization mechanism, which its timeout is set to 60 minutes. My application consists of 2 frames - a...
1
by: kelvlam | last post by:
Hello, I'm a brand new web service developer, so forgive me if my question have been asked before. I tried to search online and the only thread I can found in Google Group doesn't have any...
3
by: Brad | last post by:
I'm setting up my new pc with all my VS.net projects and I'm missing something.....something I've done many times before without problem. I have several asp.net apps accessing secure .net web...
3
by: james.p.news | last post by:
I'm new to Python, but I've been thrown into coding a pretty complicated regression testing script. I need to email the log of the daily test to the code owners. I thought I could use SMTPHandler...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.