【发布时间】:2015-09-17 05:11:50
【问题描述】:
我一直在尝试通过 Powershell 使用供应商提供的 Web 服务系统(我正在运行 4.0)。 以下是我用来设置代理以使用服务的代码:
$uri = http://somehost.employer.net:9999/AdministrationService?wsdl
$webSvc = New-WebServiceProxy -Uri $uri -namespace WebServiceProxy -Credential $TestCreds_NonPriv
目前,我对服务公开的两种方法感兴趣,“SaveAttributes”和“GetAllAttributes”, wsdl 定义如下:
<xsd:element name="SaveAttributes">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="Id" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="attributes" nillable="true" type="q5:ArrayOfKeyValueOfstringstring"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetAllAttributes">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="Id" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetAllAttributesResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="GetAllAttributesResult" nillable="true" type="q2:ArrayOfKeyValueOfstringstring"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
我已通过以下方式成功使用 GetAllAttributes 函数:
$websvc.GetAllAttributes('someid')
这会返回一个对象,它是一个键值对数组,如下所示:
Key Value
--- -----
ID 01
PERSONA godeater@stackoverflow.com
NAME GodEater's real name
如果我想在 Web 服务上创建一个新对象,我必须调用“SaveAttributes”函数 两个参数,一个作为字符串的新“ID”和一个用于其余属性的键/值对数组, 这就是我难过的地方。我无法创建成功序列化的 powershell 对象 到服务。我尝试的是这样的:
[WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring[]]$newentity = @(
@{Key='PERSONA';Value='someone_else@stackoverflow.com'},
@{Key='NAME';Value='Someone Elses Real Name'}
)
$WebSvc.SaveAttributes('02',$newentity)
但我得到的是这个相当令人困惑的错误:
Cannot convert argument "attributes", with value: "WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring[]", for "SaveAttributes" to type
"WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring[]": "Cannot convert the "WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring" value of type
"WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring" to type "WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring"."
At line:1 char:1
+ $WebSvc.SaveAttributes('02',$newentity)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
现在,看看 GetAllAttributes 返回给我的内容,它应该是与我相同的对象 尝试输入 SaveAttributes 方法的“属性”参数,它似乎稍微序列化 与我自己构建的对象不同。
序列化时 GetAllAttributes 的结果:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring[]</T>
<T>System.Array</T>
<T>System.Object</T>
</TN>
<LST>
<Obj RefId="1">
<TN RefId="1">
<T>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring</T>
<T>System.Object</T>
</TN>
<ToString>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring</ToString>
<Props>
<S N="Key">ID</S>
<S N="Value">01</S>
</Props>
</Obj>
<Obj RefId="2">
<TNRef RefId="1" />
<ToString>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring</ToString>
<Props>
<S N="Key">PERSONA</S>
<S N="Value">godeater@stackoverflow.com</S>
</Props>
</Obj>
<Obj RefId="3">
<TNRef RefId="1" />
<ToString>WebServiceProxy.ArrayOfKeyValueOfstringstringKeyValueOfstringstring</ToString>
<Props>
<S N="Key">NAME</S>
<S N="Value">GodEater's real name</S>
</Props>
</Obj>
</LST>
</Obj>
</Objs>
虽然我创建的对象序列化为此:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Collections.Hashtable</T>
<T>System.Object</T>
</TN>
<DCT>
<En>
<S N="Key">PERSONA</S>
<S N="Value"></S>
</En>
</DCT>
</Obj>
</Objs>
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Collections.Hashtable</T>
<T>System.Object</T>
</TN>
<DCT>
<En>
<S N="Key">NAME</S>
<S N="Value">Someone Elses Real Name</S>
</En>
</DCT>
</Obj>
</Objs>
这似乎缺乏对数组成员的环绕,而是似乎 将数组中的每个项目作为独立对象。
有人有什么想法吗?
【问题讨论】:
标签: arrays web-services powershell