为什么我的wcf Web服务引用中有额外的参数?

时间:2022-09-25 12:40:12

I'm trying to convert an ASP.Net web service to WCF application. The client is on the .Net Compact Framework which does not support WCF so I need to make sure the WCF keeps supporting ASP style webservices. When I add the web service reference in Visual Studio the generated proxy class' methods have extra arguments.

我正在尝试将ASP.Net Web服务转换为WCF应用程序。客户端在.Net Compact Framework上,它不支持WCF,所以我需要确保WCF继续支持ASP样式的web服务。当我在Visual Studio中添加Web服务引用时,生成的代理类'方法有额外的参数。

For example if a method is defined as:

例如,如果方法定义为:

public void GetEmpInfo(int empNo)

That method will appear in the proxy class as:

该方法将在代理类中显示为:

public void GetEmpInfo(int empNo, bool empNoSpecified)

What causes this, and how do I get it to stop?

是什么导致这种情况,我如何让它停下来?

3 个解决方案

#1


Check out this blog post ...

看看这篇博文......

Where did these extra boolean “specified” members come from and what do they do? The answer is the schema that the WCF data contract serializer generates by default. Because of the way its versioning model works, the serializer generates all data members as optional elements. The older web services stack, ASP.NET Web Services (“ASMX”), uses a different serializer, the XmlSerializer, which maintains full schema and XML fidelity. The XmlSerializer maps all optional elements to two members: one represents the data itself, and one specifies whether or not the data is actually present – this is the “xxxSpecified” member. These xxxSpecified members must be set to true to enable the serialization of the corresponding “actual data” members.

这些额外的布尔“指定”成员来自哪里,他们做了什么?答案是WCF数据协定序列化程序默认生成的模式。由于其版本控制模型的工作方式,序列化程序会将所有数据成员生成为可选元素。较旧的Web服务堆栈,ASP.NET Web服务(“ASMX”),使用不同的序列化程序XmlSerializer,它维护完整的架构和XML保真度。 XmlSerializer将所有可选元素映射到两个成员:一个表示数据本身,另一个指定数据是否实际存在 - 这是“xxxSpecified”成员。必须将这些xxxSpecified成员设置为true才能启用相应“实际数据”成员的序列化。

#2


The .NET Compact Framework does support a subset of WCF. You can review this support on MSDN. Take a look, it may support enough for you to remove your legacy Web Services support.

.NET Compact Framework确实支持WCF的子集。您可以在MSDN上查看此支持。看一下,它可能足以支持您删除旧的Web服务支持。

#3


This happens for types with a default value of not null. In these cases, it's impossible for the web service to know whether a parameter was set to the default value or simply not set at all.

对于默认值为not null的类型,会发生这种情况。在这些情况下,Web服务无法知道参数是设置为默认值还是根本不设置。

You can get rid of the extra specification parameter by decorating your operation with the [XmlSerializerFormat] attribute like:

您可以通过使用[XmlSerializerFormat]属性修改操作来消除额外的规范参数,如:

    [OperationContract]
    [XmlSerializerFormat]
    string GetEmpInfo(int? empNo);

This attribute can also be added at the Class level, and this would make sense in most cases.

此属性也可以在类级别添加,这在大多数情况下都有意义。

I understand you can handle this situation using nullable types (int?), but I was unable to fix it using this.

我知道你可以使用可空类型(int?)来处理这种情况,但我无法使用它来修复它。

#1


Check out this blog post ...

看看这篇博文......

Where did these extra boolean “specified” members come from and what do they do? The answer is the schema that the WCF data contract serializer generates by default. Because of the way its versioning model works, the serializer generates all data members as optional elements. The older web services stack, ASP.NET Web Services (“ASMX”), uses a different serializer, the XmlSerializer, which maintains full schema and XML fidelity. The XmlSerializer maps all optional elements to two members: one represents the data itself, and one specifies whether or not the data is actually present – this is the “xxxSpecified” member. These xxxSpecified members must be set to true to enable the serialization of the corresponding “actual data” members.

这些额外的布尔“指定”成员来自哪里,他们做了什么?答案是WCF数据协定序列化程序默认生成的模式。由于其版本控制模型的工作方式,序列化程序会将所有数据成员生成为可选元素。较旧的Web服务堆栈,ASP.NET Web服务(“ASMX”),使用不同的序列化程序XmlSerializer,它维护完整的架构和XML保真度。 XmlSerializer将所有可选元素映射到两个成员:一个表示数据本身,另一个指定数据是否实际存在 - 这是“xxxSpecified”成员。必须将这些xxxSpecified成员设置为true才能启用相应“实际数据”成员的序列化。

#2


The .NET Compact Framework does support a subset of WCF. You can review this support on MSDN. Take a look, it may support enough for you to remove your legacy Web Services support.

.NET Compact Framework确实支持WCF的子集。您可以在MSDN上查看此支持。看一下,它可能足以支持您删除旧的Web服务支持。

#3


This happens for types with a default value of not null. In these cases, it's impossible for the web service to know whether a parameter was set to the default value or simply not set at all.

对于默认值为not null的类型,会发生这种情况。在这些情况下,Web服务无法知道参数是设置为默认值还是根本不设置。

You can get rid of the extra specification parameter by decorating your operation with the [XmlSerializerFormat] attribute like:

您可以通过使用[XmlSerializerFormat]属性修改操作来消除额外的规范参数,如:

    [OperationContract]
    [XmlSerializerFormat]
    string GetEmpInfo(int? empNo);

This attribute can also be added at the Class level, and this would make sense in most cases.

此属性也可以在类级别添加,这在大多数情况下都有意义。

I understand you can handle this situation using nullable types (int?), but I was unable to fix it using this.

我知道你可以使用可空类型(int?)来处理这种情况,但我无法使用它来修复它。