Web Service代理类实现接口

发布于 2024-09-24 23:26:01 字数 1827 浏览 5 评论 0原文

我正在寻找一种方法,让为 Web 引用(不是 WCF)生成的代理类实现一个通用接口,以便在 Web 服务访问和“直接”访问客户端应用程序中的业务层之间轻松切换,例如:

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

但是,生成的代理类中不会引用自定义类型(例如下面示例中的 CustomSerializedType)。相反,会生成新的相同类型,这使得代理类无法实现该接口。

是否有某种方法可以使生成的代理类引用这些类型,或者我是否认为这一切都是错误的?我是否应该考虑将 Web 服务转换为 WCF 服务?


详细信息

我们的解决方案由以下四个项目组成:

  • 业务库(包含业务逻辑,访问数据存储)
  • 公共库(包含通用功能,包括 CustomSerializedType
  • Web 服务(充当远程客户端和业务层之间的代理)
  • Windows 应用程序

我们的客户希望 Windows 应用程序能够以两种不同的模式运行:

  • 本地模式,应用程序直接使用业务库来访问数据
  • 远程模式,应用程序与 Web 服务通信来访问数据

为此,我们创建了一个接口 IBusiness,它位于公共库中包含所有业务方法。

接口

public interface IBusiness
{
  CustomSerializableType DoSomeWork();
}

业务层

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

Web服务

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

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

生成的代理类(为了可读性省略了大量代码)

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
  }
}

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
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花浅忆 2024-10-01 23:26:01

假设您的客户端的默认命名空间是“Client”,并且您的 Web 引用名为“Proxy”,则执行以下操作;

  1. 在客户端项目的根目录中,创建一个名为“Proxy”的文件夹。
  2. 在该文件夹中,创建一个名为“Business”的类。
  3. 将该类设为公共和部分类,并让它实现您的 IBusiness 接口。

这样,您就不需要修改 Reference.cs。您永远不应该修改 Reference.cs 或通过代码生成生成的任何其他文件。

请注意,这将您的客户端与您的服务紧密绑定,从而违反了 SOA 的原则。至少,您应该在单独的项目中定义这些接口,以便您仅在客户端和服务之间共享“接口”项目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文