问题
I'm trying to use Powershell to ping a couple of WCF Webservices from the command line. e.g.
I have an WCF Operation
[OperationContract]
string DoWork(string name);
And I can call that with Powershell using.
$proxy = New-WebServiceProxy -Uri 'http://localhost/TestService/Service.svc'
$proxy.DoWork('Hello World')
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);
Problem 1
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)
Problem 2
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
Problem 3
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)
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.
回答1:
Read this question I asked a long time ago:
Strange behaviour calling method of wcf from powershell using new-webproxyservice
You need to add [XmlSerializerFormat]
to the operation contract to avoid additional bool parameters.
来源:https://stackoverflow.com/questions/9888610/why-is-powershell-adding-additional-parameters-to-web-service-method-signatures