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

Multiple non-url parameters in WCF post request

本文关键字:参数 url 布请求 请求 WCF      更新时间:2023-09-26

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

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

您可以实现上述操作,如下所示:

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

如果要将多个对象序列化为 POST 请求的一部分,则需要将正文样式设置为 Wraped。

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

[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"}}

你能这样做吗?

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、属性类等,因为我看不到它们?