Implementing custom interfaces

Discussion in 'ASP .Net Web Services' started by Hari, Mar 3, 2006.

  1. Hari

    Hari Guest

    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
     
    Hari, Mar 3, 2006
    #1
    1. Advertising

  2. Hari

    FoxtrotEcho Guest

    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.



    "Hari" wrote:

    > 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
     
    FoxtrotEcho, Mar 7, 2006
    #2
    1. Advertising

  3. Hari

    Hari Guest

    Hi,

    Thanks for the reply.

    That gave us some work around over the problem

    Regards
    Hari

    "FoxtrotEcho" wrote:

    > 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.
    >
    >
    >
    > "Hari" wrote:
    >
    > > 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
     
    Hari, Mar 7, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James D Carroll

    Implementing "package" interfaces

    James D Carroll, Jun 28, 2004, in forum: Java
    Replies:
    4
    Views:
    482
    Chris Smith
    Jun 28, 2004
  2. Chris
    Replies:
    6
    Views:
    527
    Chris Uppal
    Nov 7, 2004
  3. Oliver Wong
    Replies:
    16
    Views:
    634
    Oliver Wong
    Jun 28, 2006
  4. josh
    Replies:
    6
    Views:
    463
    Ed Kirwan
    Dec 19, 2006
  5. Replies:
    13
    Views:
    776
    Dave Rahardja
    Feb 2, 2007
Loading...

Share This Page