【发布时间】:2017-08-31 10:26:24
【问题描述】:
我正在尝试为两个具有不同名称空间但包含具有相同名称和定义的类的两个 Web 服务编写“包装器”代码。我想编写一些函数代码,可以在每个 Web 服务的包装类中使用,而无需简单地将代码复制到每个 Web 服务中。例如,通过每个对象的名称返回一个对象:
using WebSvc1;
WrapperClassWS1
{
// The root class inherits from System.Web.Services.Protocols.SoapHttpClientProtocol
WebSvc1Root _Root = new WebSvc1Root;
public MyNonWSobj GetObjectByName(string WhichName)
{
// Uses GetWS1ObjectByName to retrieve the 'real' object
// then turns that into my dumbed down object for use
// by calling code that knows nothing about web services
// Identical code to WrapperClassWS2
MyNonWSobj myObj = new MyNonWSobj();
CommonObj returnedObj = this.GetWS1ObjectByName(string WhichName);
myObj.Name = returnedObj.Name;
myObj.Revision = returnedObj.Revision;
myObj.Description = returnedObj.Description;
return myObj;
}
private CommonObj GetWSObjectByName(string WhichName)
{
// Identical code to WrapperClassWS2
// This function will get an object native to the WebSvc1Root and
// the retrieved object will have identical name/definition to
// one from WebSvc2Root
return _Root.getObject(WhichName);
}
}
using WebSvc2;
WrapperClassWS2
{
// The root class inherits from System.Web.Services.Protocols.SoapHttpClientProtocol
WebSvc2Root _Root = new WebSvc2Root;
public MyNonWSobj GetObjectByName(string WhichName)
{
// Uses GetWS2ObjectByName to retrieve the 'real' object
// then turns that into my dumbed down object for use
// by calling code that knows nothing about web services
// Identical code to WrapperClassWS2
MyNonWSobj myObj = new MyNonWSobj();
CommonObj returnedObj = this.GetWS1ObjectByName(string WhichName);
myObj.Name = returnedObj.Name;
myObj.Revision = returnedObj.Revision;
myObj.Description = returnedObj.Description;
return myObj;
}
private CommonObj GetWSObjectByName(string WhichName)
{
// Identical code to WrapperClassWS1
// This function will get an object native to the WebSvc2Root and
// the retrieved object will have identical name/definition to
// one from WebSvc1Root
return _Root.getObject(WhichName);
}
}
EDIT2: 从每个 Web 服务返回 CommonObj 类型的函数不需要在两个包装类中以不同的方式命名。由于 _Root 变量在那里初始化,GetWSObjectByName 代码不完全相同。修复了上面的代码。
EDIT1:修正了以下段落中的错误并在上面添加了示例代码。
在我的实际案例中,CommonObj 类的名称相同,并且在两个 Web 服务名称空间中具有相同的定义。获取 Web 服务对象的函数具有相同的名称和定义,但包含“获取 Web 服务对象”函数的类定义具有不同的名称。我真的希望 GetObjectByName 和 GetWSObjectByName 各有一个定义。
我考虑过只使用一个类并为每个 Web 服务的对象定义模块级变量,然后将它们设置为特定于命名空间的对象。这可能会使大部分代码保持通用,但我还没有尝试过。 (我承认这种疯狂的方法植根于 VB6 经验,这是一种相当普遍的做法)。
为了确保我很清楚:我已经为第一个 Web 服务编写了工作代码。通过简单地将所有代码从第一个包装类复制到一个新类,然后更改“使用”语句、类名和表示 Web 服务的模块级变量的类型,我可以为第二个 Web 服务提供相同的工作代码根类。这将解决今天的问题,同时为明天的问题做好准备。
编辑:我的 _Root2 变量的数据类型有误。
编辑:继续寻找清晰...我正在尝试将现有的 Web 服务“包装”在一些代码中,我可以在调用代码/项目时使用这些代码/项目,这些代码/项目将不了解或引用 Web 服务。
【问题讨论】:
-
你的 wsdl 到 poco 生成器是否生成部分类?
-
我发现您的问题不清楚,没有一个好的minimal reproducible example 可以清楚地说明您的情况。私有
Get...ObjectByName()方法实际上在做什么?命名空间只是组织类型的一种方式;如果您的每个实现中的代码实际上字面上 相同,我认为没有理由在每个命名空间中都需要一个副本。如果不是字面上相同,实际上存在什么区别? -
@DanielA.White:对不起,这超出了我的想象!
-
根据您的示例,我看不出 _Root 字段有两种不同类型的原因,我也看不出 _Root 字段被称为两个不同名称的原因(_Root1 和_Root2)。您无法以这种方式正确共享逻辑。
-
@KyleB:你是对的 - 我只需要 _Root 两者,而不需要 _Root1 和 _Root2。我猜是被冲昏了头脑。