我有一个简单的asmx Web服务
public class WebService1 : System.Web.Services.WebService {
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public string HelloWorld(Person person) {
return "Hello World " + person.FirstName + " " + person.LastName;
}
}
public class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
}
我从powershell这样称呼它
cls
$proxy = New-WebServiceProxy -Uri 'http://localhost:10875/WebService1.asmx' -UseDefaultCredential
$person = new-object ("Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1calhost_10875_WebService1_asmx.Person")
$person.FirstName = "foo"
$person.LastName = "bar"
$s = $proxy.HelloWorld($person)
Write-Host $s
但是我不喜欢有趣的名称空间来访问Person类。
有没有一种更好的方式来访问Person类而不使用诸如WebServiceProxy1calhost_10875_WebService1_asmx之类的有趣的东西?
我可以从$ proxy对象中发现Person类吗?
编辑:
我将Powershell代码更改为
$proxy = New-WebServiceProxy -Uri 'http://localhost:10875/WebService1.asmx' -Namespace "com.abhi" -Class "Proxy" -UseDefaultCredential
$proxy | get-member -type method
$person = new-object ("com.abhi.Proxy.Person")
$person.FirstName = "foo"
$person.LastName = "bar"
$s = $proxy.HelloWorld($person)
Write-Host $s
但是现在我得到一个错误
New-Object : Cannot find type [com.abhi.Proxy.Person]: make sure the assembly containing this type is load
ed.
At C:\Users\srabhi_adm\Documents\SP2010InfobarrierException.ps1:6 char:21
+ $person = new-object <<<< ("com.abhi.Proxy.Person")
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
Property 'FirstName' cannot be found on this object; make sure it exists and is settable.
At C:\Users\srabhi_adm\Documents\SP2010InfobarrierException.ps1:7 char:9
+ $person. <<<< FirstName = "foo"
+ CategoryInfo : InvalidOperation: (FirstName:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Property 'LastName' cannot be found on this object; make sure it exists and is settable.
At C:\Users\srabhi_adm\Documents\SP2010InfobarrierException.ps1:8 char:9
+ $person. <<<< LastName = "bar"
+ CategoryInfo : InvalidOperation: (LastName:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Exception calling "HelloWorld" with "1" argument(s): "System.Web.Services.Protocols.SoapException: Server
was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance
of an object.
at SP2010InfobarrierExceptions.WebService1.HelloWorld(Person person) in c:\users\srabhi_adm\documents\v
isual studio 2010\Projects\SP2010InfobarrierExceptions\SP2010InfobarrierExceptions\WebService1.asmx.cs:lin
e 22
--- End of inner exception stack trace ---"
At C:\Users\srabhi_adm\Documents\SP2010InfobarrierException.ps1:10 char:23
+ $s = $proxy.HelloWorld <<<< ($person)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
只需更改此行:
$person = new-object ("com.abhi.Proxy.Person")
对此:
$person = new-object ("com.abhi.Person")
该-Namespace
参数指定将在其中生成WSDL中的类型的名称空间,因此,如果您的WSDL声明了该Person
类型,则它将在该名称空间中可用。该-Class
参数默认指定代理类名称"MyClass" + random
(类似于名称空间)。您通常不关心代理人类或其名称,因此您只需不指定此参数即可将其保留为默认值名称,它实际上可以是任何有效的类名称,但是您需要将其与一起指定-Namespace
。
来自PowerShell的完整演示电话:
$proxy = New-WebServiceProxy -uri "http://localhost:57633/WebSite1/Service.asmx?WSDL" -namespace "com.example" -class "MyProxyClass"
$person = New-Object "com.example.Person";
$person.FirstName = "MyFirstName";
$person.LastName = "MyLastName";
$proxy.HelloWorld($person);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句