Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Service proxy class to implement interface

I am looking for a way to have the generated proxy class for a Web Reference (not WCF) implement a common interface in order to easily switch between web service access and "direct" access to our business layer in the client application, something like:

public IBusiness GetBusinessObject()
{
  if (_mode = "remote")
    return new BusinessWebService.Business(); // access through web service proxy class
  else
    return new Business(); // direct access
}

However, custom types (e.g. the CustomSerializableType in the examples below) aren't referenced in the generated proxy class. Instead new, identical types are generated, which makes it impossible for the proxy class to implement the interface.

Is there some way to make the generated proxy class reference these types, or am I going about this all wrong? Should I consider converting the web service to a WCF service instead?


Details

Our solution consists of these four projects:

  • A business library (contains business logic, accesses data store)
  • A common library (contains common functionality, including the CustomSerializableType)
  • A web service (acts as a proxy between remote clients and the business layer)
  • A windows application

Our client wants the windows application to be able to run in two different modes:

  • Local mode, where the application simply uses the business library directly to access data
  • Remote mode, where the application communicates with the web service to access data

In order to do this, we have created an interface, IBusiness, which is located in the common library and contains all business methods.

Interface

public interface IBusiness
{
  CustomSerializableType DoSomeWork();
}

Business layer

public class Business : IBusiness
{
  public CustomSerializableType DoSomeWork()
  {
    // access data store
  }
}

Web service

public class WebServiceBusiness : IBusiness
{
  private Business _business = new Business();

  [WebMethod]
  public CustomSerializableType DoSomeWork()
  {
    return _business.DoSomeWork();
  }
}

Generated proxy class (a ton of code left out for readability)

public partial class Business
  : System.Web.Services.Protocols.SoapHttpClientProtocol
{

  public CustomSerializableType DoSomeWork()
  {
    // ...
  }

  public partial class CustomSerializableType {
    // PROBLEM: this new type is referenced, instead of the
    // type in the common library
  }
}
like image 343
bernhof Avatar asked Sep 28 '10 17:09

bernhof


People also ask

What is proxy class in Web service?

A proxy class maps parameters to XML elements and then sends the SOAP messages over a network. In this way, the proxy class frees you from having to communicate with the Web service at the SOAP level and allows you to invoke Web service methods in any development environment that supports SOAP and Web service proxies.

How do you create a proxy class from a Web service?

After creating the directory we will write a Proxy class by “wsdl” command on a specified location. Just write wsdl and paste your URL that was copied from the web browser of the web service file . asmx and press Enter to create it. It will create a Proxy class on the selected location.


1 Answers

Assuming that the default namespace for your client is "Client", and that your web reference is named "Proxy", then do the following;

  1. In the root of your client project, create a folder named "Proxy".
  2. In that folder, create a class named "Business".
  3. Make that class public and partial, and have it implement your IBusiness interface

This way, you do not need to modify the Reference.cs. You should never modify Reference.cs, or any other file produced through code generation.

Note that this violates the principals of SOA by tightly binding your client to your service. At the very least, you should define those interfaces in a separate project, so that you are only sharing the "interface" project between the client and service.

like image 169
John Saunders Avatar answered Nov 03 '22 11:11

John Saunders