Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have create WCF service Project. I can consume from .NET framework. I can pass a variable and can get return variable while consume from PHP. But I cannot consume that service when pass class object to service from PHP.

Iservice.cs
C#
namespace WCFTest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string getVal(Student[] st);

        [OperationContract]
        string PassMember(CompMember cmp);
    }

    public class ALSubjects
    {
        string SID;
        string SName;
        string sResult;

        public string SubID
        {
            get { return SID; }
            set { SID = value; }
        }

        public string SubName
        {
            get { return SName; }
            set { SName = value; }
        }

        public string SubResult
        {
            get { return sResult; }
            set { sResult = value; }
        }
    }

    public class Student
    {
        string stID;
        string stName;
        int stAge;
        ALSubjects[] Als = new ALSubjects[] { new ALSubjects()};

        public string StudID
        {
            get { return stID; }
            set { stID = value; }
        }

        public string StudName
        {
            get { return stName; }
            set { stName = value; }
        }

        public int studAge
        {
            get { return stAge; }
            set { stAge = value; }
        }

        public ALSubjects[] Alsubject
        {
            get { return Als; }
            set { Als = value; }
        }
    }

    public class CompMember
    {
        string MemID;
        string MemName;
        int MemAge;

        public string MemberID
        {
            get { return MemID; }
            set { MemID = value; }
        }

        public string MemberName
        {
            get { return MemName; }
            set { MemName = value; }
        }

        public int MemberAge
        {
            get { return MemAge; }
            set { MemAge = value; }
        }
    }
}


Service1.cs
C#
namespace WCFTest
{   
    public class Service1 : IService1
    {
        public string getVal(Student[] stu)
        {
            string t;
            Student[] st = new Student[] {new Student()};
            for (int i = 0; i < st.Length; i++)
            {
                st[i].StudID = stu[i].StudID;
                st[i].StudName = stu[i].StudName;
                st[i].studAge = stu[i].studAge;
                //st[i].Alsubject = stu[i].Alsubject;
                st[i].Alsubject=new ALSubjects[] {new ALSubjects()};
                for (int j = 0; j < st[i].Alsubject.Length; j++)
                {
                    st[i].Alsubject[j].SubID = stu[i].Alsubject[j].SubID;
                }
            }
            //t = st[0].StudName;
            t = st[0].Alsubject[0].SubID;
            return t;
        }

        public string PassMember(CompMember mem)
        {
            CompMember cmp = new CompMember();

            cmp.MemberID = mem.MemberID;
            cmp.MemberName = mem.MemberName;
            cmp.MemberAge = mem.MemberAge;

            string j = "You Entered : "+cmp.MemberID+" : "+cmp.MemberName+" : "+cmp.MemberAge.ToString();
            return j;
        }
    }
}

App.config of service project
XML
<?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>
    <bindings />
    <client />
    <services>
      <service name="WCFTest.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFTest/Service1/"   />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address=""  binding="basicHttpBinding" contract="WCFTest.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>
          <!-- 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>



web.config of hosted project

XML
<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
        <endpoint address="" binding="basicHttpBinding"
          contract="WCFTest.IService1" listenUriMode="Explicit"/>
        <!--<endpoint
          address="" binding="wsHttpBinding"
           contract="WCFTest.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>-->        
        <endpoint address="mex" binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>  
</configuration>



Consuming file of CS
C#
class Program
    {
        Student[] st = null;
        CompMember cmp = new CompMember();
        Service1Client proxy = new Service1Client("BasicHttpBinding_IService1");

        string mID="/", mName="/";
        int mAge=0;

        // pass student object to service
        string gval()
        {
            st = new Student[] { new Student()};
            for (int i = 0; i < st.Length; i++)
            {
                st[i].StudID = "001";
                st[i].StudName = "Lal";
                st[i].studAge = 20;
                st[i].Alsubject = new ALSubjects[] { new ALSubjects()};
                for (int j = 0; j < st[i].Alsubject.Length; j++)
                {
                    st[i].Alsubject[j].SubID = "55";
                    st[i].Alsubject[j].SubName = "Maths";
                    st[i].Alsubject[j].SubResult = "A";
                }
            }

            string t = proxy.getVal(st);
            return t;
        }
        
        //Pass member details to service and get all pass value in single string 
        string PassMemb()
        {
            cmp = new CompMember();
            cmp.MemberID = "4523";
            cmp.MemberName = "Jakson";
            cmp.MemberAge = 30;
            string j = proxy.PassMember(cmp);
            return j;
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            //string j = p.test(10);
            //string g = p.gval();
            //string vx = p.PassMemb();
            p.met();
            //Console.WriteLine(j+"\n"+g+"\n"+vx+"\n\n");
            Console.WriteLine(p.mID + " \n" + p.mName + " \n"+p.mAge.ToString());
            Console.ReadLine();
        }
    }


This is PHP code which used to call WCF service
PHP
<?php

try{
  $client = new SoapClient("http://localhost:1236/WebSite/Service.svc?wsdl");

// Set parameters
$parms['CompMember']['MemberAge'] = 'Member Name';
$parms['CompMember']['MemberID'] ="123";
$parms['CompMember'] ['MemberName']= 25;

// Call web service PassMember methordd
 $webService = $client-> PassMember($parms);
 $wsResult = $webService->PassMemberResult();
 
// print response
 print_r($wsResult);
       
       } catch (Exception $e) {
       echo 'Caught exception:',  $e->getMessage(), "\n";
}  
// -----------------------------------------------------

try{
  $client = new SoapClient("http://localhost:1236/WebSite/Service.svc?wsdl");
 


// Set parameters
$parms['Student']['Alsubject']['SubID']  = 'AL002';
$parms['Student']['Alsubject']['SubName']  = 'MATHS';
$parms['Student']['Alsubject']['SubResult']  = 'A';
$parms['Student']['StudID'] ="123";
$parms['Student'] ['StudName']= 'Student Name';
$parms['Student'] ['studAge']= 25;

// Call web service PassMember methordd
 $webService = $client-> getVal($parms);
 $wsResult = $webService->getValResult();
 
// print response
 print_r($wsResult);
       
       } catch (Exception $e) {
       echo 'Caught exception:',  $e->getMessage(), "\n";
}
?>


When we call service via PHP We have exception
"Caught exception: Object reference not set to an instance of an object."

If someone know solve this problem Please let me know.
Thanks,
Chamara.
Posted
Updated 17-Aug-12 1:10am
v2

Hi Guys,

Please find below URL for another solution.


https://stackoverflow.com/questions/8387172/consuming-a-wcf-service-from-php/28713501#28713501

Regards,
Rahul Soni
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900