locked
Factory class for proxies RRS feed

  • Question

  •  

    My project consumes 2 different services and I have a helper class as follows

    public ServiceHelper{

    public const WebService1.ServiceClient  Service1Client = new WebService1.ServiceClient();

    public const WebService2.ServiceClient  Service2Client = new WebService2.ServiceClient();

     

    public object GetServiceClient(int id)

    {

         switch(id)

    {

    case 1:

    return Service1Client;

    case 2:

    return Service2Client;

    }

    }

    }

     

    Now I have a function MethodA(int id)

    {

    var client = ServiceHelper.GetServiceClient(id);

    }

    How do I typecast this var to the correct Proxy? or how can I return ServiceClient from GetServiceClient??

    Monday, October 18, 2010 3:21 AM

Answers

  • Hi Xaria,

    if both Webservices implement the same contract (only then they are interchangeable, from a design perspective), you could return the contract interface instead of the proxy class:


    public IFoo GetServiceClient(int id)
    {
     switch (id)
     {
      case 1:
       return Service1Client;
      case 2:  
        return Service2Client;
     }
    }


    [...]


    void MethodA(int id)
    {

     IFoo client = ServiceHelper.GetServiceClient(id);
     client.MethodA();
    }

    HTH,

    Markus

    Wednesday, October 20, 2010 10:11 AM
  • Hi,

    I used Chanelfactory instead of adding a service reference and as Markus mentioned, The services implement the same contract.

    So instead of returning a client, the interface is returned.

    The id is used an identifier to call the respective service.

    The problem is resolved now, thanks.

    Wednesday, October 20, 2010 10:24 AM

All replies

  • Hi,

    You may not do that easliy since these two ServiceClient class may not derive from the same base class. What's the purpose of using this helper class? What's the id?

    One possible solution is to check the type of client and explictly convert it to the correct type but I'm not sure why it's needed to use the helper class as it brings trouble to you.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Windows Azure Platform China Blog: http://blogs.msdn.com/azchina/default.aspx
    Wednesday, October 20, 2010 9:55 AM
  • Hi Xaria,

    if both Webservices implement the same contract (only then they are interchangeable, from a design perspective), you could return the contract interface instead of the proxy class:


    public IFoo GetServiceClient(int id)
    {
     switch (id)
     {
      case 1:
       return Service1Client;
      case 2:  
        return Service2Client;
     }
    }


    [...]


    void MethodA(int id)
    {

     IFoo client = ServiceHelper.GetServiceClient(id);
     client.MethodA();
    }

    HTH,

    Markus

    Wednesday, October 20, 2010 10:11 AM
  • Hi,

    I used Chanelfactory instead of adding a service reference and as Markus mentioned, The services implement the same contract.

    So instead of returning a client, the interface is returned.

    The id is used an identifier to call the respective service.

    The problem is resolved now, thanks.

    Wednesday, October 20, 2010 10:24 AM