Wednesday, January 14, 2015

Consume wcf service with wsHttpBinding using PHP Soap Client

Recently I wrote blog post on accessing wcf using php. But that was with basicHttpBinding. Somehow after long research I found how to consume wcf with wsHttpBinding. Follow the codes,




using System;
using System.Linq;
using System.ServiceModel;
namespace WcfServiceLibrary1
{
[ServiceContract(Namespace = "http://tempuri.org")]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
}
view raw IService1.cs hosted with ❤ by GitHub

using System;
using System.Linq;
using System.ServiceModel;
namespace WcfServiceLibrary1
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
view raw Service1.cs hosted with ❤ by GitHub

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary1.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/test/WcfServiceLibrary1/Service1/" />
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" contract="WcfServiceLibrary1.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1" openTimeout="00:10:00"
closeTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
maxReceivedMessageSize="2147483647"
maxBufferPoolSize="2147483647">
<security mode="None" />
<reliableSession enabled="false" />
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:8733/test/WcfServiceLibrary1/Service1/"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
view raw App.config hosted with ❤ by GitHub

<?php
try {
$params = array("soap_version"=> SOAP_1_2,
"trace"=>1,
"exceptions"=>0,
);
$client = new SoapClient('http://localhost:8733/test/WcfServiceLibrary1/Service1/?wsdl',$params);
$actionHeader = new SoapHeader('http://www.w3.org/2005/08/addressing','Action','http://tempuri.org/IService1/GetData',true);
$client->__setSoapHeaders($actionHeader);
$params = new StdClass;
$params->value = 2;
$retval = $client->GetData($params);
if (is_soap_fault($retval)) {
trigger_error("SOAP Fault: (faultcode: {$retval->faultcode}, faultstring: {$retval->faultstring})", E_USER_ERROR);
}
else{
echo $retval->GetDataResult;
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
view raw wcf.php hosted with ❤ by GitHub

No comments:

Post a Comment