首页 新闻 会员 周边

关于WCF参数为自定义类类型时,无法通过GET请求来调问,请高手指教,谢谢!

0
悬赏园豆:5 [已关闭问题] 关闭于 2015-12-06 18:11

源码如下:

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "*",  RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        CompositeType GetDataUsingDataContract(CompositeType composite);

}


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

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {

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

通过如下POST方法,是可以正常调用WCF服务方法的

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery-1.4.4.min.js" type="text/javascript"></script>
</head>
<body>
    <input id="Text1" type="text" /><input id="Button1" type="button" value="button" />
    <fieldset>
        <legend>结果输出:</legend>
        <div id="resultbox">
        
        </div>
    </fieldset>
    <script type="text/javascript">
        $(function () {
            $("#Button1").click(function () {
                alert("click!");

                $.ajax({
                    url: "Service1.svc/GetDataUsingDataContract",
                    contentType:"text/json",
                    type: "post",
                    datatype: "json",
                    data:{ composite: { BoolValue: true, StringValue: $("#Text1").val()} },
                    success: function (r,s,x) {
                        $("#resultbox").append("<p>" + JSON.stringify(r) + "</p>");
                    }
                });
            });
        });
    </script>
</body>
</html>

但若将type改为get,则报如下错误:

约定“IService1”中的操作“GetDataUsingDataContract”具有名称为“composite”、类型为“WcfService1.CompositeType”的查询变量,但“QueryStringConverter”不能转换类型“WcfService1.CompositeType”。UriTemplate 查询值的变量必须为“QueryStringConverter”可转换的类型。

还请高手指教,非常感谢!

WCF
梦在旅途的主页 梦在旅途 | 初学一级 | 园豆:10
提问于:2015-11-27 16:18
< >
分享
所有回答(3)
0

WebInvoke改为WebGet试试

MrNice | 园豆:3450 (老鸟四级) | 2015-11-27 16:29

一样的

支持(0) 反对(0) 梦在旅途 | 园豆:10 (初学一级) | 2015-11-27 17:09
0

已自己解决:通过自定义MessageFormatter来实现GET请求,详见我的文章:

实现在GET请求下调用WCF服务时传递对象(复合类型)参数

梦在旅途 | 园豆:10 (初学一级) | 2015-12-06 18:10

POST 呢?

获取不到querystring

支持(0) 反对(0) 北京袋鼠 | 园豆:200 (初学一级) | 2016-07-22 21:39
0

UriTemplate = "/?composite={composite}"

上帝视角 | 园豆:202 (菜鸟二级) | 2016-08-18 15:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册