WCF发布请求中的多个非url参数

时间:2012-05-16 10:49:19

标签: javascript wcf post

我有这样的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"
    }
}

但它不起作用!我尝试了一些不同的方法,但它们都不起作用:(任何帮助?

2 个答案:

答案 0 :(得分:1)

您可以如下所示实现上述目标:

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

如果要在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"}}

答案 1 :(得分: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等,因为我无法看到它们?