c# – 使用Powershell调用WCF服务方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用Powershell调用WCF服务方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2921字,纯文字阅读大概需要5分钟。
内容图文
![c# – 使用Powershell调用WCF服务方法](/upload/InfoBanner/zyjiaocheng/963/d7c98e129ba74a3fa90e5781138c8396.jpg)
我有一个WCF服务,它使用带有消息安全性的wsHttpBinding和作为windows的clientcredentialtype,并且该服务有一个简单的方法
[OperationContract]
string SayHello();
public string SayHello()
{
return "HELLO";
}
<wsHttpBinding>
<binding name="WSHttpBinding">
<security mode="Message">
<message clientCredentialType="Windows" />
</security>
</binding>
</wsHttpBinding>
我试图在powershell(版本> = 2)上执行以下操作,我得到以下错误
$wshttpbinding= New-WebServiceProxy -uri http://localhost:52871/Service.svc -Credential DOMAIN\gop
PS> $wshttpbinding.SayHello.Invoke()
Exception calling "SayHello" with "0" argument(s): "The operation has timed out"
At line:1 char:1
+ $wshttpbinding.SayHello.Invoke()
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
但是,当我更改绑定以使用basicHttpBinding时,它工作正常
<basicHttpBinding>
<binding name="basicconfig"
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
$basichttpbinding= New-WebServiceProxy -uri http://localhost:52871/Service.svc -Credential DOMAIN\gop
PS> $basichttpbinding.SayHello.Invoke()
HELLO
使用wsHttpBinding时,我的脚本中有什么不同的东西吗?
提前致谢.
最后的方法
我只使用wsHttpBinding来支持WCF事务.但是我很快意识到powershell脚本需要调用的服务方法调用与事务无关.因此,我使用Windows身份验证公开了另一个BasicHttpBinding端点,它使用了以下脚本.请参阅下面的代码段
Try
{
$cred = new-object -typename System.Management.Automation.PSCredential ` -argumentlist $username, $password -ErrorAction Stop
}
Catch {
LogWrite "Could not create PS Credential"
$credErrorMessage = $_.Exception.Message
LogWrite $credErrorMessage
Break
}
Try{
$service=New-WebServiceProxy –Uri $url -Credential $cred -ErrorAction Stop
} Catch {
LogWrite "Could not create WebServiceProxy with $url"
$proxyErrorMessage = $_.Exception.Message
LogWrite $proxyErrorMessage
Break
}
# Create Request Object
$namespace = $service.getType().namespace
$req = New-Object ($namespace + ".UpdateJobRequest")
LogWrite "Calling service..."
$response = $service.UpdateJob($req)
解决方法:
我已经在gallery 中创建了一个PowerShell模块WcfPS,它可以帮助您使用元数据交换在目标服务的内存代理中创建.我已经使用这个模块访问具有联合安全性的服务,这在配置中非常繁重和困难,因此我相信它也适用于您.还有一个blog post.所有和所有模块允许您使用soap端点,而无需维护您通常在.net项目中找到的servicemodel配置文件和服务引用.
这是$svcEndpoint保存目标端点值的示例
>探测元数据交换端点
>为代理创建内存类型
>根据导入的配置创建端点
>创建一个频道(代理实例)
这是从github页面复制的示例代码
$wsImporter=New-WcfWsdlImporter -Endpoint $svcEndpoint -HttpGet
$proxyType=$wsImporter | New-WcfProxyType
$endpoint=$wsImporter | New-WcfServiceEndpoint -Endpoint $svcEndpoint
$channel=New-WcfChannel -Endpoint $endpoint -ProxyType $proxyType
该模块并不完美,所以如果缺少某些东西,请随时做出贡献.
我道歉可能被认为是不完整的答案,但这不符合评论.
内容总结
以上是互联网集市为您收集整理的c# – 使用Powershell调用WCF服务方法全部内容,希望文章能够帮你解决c# – 使用Powershell调用WCF服务方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。