如何防止“指定” WCF 客户端中生成的属性?

发布于 2024-08-17 16:37:07 字数 1061 浏览 2 评论 0原文

我有两个使用 VS2008 构建的 .NET 3.5 WCF 服务。

我在 Silverlight 中有两个 WCF 客户端来使用这些服务。客户端是通过“添加服务引用”生成的。我正在使用 Silverlight 4。

其中一个代理是使用每个属性的指定属性生成的。这是我的服务方法的“消息输入”类:

    // properties are generated for each of these fields
    private long customerProfileIdField;        
    private bool customerProfileIdFieldSpecified;        
    private bool testEnvField;        
    private bool testEnvFieldSpecified;

现在我的其他服务(仍然使用 Silverlight 客户端)不会生成 Specified 属性。

现在我不关心“良好 SOA 的原则”。我只是想摆脱这些该死的属性,因为在我所做的事情的背景下,我绝对讨厌它们。

这两种服务之间肯定存在一些差异 - 但我不想将它们完全拆开才能找出差异。

之前的一个类似问题得到了答案'你不能这样做' - 这绝对不是真的,因为我有它 - 我只是不知道我做了什么不同的事情。

编辑:我现在的情况是,我将 Silverlight 4 代理重新生成到 3.5 WCF 服务(全部在同一台本地主机计算机上),有时我会获得“指定”属性,有时则不会。我不再认为(正如我最初怀疑的那样)这仅仅是由于某些端点配置或服务级别[属性]造成的。消息本身存在某些触发器,导致生成(或不生成)指定。可能涉及很多因素,也可能很简单。

I have two .NET 3.5 WCF services build with VS2008.

I have two WCF clients in Silverlight to consume these services. The clients are generated with the 'Add Service Reference'. I am using Silverlight 4.

ONE of the proxies is generated with Specified properties for each property. This is a 'message-in' class for my service method :

    // properties are generated for each of these fields
    private long customerProfileIdField;        
    private bool customerProfileIdFieldSpecified;        
    private bool testEnvField;        
    private bool testEnvFieldSpecified;

Now my other service (still with a Silverlight client) does NOT generate Specified properties.

Now I don't care about 'tenets of good SOA'. I just want to get rid of these damn properties because in the context of what I'm doing I absolutely hate them.

There has to be some difference between the two services - but I don't want to have to completely rip them apart to find out the difference.

A similar question before had the answer 'you cant do it' - which is definitely not true because I have it - I just don't know what I did differently.

Edit: I am now in a situation where I regenerate my Silverlight 4 proxy to my 3.5 WCF service (all on the same localhost machine) that sometimes I get 'Specified' properties and sometimes I don't. I no longer think (as I suspected originally) that this is due solely to some endpoint configuration or service level [attribute]. Theres certain triggers in the message itself that cause Specified to be generated (or not). There may be many factors involved or it may be something very simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╰ゝ天使的微笑 2024-08-24 16:37:07

在 WCF 服务中尝试此操作,其中声明的属性

[DataMember(IsRequired=true)]
public bool testEnvField { get; set; }

IsRequired=true 将不再需要 testEnvFieldSpecified 属性

try this in your WCF service where the property is declared

[DataMember(IsRequired=true)]
public bool testEnvField { get; set; }

IsRequired=true will negate the need for the testEnvFieldSpecified property

暮倦 2024-08-24 16:37:07

这些额外的指定属性是为在契约或属性标记中指定为可选的值类型生成的。

由于值类型默认有一个值,因此将为这些属性添加额外的 Specified 标志,以允许客户端(和服务器)区分显式未指定或显式指定的内容 - 这很可能是设置为默认值。如果没有它,即使您没有在客户端代码中设置整数(因为映射到 int),整数最终也将始终为 0(并被序列化)。因此,当您这样做时,您还需要确保将 Specified 标志设置为 true,否则这些属性将不会被序列化。

因此,为了防止为值类型生成这些标志,您必须更改约定以使这些值类型属性成为强制性的,而不是可选的。

希望这是有道理的。

These extra Specified properties are generated for value types which are being specified as optional in either the contract or the attribute markup.

As value types have a value by default, the extra Specified flags are being added for these properties, to allow the client (and server) to distinguish between something explicitly not specified or explicitly specified - which may well be set to the default value. Without it, integers would always end up being 0 (and being serialized) even if you don't set them (because of the mapping to int) in your client code. So when you do, you need to also make sure that you set the Specified flag to true, otherwise these properties will not get serialized.

So to prevent these flags being generated for value types, you would have to change the contract to make these value type properties mandatory, instead of optional.

Hope that makes sense.

我不在是我 2024-08-24 16:37:07

好的,到目前为止,我发现了一件事会导致生成 Specified 属性:

  • 消息中存在 XTypedElement

这些由 Linq2XSD 使用。我从 Linq2XSD 模型返回一个元素。

这触发了在我的所有类中生成所有内容的指定属性:

    public XTypedElement Foo { get; set; }

然而这并没有:

    public XElement Foo { get; set; }

仍然好奇为什么会这样,以及是否有任何其他事情触发它。

OK I've found one thing so far that will cause Specified properties to be generated:

  • The presence of an XTypedElement in the message.

These are used by Linq2XSD. I was returning an element from a Linq2XSD model.

This triggered Specified properties to be generated EVERYTHING in all my classes :

    public XTypedElement Foo { get; set; }

This however didn't :

    public XElement Foo { get; set; }

Still curious as to why this is, and if there are any other things that trigger this.

节枝 2024-08-24 16:37:07

注意:我意识到这是一个老问题。我在这里添加这个问题是因为这个问题在谷歌上出现在最热门的结果中,对于任何来寻找的人来说这都是有用的信息。

尝试将此行添加到您的操作合同声明中:

[XmlSerializerFormat]

它应该看起来像这样:

namespace WebServiceContract
{
    [ServiceContract(Namespace = "http://namespace")]
    [XmlSerializerFormat] //This line here will cause it to serialize the "optional" parameters correctly, and not generate the extra
    interface InterfaceName
    {
        /*...Your web service stuff here...*/
    }
}

NOTE: I realize this is an old question. I'm adding this here because this question comes up as a top result on Google, and it's helpful information for whoever comes looking.

Try adding this line into your operation contract declaration:

[XmlSerializerFormat]

It should look something like this:

namespace WebServiceContract
{
    [ServiceContract(Namespace = "http://namespace")]
    [XmlSerializerFormat] //This line here will cause it to serialize the "optional" parameters correctly, and not generate the extra
    interface InterfaceName
    {
        /*...Your web service stuff here...*/
    }
}
孤独陪着我 2024-08-24 16:37:07

我发现,如果我将 DataTable 放入服务 DataContract 中,则生成的客户端将使用 xml 序列化程序,从而生成 *IsSpecified 成员。

I found that if I put a DataTable in a service DataContract then the generated client will use xml serializer and thus generate the *IsSpecified members.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文