Question

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
  }
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top