7

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 Answer 1

5

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.

2
  • Thanks. Will this have any adverse effect on any other .NET Client applications which are already calling the service. Is this a "Breaking" attribute. Will they need to be recompiled against the end point ? Mar 27, 2012 at 11:42
  • After I added the attribute , recompiled and published my wcf all my clients applications (all in .net 2.0 or 4.0 ) have no changed behavior. But I've tested it only with .net app and powershell.
    – CB.
    Mar 27, 2012 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.