从Powershell调用ASMX

知道不多

我有一个简单的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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

异步/等待对asmx服务的调用

来自分类Dev

无法从PHP调用.NET ASMX

来自分类Dev

从ASMX调用基于任务的方法

来自分类Dev

从跨域调用asmx服务

来自分类Dev

从ASMX调用基于任务的方法

来自分类Dev

ASMX跨域调用(REST与JSONP)

来自分类Dev

调用ASMX服务中的多余参数

来自分类Dev

ASMX Web服务的异步调用

来自分类Dev

从Angle 2调用ASMX Web服务

来自分类Dev

Xamarin Forms asmx webservice 多次调用

来自分类Dev

如何从另一个asmx调用一个asmx

来自分类Dev

从jQuery调用.asmx Web服务:不允许GET吗?

来自分类Dev

通过Microsoft Web API端点调用Web服务-asmx?

来自分类Dev

从JavaScript调用asmx Web服务会产生404

来自分类Dev

如何从Windows Phone 8.1调用WCF服务或ASMX服务

来自分类Dev

通过AJAX的ASMX调用返回内部服务器错误

来自分类Dev

如何在ASMX服务客户端上调用方法

来自分类Dev

ASMX到64位服务器中的COM调用

来自分类Dev

在 swift 3 和 xcode 8+ 中调用 .NET asmx

来自分类Dev

从Java调用Powershell脚本

来自分类Dev

Powershell调用-MySQL

来自分类Dev

Powershell调用静态方法

来自分类Dev

如何从Powershell调用GetInstance

来自分类Dev

Powershell调用WebRequest的问题

来自分类Dev

Powershell调用-MySQL

来自分类Dev

从Java调用Powershell脚本

来自分类Dev

从PowerShell调用Windows副本

来自分类Dev

使用Powershell调用psftp

来自分类Dev

挣扎着从jQuery调用返回数据到vb.Net ASMX Web服务