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("Aut hHeader")]
public ReturnType GetSomeData(Som eType param1)
{
try
{
// Authentication
// Implementation
}
catch(Exception ex)
{
// Exception handling and logging
}
finally
{
// Request logging
}
}
[WebMethod][SoapHeader("Aut hHeader")]
public ReturnTypeArray GetMoreData(Typ e1 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 GetSomeDataImpl Delegate(SomeTy pe param1);
private delegate ReturnTypeArray GetMoreDataImpl Delegate(Type1 param1,
Type2 param2, Type3 param3);
private MulticastDelega te _func;
[WebMethod][SoapHeader("Aut hHeader")]
public ReturnType GetSomeData(Som eType param1)
{
_func = new GetSomeDataImpl Delegate(GetSom eDataImpl);
return (ReturnType)Exe cuteRequest(par am1);
}
[WebMethod][SoapHeader("Aut hHeader")]
public ReturnTypeArray GetMoreData(Typ e1 param1, Type2 param2, Type3
param3)
{
_func = new GetMoreDataImpl Delegate(GetMor eDataImpl);
return (ReturnTypeArra y)ExecuteReques t(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.DynamicIn voke(parameters );
}
catch(Exception ex)
{
// Exceptions thrown withing the implementation function are
// wrapped in TargetInvocatio nException. Unwrap before logging.
if (ex is System.Reflecti on.TargetInvoca tionException)
ex = ex.InnerExcepti on;
// Exception handling and logging
}
finally
{
// Request logging
}
}
What do you think? I don't believe I've seen this technique anywhere
else... 0 1364 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 tray, shows
that the instance has not started. The field that displays the
instance name is blank and it is not listed in the drop down list.
The MSSQLSERVER service is set to start with a Windows domain
administrator account. The SQL server...
|
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 (based on the portal starter kit) and I want to apply role based
security to the site. - When the users type in the intranet URL, a windows
logon window would pop up and then users login with their NT credentials.
Based upon theie level of security...
|
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 drop non-visual (controls) on the data
module surface and wire them up and create procedures, functions, event
procedures. In the source file (code behind file) the Data Module is a
class, and the dropped components are public properties. The...
|
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 Service gives the error
"connectedSQL Server does not exist
or access denied."
I am using Windows Integrated Security. The connection string looks like
this: "Provider=SQLOLEDB; Data Source=servername;Initial
|
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 from an installed version of
the application on another machine. My problem is that I would like to
access the database overnight unattended to perform some maintenance tasks.
With my fairly limited knowledge of SQL Server and VB.NET, I thought...
| |
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 header frame and a main frame.
When i enter the application, i see the login screen on the whole screen,
but when the authentication expires, the login screen appears on both
frames.
|
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 answer/resolution.
This is the only one I'm able to find so far:
http://groups.google.com/group/microsoft.public.dotnet.framework.webservices/browse_thread/thread/8ae08c100682b774/7efb09202802d00a
I'm trying to integrate with a 3rd party vendor. ...
|
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 services (secure meaning
windows authentication credentials must be used to access the web services).
The problem is that when the web apps attempt to access a web service they
are receiving 401: unathorized.
- The apps are all running on web...
|
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 for
this, but our email system requires authentication. I have not been
able to figure out how to log in using SMTPHandler. I found the
following post on comp.lang.python:
...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |