Using Powershell to Call a WCF Service Method

I have a WCF service that uses wsHttpBinding with message protection and clientcredentialtype as windows, and the service has a simple method

[OperationContract]
string SayHello();

public string SayHello()
    {
        return "HELLO";
    } 

<wsHttpBinding>
    <binding name="WSHttpBinding">          
      <security mode="Message">
        <message clientCredentialType="Windows" />
      </security>
    </binding>
  </wsHttpBinding>

I am trying to execute the below on powershell (version> = 2) and I am getting the following error

$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

However, when I changed the binding to use basicHttpBinding, it works fine

<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   

Is there something different that I need to do in my script when using wsHttpBinding?

Thanks in advance.

wsHttpBinding WCF. , , powershell script, . BasicHttpBinding Windows Authentication script. .

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)
+4
1

PowerShell WcfPS, - , . , , , . . , .net.

, $svcEndpoint

  1. ( )

, GitHub

$wsImporter=New-WcfWsdlImporter -Endpoint $svcEndpoint -HttpGet
$proxyType=$wsImporter | New-WcfProxyType
$endpoint=$wsImporter | New-WcfServiceEndpoint -Endpoint $svcEndpoint
$channel=New-WcfChannel -Endpoint $endpoint -ProxyType $proxyType

, , - .

, .

0

Source: https://habr.com/ru/post/1610677/


All Articles