我正在寻找一种方法,让为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
}
但是,自定义类型(例如
CustomSerializableType
有没有什么方法可以让生成的代理类引用这些类型,或者我是不是搞错了?我应该考虑将web服务转换为WCF服务吗?
细节
我们的解决方案包括以下四个项目:
-
-
公共库(包含公共功能,包括
CustomSerializableType
)
-
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
}
}