gpt4 book ai didi

powershell - 从Powershell调用ASMX

转载 作者:行者123 更新时间:2023-12-02 23:28:27 26 4
gpt4 key购买 nike

我有一个简单的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

但是我不喜欢有趣的 namespace 来访问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中生成类型的 namespace ,因此,如果您的WSDL声明了 Person类型,它将在该 namespace 中可用。 -Class参数指定代理类名称,默认情况下为 "MyClass" + random(类似于 namespace )。通常,您不必担心代理类或其名称,因此您可以通过不指定此参数名称而将其保留为默认值,只要它实际上可以是任何有效的类名称,但是您需要将其与 -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);

关于powershell - 从Powershell调用ASMX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25901228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com