Implementing custom interfaces

H

Hari

Hi,

I have created a web service which implements a custom interface.

I have added this web service as a web reference to a console application
that acts as a web service consumer.

After creating an instance for the web service class, when i am trying to
type cast that instance to my custom interface, i am getting
"InvalidTypeCastException" ?

When I do the same for System Interfaces like IDisposable it is working fine.

Can somebody help me with this ?

Thanks in Advance

Regards
Hari
 
F

FoxtrotEcho

When you added a web reference to your project, the framework created a class
that derives from SoapHttpClientProtocol (which implements IDisposable). You
can see this yourself if you view all files and naviagte down the web
reference tree until to come to a file named Reference.cs. The class
implemented in this file will not have derived from your interface even if
the methods are clearly there.
If you need that functionality,i.e. to have your class assume the behaviour
of an interface , it is best that create another class in your project and
derive it from the class implemented in Reference.cs and your interface- No
additional code would be required because the class in Reference.cs already
takes care of all implementations.
Now from your console app, instantiate this newly created class instead of
the one created by the framework and extract your interface as required.

ex
public class MyProxy : Service, IMyInterface {
//other customizations here if required.
}

where service is the SoapHttpClientProtocol derived class in Reference.cs
and IMyinterface is your custom interface.

In console app

MyProxy p = new MyProxy();
IMyInterface i = p as IMyInterface;
if (p!=null) //and is should not
{
//use i
}



You could achieve the same result by directly tampering with the framework
generated class, but everytime you update the web reference, you would lose
your customisations.
Hope this helped.
 
H

Hari

Hi,

Thanks for the reply.

That gave us some work around over the problem

Regards
Hari
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top