加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用Powershell调用WCF服务方法

发布时间:2020-12-15 20:56:01 所属栏目:百科 来源:网络整理
导读:我有一个WCF服务,它使用带有消息安全性的wsHttpBinding和作为 windows的clientcredentialtype,并且该服务有一个简单的方法 [OperationContract]string SayHello();public string SayHello() { return "HELLO"; } wsHttpBinding binding name="WSHttpBinding"
我有一个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 DOMAINgop

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 DOMAINgop

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

该模块并不完美,所以如果缺少某些东西,请随时做出贡献.

我道歉可能被认为是不完整的答案,但这不符合评论.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读