Sunday, January 8, 2012

Creating first PHP program (in Windows platform)

Creating a PHP client and consuming WCF service
1) Download PHP for windows and install it.
2) Run” PHP.exe –i “ to see the initialization settings.
3) You ll find PHP expects PHP.ini file to be present in C:\Windows. So first rename PHP.ini development to PHP.ini and copy it to C:\Windows
4) Modify C:\Windows\PHP.ini to include some settings for PHP runtime. Most of the settings are present there but commented with semi-colon (;) prefix. Just uncomment to include that setting.
a) uncomment include_path
b)uncomment extension=php_soap.dll (as we ll be needing to making SOAP calls)
c)uncomment extension_dir = "c:\php\ext" (set your extension dir. Mine is C:\PHP\ext
       5) Create the PHP program as follows and save it in LMSClient.php (Read the program. It ll make sense):


// Create a new soap client based on the service's metadata (WSDL)
$client = new SoapClient("http://localhost:24802/LMSService.svc?wsdl");

   //echo("<br />Dumping client object:<br />");
   //echo("<br /><br />");
   //echo("Dumping client functions:<br />");
   //echo("<br /><br />");

//$result = $client->Test();
$result = $client->GetUserById(1);

//echo $result->TestResult;
echo $result->GetUserByIdResult->Firstname;
echo $result->GetUserByIdResult->Lastname;


6) Run the program by php -f c:\php\MyPHPFiles\LMSClient.php

WCF Metadata publishing and use of DATACONTRACTS

No Datacontracts are not used so they can consumed outside .NET like JAVA / PHP

1)      If enabled, WCF service metadata could be found by opening the http://...svc?wsdl
2)      In there, there is a section types, which refers to an xsd for example http://localhost/LMSService/LMSService.svc?xsd=xsd2
3)      That xsd has all the data types/structures for input and output.
4)      The funny part is, it has all the public properties used in the input/output even if you don’t have DataContract attributes.

So the question is What’s the point of DataContract/DataMember attributes. The above xsd has the datatypes/structures and a PHP/Java client could consume that. The answer is for more control on which properties to be published. So without DataContract/DataMember attribute, its ALL or NONE for all public properties publishing. (Note: You can all together switch off WCF Metadata publishing). With DataContract/DataMember, we have some properties public to be used in our own projects/solution yet not expose it the outside world by not adorning it with DataMember attribute.

Question- ) How to switch off/on WCF metadata publishing

        <behavior name="metadataBehavior">
          <!—The answer is in the comment line below à
          <!-- 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"/>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      <service name="SRO.LMS.Services.LMSService" behaviorConfiguration="metadataBehavior">
            <add baseAddress=
                 "http://localhost/LMSService" />
        <endpoint address="" binding="basicHttpBinding" contract="SRO.LMS.Services.ILMSService"/>
        <endpoint address="mex"  binding = "mexHttpBinding" contract = "IMetadataExchange"/>