自动别名生成的SOAP代理 [英] Auto-alias generated SOAP proxies

查看:196
本文介绍了自动别名生成的SOAP代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前preparing消耗 SOAP Web服务在.NET项目(C#),但是使用的服务类型和操作的命名约定是<打击> pretty的坏的命名约定通常为C#.NET项目并不一致

I'm currently preparing to consume a SOAP web service in a .NET project (C#), however the naming conventions used for the service types and operations are pretty bad not consistent with the naming conventions typical to C# .NET projects.

我的问题,基本上是:有没有办法来自动别名在我的客户端实现生成的SOAP Web服务代理类/方法

My question, essentially: is there a way to auto-alias the generated SOAP web service proxy types/methods in my client implementation?

我希望有一些方法来执行与别名的地图,​​使得产生的(或再生)类型使用的名称,如联系方式但映射到底层 contactObject 定义。

I'm hoping that there's some way to perform a transformation of the WSDL with a map of aliases, such that the generated (or regenerated) types use names such as Contact but map to the underlying contactObject definition.

由于我不知道,可以生成期间执行的任何变换,我目前寻找手动(或至少与T4的协助下)写入封装器的类,但是这似乎的不必要的水平间接;更何况,一屁股痛。

Since I'm unaware of any transformations that could be performed during generation, I'm currently looking at manually (or at least with the assistance of a T4) writing wrappers for the classes, however this seems like an unnecessary level of indirection; not to mention, a pain in the ass.

我读通过的 SvcUtil工具,但没有发现任何适用的标志。

I'm reading through the docs on Svcutil, but haven't found any applicable flags.

推荐答案

我有这个贴到您的<一个href="http://stackoverflow.com/questions/22408104/expose-different-type-names-aliasing-from-assembly/22410537#22410537">other问题但你是正确的,它更适合于这一个:

I had posted this to your other question but you are right, it's better suited to this one:

我假定你是用它添加食用这种第三方服务添加服务引用...,其中自动生成一些code为每个类的Reference.cs,与签名看起来可能是这样的:

I assume that you are consuming this third-party service by adding it with "Add Service Reference...", which auto-generates some code for each class in a Reference.cs, with signatures that might look something like this:

[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,但增加了类型名=qux XmlTypeAttribute ,并更改引用中这一类的所有引用。这保持在SOAP正确的XML架构,但让我们你在项目中更改名称:

And you wish that instead of qux, it said Qux. If this is all similar to your model so far, then you can just change qux to Qux, but add TypeName="qux" to the XmlTypeAttribute, and change all references to this class within the reference. This maintains the correct xml schema in the SOAP, but let's you change the name within your project:

[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将属性不是已经在类来定义命名空间,您可以添加它。它只是将不会有命名空间参数。我测试了刚才它允许我使用该服务,只需拨打一个目的是通过不同的名字无处不在,我用它。

And of course, if that XmlType attribute is not already on the class to define the namespace, you can add it. It just won't have the Namespace parameter. I tested this just now it does allow me to use the service and simply call an object by a different name everywhere that I use it.

这是否会为你工作?

编辑:(向大家介绍未来的SchemaImporterExtension想法的读者) 据我了解,这个扩展类可以调用从当服务引用从WSDL添加默认的code生成行为的偏差。您仍可以有一些Reference.cs充当你的项目和服务之间的联系,但你可以改变会产生什么。因此,如果我们想的对象总是以一个大写字母,例如,我认为这个想法是做这样的事情(未经测试):

(To brief future readers on the SchemaImporterExtension idea) As I understand it, this extension class can invoke a deviation from the default code generation behavior when a Service Reference is added from a WSDL. You still end up having some Reference.cs that acts as the link between your project and the Service, but you can change what is generated. So if we want the objects to always begin with a capital letter, for example, I think the idea is to do something like this (untested):

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

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

This, in theory, will write in the XmlType Attribute and change the name to the correct casing, thus maintaining the correct XML mapping in the SOAP. The advantage in using SchemaImporterExtension, in theory, is that updates to the service reference will not overwrite the changes. Also, changes can be made generically rather than to each specific reference.

谁使用SchemaImporterExtension从人的评论或编辑成功是受欢迎的。

Comments or edits from people who have used SchemaImporterExtension successfully are welcome.

这篇关于自动别名生成的SOAP代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆