关于 .net:自动别名生成的 SOAP 代理 | 珊瑚贝

Auto-alias generated SOAP proxies


我目前正准备在 .NET 项目 (C#) 中使用 SOAP Web 服务,但是用于服务类型和操作的命名约定相当糟糕与命名约定不一致典型的 C# .NET 项目。

我的问题本质上是:有没有办法在我的客户端实现中自动为生成的 SOAP Web 服务代理类型/方法设置别名?

我希望有某种方法可以使用别名映射执行 WSDL 的转换,这样生成的(或重新生成的)类型使用诸如 Contact 之类的名称,但映射到底层的 contactObject 定义。

由于我不知道可以在生成过程中执行的任何转换,我目前正在手动(或至少在 T4 的帮助下)为类编写package器,但这似乎是不必要的级别间接;更不用说,一个痛苦的屁股。

我正在阅读 Svcutil 上的文档,但没有找到任何适用的标志。

  • 在我提出更复杂的答案之前,让我知道这个问题的答案是否有帮助。我之所以这么问,是因为 SOAP 服务中那些糟糕的名称似乎必须来自同一个来源,即自动生成的 DataContractFormat。您是否有权访问 SOAP 服务的程序集?如果可以只重写 Web 服务接口层并使用它的依赖项,那么您只需要 [XmlSerializerFormat] 属性。
  • @MarkBailey 我无权访问服务器端应用程序代码;它完全是第三方。糟糕的 SOAP 名称(我推测)可能是第三方命名约定的结果,可能源自他们使用的任何平台的约定(PHP、Java、*耸肩*)如果问题只是那个”字段”被附加到成员身上,我只是忍受它。
  • @MarkBailey 我刚刚找到了关于 Web Service Software Factory 的答案。也许类似的东西可以为我提供完成此任务所需的定制;我得读一读。不过,与此同时,我全神贯注于您的复杂答案;-)
  • 好吧,显然 Web 服务软件工厂不想安装,根据我正在阅读的内容,这似乎是一个”陷入困境”的情况……


我已将这个发布到你的另一个问题,但你是对的,它更适合这个问题:

我假设您通过使用”添加服务引用…”添加此第三方服务来使用它,它会为 Reference.cs 中的每个类自动生成一些代码,其签名可能看起来像像这样:

1
2
3
4
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“code”)]
[System.Xml.Serialization.XmlTypeAttribute(Namespace =”http://www.thirdpartyguys.net”)]
public partial class qux: object, System.ComponentModel.INotifyPropertyChanged {

你希望它不是qux,而是Qux。如果到目前为止这一切都与您的模型相似,那么您只需将 qux 更改为 Qux,但将 TypeName=”qux” 添加到 XmlTypeAttribute,并更改引用中对此类的所有引用。这在 SOAP 中维护了正确的 xml 模式,但是让您更改项目中的名称:

1
2
3
4
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“code”)]
[System.Xml.Serialization.XmlTypeAttribute(Namespace =”http://www.thirdpartyguys.net”, TypeName =”qux”)]
public partial class Qux: object, System.ComponentModel.INotifyPropertyChanged {

当然,如果该 XmlType 属性尚未在定义命名空间的类上,您可以添加它。它只是没有命名空间参数。我刚刚对此进行了测试,它确实允许我使用该服务,并且只需在我使用它的任何地方以不同的名称调用一个对象。

这对你有用吗?

编辑:(向未来的读者简要介绍 SchemaImporterExtension 的想法)
据我了解,当从 WSDL 添加服务引用时,此扩展类可以调用与默认代码生成行为的偏差。您最终仍然拥有一些 Reference.cs 作为您的项目和服务之间的链接,但您可以更改生成的内容。因此,如果我们希望对象始终以大写字母开头,例如,我认为这个想法是做这样的事情(未经测试):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class test : SchemaImporterExtension
{
    public override string ImportSchemaType(string name, string ns, XmlSchemaObject context,
        XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit,
        CodeNamespace codeNamespace, CodeGenerationOptions options, CodeDomProvider codeGenerator)
    {
        if (name[0].CompareTo(‘a’) >= 0) //tests if first letter is lowercase
        {
            CodeExpression typeNameValue = new CodePrimitiveExpression(name);
            CodeAttributeArgument typeNameParameter = new CodeAttributeArgument(“TypeName”, typeNameValue);
            CodeAttributeDeclaration xmlTypeAttribute = new CodeAttributeDeclaration(“XmlTypeAttribute”, typeNameParameter);
            compileUnit.AssemblyCustomAttributes.Add(xmlTypeAttribute);
            return name.Substring(0, 1).ToUpper() + name.Substring(1);
        }
        return null;
    }
}

理论上,这将写入 XmlType 属性并将名称更改为正确的大小写,从而在 SOAP 中保持正确的 XML 映射。理论上,使用 SchemaImporterExtension 的优点是对服务引用的更新不会覆盖更改。此外,可以进行一般更改,而不是针对每个特定参考。

欢迎成功使用 SchemaImporterExtension 的人发表评论或编辑。

  • 太棒了,谢谢@MarkBailey。正如我们在聊天中讨论的那样,我将看一下 SchemaImporterExtension 方法,否则会退回到 XmlTypeAttribute 重命名”所有事物”;-)
  • 哇,关于 SchemaImporterExtension 的好例子。我仍在处理这个过程,但是当我回到我的机器并附加一个代码示例时,我将把它标记为答案。


来源:https://www.codenong.com/22097706/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8943.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?