我有一个简单的asmx网络服务
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
答案 0 :(得分:3)
只需更改此行:
$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);
答案 1 :(得分:0)
我解决了问题,虽然是以一种弯曲的方式。
上述方法继续给我错误。所以我做的是我在我的网络服务中创建了一个方法,如
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public Person GetEmptyPerson() {
return new Person();
}
现在而不是在我的客户端代码中调用new。我只是调用此方法并获取Person对象。这有效,但需要额外往返服务器。
反正。我的代码不需要超强性能,因此额外调用的开销很好。