How to query n web services

N

Nightfall

Dear friends,
let's say we have 10 "server-side" web service which return some kind
of data.

I'm using visual studio 2005 and I have a Project - the client -
where I added Web References to those 10 ws.
When I added web references, I wrote 10 different web references name.

So, i.e., I should write

Name01.Service service01 = new Name01.Service();
Name02.Service service02 = new Name02.Service();
Name03.Service service03 = new Name03.Service();
....
and then invoke service01.someMethod...

But let's say that I just have to query a subset of those 10 ws.
The subset is given by strings. (so that I have to invoke only those
ws whose names are specified in the following strings)

string s1 = "Name01";
string s4 = "Name04";
string s7 = "Name07";

I need some way to do

Name01.Service service01 = new Name01.Service();
Name04.Service service04 = new Name04.Service();
Name07.Service service07 = new Name07.Service();

which, certainly is not

s1.Service service01 = new s1.Service();

isn't it?

Thanx a lot for any hint.

bye!
 
L

Laurent Bugnion, MVP

Hi,
Dear friends,
let's say we have 10 "server-side" web service which return some kind
of data.

I'm using visual studio 2005 and I have a Project - the client -
where I added Web References to those 10 ws.
When I added web references, I wrote 10 different web references name.

So, i.e., I should write

Name01.Service service01 = new Name01.Service();
Name02.Service service02 = new Name02.Service();
Name03.Service service03 = new Name03.Service();
...
and then invoke service01.someMethod...

But let's say that I just have to query a subset of those 10 ws.
The subset is given by strings. (so that I have to invoke only those
ws whose names are specified in the following strings)

string s1 = "Name01";
string s4 = "Name04";
string s7 = "Name07";

I need some way to do

Name01.Service service01 = new Name01.Service();
Name04.Service service04 = new Name04.Service();
Name07.Service service07 = new Name07.Service();

which, certainly is not

s1.Service service01 = new s1.Service();

isn't it?

Thanx a lot for any hint.

bye!

Using Reflection, you can use a string to create an instance of an
object and then invoke methods on that instance. Check Reflection on Google.

Note that Reflection is slow, so don't use it for performance-critical
operations.

HTH,
Laurent
 
N

nano2k

Hi,




Dear friends,
let's say we have 10 "server-side" web service which return some kind
of data.
I'm using visual studio 2005 and I have a Project - the client -
where I added Web References to those 10 ws.
When I added web references, I wrote 10 different web references name.
So, i.e., I should write
Name01.Service service01 = new Name01.Service();
Name02.Service service02 = new Name02.Service();
Name03.Service service03 = new Name03.Service();
...
and then invoke service01.someMethod...
But let's say that I just have to query a subset of those 10 ws.
The subset is given by strings. (so that I have to invoke only those
ws whose names are specified in the following strings)
string s1 = "Name01";
string s4 = "Name04";
string s7 = "Name07";
I need some way to do
Name01.Service service01 = new Name01.Service();
Name04.Service service04 = new Name04.Service();
Name07.Service service07 = new Name07.Service();
which, certainly is not
s1.Service service01 = new s1.Service();
isn't it?
Thanx a lot for any hint.

Using Reflection, you can use a string to create an instance of an
object and then invoke methods on that instance. Check Reflection on Google.

Note that Reflection is slow, so don't use it for performance-critical
operations.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog:http://www.galasoft.ch
PhotoAlbum:http://www.galasoft.ch/pictures
Support children in Calcutta:http://www.calcutta-espoir.ch- Ascunde citatul -

- Afi are text în citat -

Hi
You may use a HashTable to establish associations between string
values and the proxy objects used to query the webservices.
Something like this:

Name01.Service service01 = new Name01.Service();
Name02.Service service02 = new Name02.Service();
Name03.Service service03 = new Name03.Service();

private HashTable m_htProxies = new HashTable(10);
m_htProxies["Name01"] = service01;
m_htProxies["Name02"] = service02;
....
m_htProxies["Name10"] = service10;

Then, you can query like this:
SoapHttpClientProtocol service = m_htProxies[name] as
SoapHttpClientProtocol;
You then need to call the methods using reflection. Except only if the
webservices are of the same type. In this case it's far more simple.
 
L

Laurent Bugnion, MVP

Hi,
Hi
You may use a HashTable to establish associations between string
values and the proxy objects used to query the webservices.
Something like this:

Name01.Service service01 = new Name01.Service();
Name02.Service service02 = new Name02.Service();
Name03.Service service03 = new Name03.Service();

private HashTable m_htProxies = new HashTable(10);
m_htProxies["Name01"] = service01;
m_htProxies["Name02"] = service02;
...
m_htProxies["Name10"] = service10;

But this requires that the developer knows the services in order to
store them in the hashtable. That might be the case, but they also may
be unknown (for edxample in a plug-in architecture), thus my proposal to
use reflection.
Then, you can query like this:
SoapHttpClientProtocol service = m_htProxies[name] as
SoapHttpClientProtocol;
You then need to call the methods using reflection. Except only if the
webservices are of the same type. In this case it's far more simple.

If the services are known, and the hashtable can be used, then it's
simpler to have them implement an interface (IMyService). This interface
defines a method (ExecuteQuery for example) common to all the services.
Then cast the services to that interface and you can safely call the
method. This is the best way to decouple definition and implementation,
and to lose the need to have all the services from the same type.
Software design 101 :)

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top