In my last post I provided an example of PHP calling a .NET windows communication foundation web service. The PHP invoked the .NET service with no parameters, getting the time on server.
Sometime (well, most of the times) you need to pass parameters to the .NET web service. for example you might want to pass a client ID and get back its account balance.
Here is what you do:
First create a "out of the box" windows communication foundation (WCF) WCF service library project ( I used Visual Studio 2008) .
Then, modify the project files according to the files posted bellow.
My server side WCF code looks like this:
1) Service1.cs –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceLibrary2
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
2) IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceLibrary2
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
}
The server side WCF application configuration (App.config) file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!– When deploying the service library project, the content of the config file must be added to the host’s
app.config file. System.Configuration does not support config files for libraries. –>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary2.Service1" behaviorConfiguration="WcfServiceLibrary2.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary2/Service1/" />
</baseAddresses>
</host>
<!– Service Endpoints –>
<!– Unless fully qualified, address is relative to base address supplied above –>
<endpoint address ="" binding="basicHttpBinding" contract="WcfServiceLibrary2.IService1">
<!–
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
–>
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!– Metadata Endpoints –>
<!– The Metadata Exchange endpoint is used by the service to describe itself to clients. –>
<!– This endpoint does not use a secure binding and should be secured or removed before deployment –>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceLibrary2.Service1Behavior">
<!– To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment –>
<serviceMetadata httpGetEnabled="True"/>
<!– To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information –>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Note my blog post about the importance of setting this WCF service binding to basicHttpBinding
The PHP side of the application code looks like this:
<h1>
<?php
try{
$client = new
SoapClient(
"http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary2/Service1/?wsdl"
);
$params = array(‘value’=>"3");
$webService = $client->GetData($params);
$wsResult = $webService->GetDataResult;
print $wsResult;
} catch (Exception $e) {
print ‘Caught exception: ‘. $e->getMessage(). "\n";
}
?>
</h1>
This is very similar to the previous example of PHP to WCF communication, only here the PHP passes an array of parameters to the WCF web service:
$params = array(‘value’=>"3");
$webService = $client->GetData($params);
In my next post I will show how to investigate the WCF service and learn the parameters it requires and their soup names.