使用dataContract作为PHP的参数来使用WCF服务

时间:2011-12-03 11:10:05

标签: php wcf soap

我正在尝试从PHP客户端使用WCF服务但没有成功。 我无法使用具有“DataContract”对象作为参数的服务方法。 这是我的代码:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

下面是“CompositeType”合同:

[DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

以下是我创建的PHP客户端

$obj = new stdClass();
$obj->BoolValue = true;
$obj->StringValue ="Sandeep";
$parameter= array("CompositeType"=>$obj);
echo "\n";
$client = new SoapClient('http://localhost:2051/Service1.svc?wsdl');
$obj1 = $client->GetDataUsingDataContract($parameter);
print_r($obj1);

当我使用上面的内容时,DataContarct作为Null传递给WCF。

解决方案是什么?

1 个答案:

答案 0 :(得分:3)

$parameter= array("CompositeType"=>$obj); 

更改Composite中的CompositeType(=与接口中的变量名完全相同)