0

我有这样的 WCF 服务:

public IEnumerable<int> AddProperty(string productId, string propertyClass, Property properties)

我用以下数据调用它:

{
    "Class":{"Name":"aoeu","Restricted":false,"Type":"ScalarProperty"},
    "DisplayName":"aoeu",
    "Id":"560",
    "Value":"aoeu"
}

它工作得很好。现在我想像这样向Property服务中添加另一个:

public IEnumerable<int> AddProperty(string productId, string propertyClass, Property properties, Property properties2)

我应该传递什么数据?我试过这个:

{
    properties: {
        "Class":{"Name":"aoeu","Restricted":false,"Type":"ScalarProperty"},
        "DisplayName":"aoeu",
        "Id":"560",
        "Value":"aoeu"
    },
    properties2: {
        "Class":{"Name":"aoeu","Restricted":false,"Type":"ScalarProperty"},
        "DisplayName":"aoeu",
        "Id":"560",
        "Value":"aoeu"
    }
}

但它没有用!我尝试了一些不同的方法,但它们都不起作用:(有什么帮助吗?

4

2 回答 2

1

可以实现如上图:

[WebInvoke(BodyStyle=WebMessageBodyStyle.Wrapped,UriTemplate = "MultipleProperties?productId={productId}&propertyClass={propertyClass}")]    
public IEnumerable<int> AddProperty(string productId, string propertyClass, Property properties, Property properties2)

当您想要序列化超过 1 个对象作为 POST 请求的一部分时,您需要将主体样式设置为 Wrapped。

现在假设您的 Property 类定义如下:

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

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

原始 Http 请求如下所示:

POST http://locahost/XMLService/Service1.svc/AddProperty?productId=1&propertyClass=2 HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Content-Length: 134

{"properties": {"boolvalue": "true", "stringvalue": "str"}, "properties2" :{"boolvalue": "true", "stringvalue":"str2"}}
于 2012-05-16T11:53:36.693 回答
0

你能这样做吗?

public IEnumerable<int> AddProperty(string productId, string propertyClass, Property[] properties);

进而

[ 
    { 
        "Class":{"Name":"aoeu","Restricted":false,"Type":"ScalarProperty"}, 
        "DisplayName":"aoeu", 
        "Id":"560", 
        "Value":"aoeu" 
    }, 
    { 
        "Class":{"Name":"aoeu","Restricted":false,"Type":"ScalarProperty"}, 
        "DisplayName":"aoeu", 
        "Id":"560", 
        "Value":"aoeu" 
    } 
] 

我假设您只向我们展示了属性代码而不是 productId、propertyClass 等,因为我看不到它们?

于 2012-05-16T10:59:39.873 回答