从Powershell调用ASMX
我有一个简单的ASMXWeb服务从Powershell调用ASMX,powershell,asmx,Powershell,Asmx,我有一个简单的ASMXWeb服务 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 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类而不使用像WebServiceProxyCalHost\u 10875\u WebService1\u 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
新对象:找不到类型[com.abhi.Proxy.Person]:请确保包含此类型的程序集已加载
预计起飞时间。
在C:\Users\srabhi\u adm\Documents\SP2010InfobarrierException.ps1:6 char:21
+$person=新对象只需更改此行:
$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);
我解决了这个问题,尽管方法不正确
上述方法继续给我带来错误。所以我所做的是在我的web服务中创建了一个方法,比如
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public Person GetEmptyPerson() {
return new Person();
}
而不是在我的客户机代码中调用new。我只是调用这个方法并获取Person对象。这是可行的,但需要到服务器进行一次额外的往返
无论如何。我的代码不需要出色的性能,因此一次额外调用的开销是可以接受的。仍然出现错误无法将参数“0”(值为“com.abhi.Person”)的“HelloWorld”转换为类型“com.abhi.Person”:“C无法将类型为“com.abhi.Person”的“com.abhi.Person”的“com.abhi.Person”值转换为类型“com.abhi.Person”在C:\Users\srabhi\u adm\Documents\SP2010InfobarriereException.ps1:10 char:23+$s=$proxy.HelloWorld好的,我已经根据您的代码创建了一个新的Web服务,它工作了。我正在编辑我的答案以包含完整的PowerShell部分,也许您会发现您的代码有一些不同?值得一提的是,我使用的是ISE中的PowerShell 4.0。事实上,在进一步研究这个主题之后,我发现了一个bug(?)的一些提示:当您指定-Namespace
,但忽略-Class
,新的WebServiceProxy
将生成一个随机命名的代理类,因此,当您重复运行代码时,名称空间将被同一Person
类的多个声明所污染。同时指定类名时,声明将被正确替换。我在编辑我的答案时考虑到了这一点。