So I ran into this exact problem: http://www.vistax64.com/powershell/273120-bug-when-using-namespace-parameter-new-webserviceproxy.html
The gist of the issue is that when using the New-WebServiceProxy cmdlet AND the -Namspace parameter then you can't execute a method on the proxy with an argument of an autogenerated type.
Something like this:
// In the service
public void DoSomething(DoSomethingRequest request) { ... }
$proxy = New-WebServiceProxy -Uri "http://something.com/MyService.svc"
-Namespace ns
$req = New-Object ns.DoSomethingRequest
$proxy.DoSomething($req)
This throws an exception along the lines of Cannot convert argument "0" of type "ns.DoSomething" to type "ns.DoSomething"
As is explained in the link, by removing the -Namespace parameter and utilizing the autogenerated namespace everything works fine. However, I'd really like to use the -Namespace....
I can't find anything related to a "fix" or the correct way to utilize the -Namespace in this scenario. Can anyone shed some light on this for me?
Actually you're seeing something a notch more subtle. With -Namespace, you cannot execute an argument of the type in the namespace twice
I suspect you're creating New-WebServiceProxy a lot, like, in the process block of a function, or inside of a command that's called repeatedly. Every time you call it, it tries to regenerate, and, with -Namespace, this leaves little collisions with associated types in the AppDomain. There end up being several of them, and you get this error.
There are two ways around this:
This is far easier to show than tell.
I had this problem with a module I have for Office365 / Exchange Web Services, and, beneath the covers, it has something like:
$createItemType =
New-Object "$script:ExchangeWebServiceNamespace.CreateItemType"
-Property @{
MessageDisposition = $messageDisposition
MessageDispositionSpecified = $true
Items =
New-Object "$script:ExchangeWebServiceNamespace.NonEmptyArrayOfAllItemsType"
}
This is obviously a little cryptic, but, well, so is Exchange Web Services. And so are many web services that are built directly from complex object models.
Unfortunately, that is the vast majority of web services that New-WebServiceProxy works with.
Bear in mind, it's still good practice to cache the web service object you create in your module (using $script:variablename
like above), but this weird trick to reference web service parts by the proxy object has saved me more times than I'd like.
Hope this Helps
Are you running your code in some editor? I had similar problem when running script from PowerGUI editor, but it is working fine for me when I run it from console. Try close all your powershell related stuff and open it again
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With