为什么 powershell 向 Web 服务方法签名添加额外的参数 [英] Why is powershell adding additional parameters to Web Service Method Signatures

查看:53
本文介绍了为什么 powershell 向 Web 服务方法签名添加额外的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Powershell 从命令行 ping 几个 WCF Web 服务.例如

I'm trying to use Powershell to ping a couple of WCF Webservices from the command line. e.g.

我有一个 WCF 操作

I have an WCF Operation

[OperationContract]
string DoWork(string name);

我可以使用 Powershell 调用它.

And I can call that with Powershell using.

$proxy = New-WebServiceProxy -Uri 'http://localhost/TestService/Service.svc'
$proxy.DoWork('Hello World')

只要输入参数和返回类型是字符串,就可以正常工作.但是,如果我引入整数,生成的方法签名 &返回类型生成了额外的 paramSpecified 属性.

This works fine as long as the input params and return types are strings. However if I introduce integers, the generated method signatures & return types have additional paramSpecified properties generated.

考虑以下带有数据协定返回类型的方法.

Consider the following Method with a Data Contract return type.

[DataContract]
public class SimpleClass
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public int Count { get; set; }
}

... 

[OperationContract]
SimpleClass DoWorkD(string name, int howMany);

问题 1

方法签名错误&有一个额外的参数 bool howManySpecified.

The signature of the method is wrong & has an extra parameter bool howManySpecified.

$proxy = New-WebServiceProxy -Uri 'http://localhost/TestService/Service.svc'
$method = $proxy | Get-Member -Name DoWorkD
$method.Definition

Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy3alhost_TestService_Service_svc.SimpleClass, -nv8lxgh, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null DoWorkD(string name, int howMany, bool howManySpecified)

问题 2

DataContract 类的返回代理还有额外的 XXXSpecified 属性用于非字符串属性.

The returned proxy of the DataContract class also has additional XXXSpecified properties for non-string properties.

______________________________________________________________________
PS D:\Work\Sandbox\Powershell> $proxy.DoWorkD("Hello World", 10, $true")

Count       CountSpecified    Name                             
-----       --------------    ----                             
10                    True    Hello World 

问题 3

将原始类型设置为返回类型只是完全不直观的行为.一个返回整数的简单方法作为 System.Void 方法出现,其结果可通过 ref 参数获得.

Setting a primitive type as return type just has completely unintuitive behaviour. A simple method which returns an integer comes out as a System.Void method, whose results are available through ref parameters.

[OperationContract]
int DoWorkE(int a, int b, int c, int d);

PS D:\Work\Sandbox\Powershell> $proxy.DoWorkE(1,$true, 2,$true,3,$true,4,$true, [ref] $intresult, [ref] $intresultPresent)
$intresult
10

PS D:\Work\Sandbox\Powershell> ($proxy | Get-Member -Name DoWorkE).Definition
System.Void DoWorkE(int a, bool aSpecified, int b, bool bSpecified, int c, bool cSpecified, int d, bool dSpecified, System.Int32& DoWorkEResult, System.Boolean& DoWorkEResultSpecified)

这是故意的吗?我很困惑为什么需要这些额外的 specified 参数,如果不需要,是否可以删除它们,并且 int-results-by-ref 很奇怪

Is this by design. I'm confused as to why these extra specified params are needed and if not, can they be removed and the int-results-by-ref is just bizarre

如果有人能对此设计/行为有所了解,谢谢.

Thanks if anyone can shed any light on this design/behaviour.

推荐答案

阅读我很久以前问的这个问题:

Read this question I asked a long time ago:

奇怪的行为调用方法wcf 从 powershell 使用 new-webproxyservice

您需要在操作合约中添加[XmlSerializerFormat]以避免额外的bool参数.

You need to add [XmlSerializerFormat] to the operation contract to avoid additional bool parameters.

这篇关于为什么 powershell 向 Web 服务方法签名添加额外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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