[英]Calling ASMX from Powershell
我有一個簡單的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
(類似於命名空間)。 您通常不關心代理 類或其名稱,因此只需不指定此參數即可將其保留為默認值 name,它實際上可以是任何有效的類名,但您需要與-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);
我解決了問題,盡管是以一種彎曲的方式。
上面的方法繼續給我錯誤。 所以我做的是我在我的網絡服務中創建了一個方法
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public Person GetEmptyPerson() {
return new Person();
}
現在而不是在我的客戶端代碼中調用new。 我只是調用此方法並獲取Person對象。 這工作但涉及到服務器的額外往返。
無論如何。 我的代碼不需要超強的性能,所以額外調用的開銷很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.