简体   繁体   English

为什么WCF有时会在生成的代理类型的末尾添加“Field”?

[英]Why does WCF sometimes add “Field” to end of generated proxy types?

Basically, I have a server-side type "Foo" with members X and Y. Whenever I use Visual Studio's "Add Server Reference" then I see the WSDL and the generated proxy both append the word "Field" to all the members and change the casing of the first letter. 基本上,我有一个服务器端类型“Foo”,成员X和Y.每当我使用Visual Studio的“添加服务器参考”时,我看到WSDL和生成的代理都向所有成员附加单词“Field”并更改第一个字母的外壳。 IE, "X" and "Y" are renamed "xField" and "yField". IE,“X”和“Y”被重命名为“xField”和“yField”。 Any idea why this is happening? 知道为什么会这样吗? I can't figure out the pattern. 我无法弄清楚这种模式。

Details -- I have a legacy ASMX web service which exposes a "Foo" type. 详细信息 - 我有一个传统的ASMX Web服务,它公开了一个“Foo”类型。 I created a new WCF service that's a wrapper around that old web service -- the new service just wraps those methods and maybe updates the values of a few fields, but it exposes the exact same methods and returns the exact same types. 我创建了一个新的WCF服务,它是旧Web服务的包装器 - 新服务只包装这些方法并可能更新几个字段的值,但它公开了完全相同的方法并返回完全相同的类型。 I've tried re-creating the referenes several times, and every time, it always renames my fields: the varible "STUFF" is exposed in the wsdl and proxy as "sTUFFField". 我曾多次尝试重新创建referenes,每次都会重命名我的字段:变量“STUFF”在wsdl和proxy中显示为“sTUFFField”。 Variable "X" is exposed as "xField", etc. 变量“X”显示为“xField”等。

Funny thing is I can't figure out the pattern -- I tried creating a new ASMX web service as a test and wrapping that -- variables are not renamed then. 有趣的是我无法弄清楚模式 - 我尝试创建一个新的ASMX Web服务作为测试和包装 - 变量不会被重命名。 So I can't figure out the pattern of why/when WCF renames variables. 所以我无法弄清楚为什么/何时WCF重命名变量的模式。

Anybody know? 有人知道吗?

I had the same issue, and sergiosp's answer got me headed in the right direction. 我有同样的问题,而sergiosp的回答让我朝着正确的方向前进。 Just adding some additional info to hopefully help someone else. 只需添加一些额外的信息,希望能帮助别人。

Adding [System.ServiceModel.XmlSerializerFormatAttribute()] to the interface, and re-generating the client code resolved the problem for me. [System.ServiceModel.XmlSerializerFormatAttribute()]添加到接口,并重新生成客户端代码为我解决了问题。

public interface IMyService
{
    [System.ServiceModel.XmlSerializerFormatAttribute()]
    [System.ServiceModel.OperationContract]
    recordResponse GetRecord(recordRequest request);

}

I had the same problem but i was able to find solution. 我有同样的问题,但我能够找到解决方案。

In the interface if you add [DataContractFormat] tag you will end up with "XFieldField" case. 在界面中,如果添加[DataContractFormat]标记,最终会出现“XFieldField”情况。 But if you replace it with [XmlSerializerFormat] in the interface it will not change the names in the proxy generated. 但是如果在接口中用[XmlSerializerFormat]替换它,它将不会更改生成的代理中的名称。

Typically, the generated proxy will have "XField" and "YField" as internal/protected/private fields, and expose the values through properties called "X" and "Y". 通常,生成的代理将“XField”和“YField”作为内部/保护/私有字段,并通过名为“X”和“Y”的属性公开值。 There are options you can set when creating the proxy client to tweak that to your liking, I think. 根据您的喜好,您可以在创建代理客户端时设置选项,以便根据自己的喜好进行调整。

UPDATE: I don't seem to find any switches or options to control this behavior. 更新:我似乎没有找到任何开关或选项来控制此行为。 It might depend on which serializer (DataContractSerializer vs. XmlSerializer) WCF uses for creating the client proxy. 它可能取决于WCF用于创建客户端代理的序列化程序(DataContractSerializer与XmlSerializer)。

In the end, it's really more or less just an issue of coding style - functionally, it shouldn't make a difference. 最后,它或多或少只是一个编码风格的问题 - 在功能上,它不应该有所作为。

Marc

I had this problem too, but from the client I was still getting Field at the end of the class members even after making the mentioned change at the interface. 我也有这个问题,但是从客户端我仍然在类成员结束时得到Field ,甚至在接口处进行了上述更改之后。

The problem was, I was using a DataContractSerializer to work with disk file serialized requests (during the test of our service, we were getting serialized requests from the provider, to be able to debug before going live). 问题是,我使用DataContractSerializer处理磁盘文件序列化请求(在我们的服务测试期间,我们从提供程序获得序列化请求,以便能够在上线之前进行调试)。

After changing the DataContractSerializer to a XmlSerializer , specifying on its constructor the root element (by a typeof() call) and the rootnamespace (because by default, XmlSerializers write the standard namespace), I could deserialize the requests and work perfectly with the WCF Service. 在将DataContractSerializer更改为XmlSerializer ,在其构造函数上指定根元素(通过typeof()调用)和rootnamespace(因为默认情况下, XmlSerializers编写标准名称空间),我可以反序列化请求并与WCF服务完美配合。

Hope this helps somebody. 希望这有助于某人。 I lost soooo many time with this "issue". 这个“问题”让我失去了太多时间。

Adding XmlSerializerFormat worked for me. 添加XmlSerializerFormat对我有用。 Got solution from http://geekswithblogs.net/mipsen/archive/2010/02/06/field-postfix-in-wcf-reference.aspx http://geekswithblogs.net/mipsen/archive/2010/02/06/field-postfix-in-wcf-reference.aspx获得解决方案

[ServiceContract(Namespace="http://somenamespace.com/contracts")]    
public interface ISchemaService
{
    [OperationContract]
    [XmlSerializerFormat]
    void DoSomething(GeneratedType data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM