Bug in New-WebServiceProxy cmdlet when using -Namespace?

后端 未结 2 734
长发绾君心
长发绾君心 2021-02-05 20:36

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 whe

相关标签:
2条回答
  • 2021-02-05 21:05

    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

    0 讨论(0)
  • 2021-02-05 21:20

    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:

    • Make sure it's imported once and only once per AppDomain. In something like PowerGUI or the ISE this might be harder than it sounds, because not only are there the runspaces you think about (like open tabs), but there are the runspaces you don't (like those fired up by add ons, or in-process jobs)
    • The far "easier", but significantly more cryptic looking workaround is to simple create types relative to a particular namespace.

    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

    0 讨论(0)
提交回复
热议问题