【问题标题】:Passing a class as parameter in RESTful WCF Service在 RESTful WCF 服务中将类作为参数传递
【发布时间】:2011-10-10 15:04:14
【问题描述】:

在我的 RESTful WCF Serice 中,我需要传递一个类作为 URITemplate 的参数。 我能够将一个字符串或多个字符串作为参数传递。 但是我有很多字段可以传递给 WCF 服务。 所以我创建了一个类并将所有字段添加为属性,然后 我想将这个类作为一个参数传递给 URITemplate。 当我尝试将类传递给 URITemplate 时,出现错误 “路径段必须具有类型字符串”。它不接受类作为参数。 知道如何将类作为参数传递。 这是我的代码(inputData 是类)


    [OperationContract]
    [WebGet(UriTemplate = "/InsertData/{param1}")]
    string saveData(inputData param1);

【问题讨论】:

    标签: wcf rest service uritemplate


    【解决方案1】:

    您实际上可以在 GET 请求中传递复杂类型(类),但您需要通过 QueryStringConverter“教”WCF 如何使用它。但是,您通常不应该这样做,尤其是在会更改服务中某些内容的方法中(GET 应该用于只读操作)。

    下面的代码显示了在 GET(使用自定义 QueryStringConverter)和 POST(应该采用的方式)中传递复杂类型。

    public class StackOverflow_6783264
    {
        public class InputData
        {
            public string FirstName;
            public string LastName;
        }
        [ServiceContract]
        public interface ITest
        {
            [OperationContract]
            [WebGet(UriTemplate = "/InsertData?param1={param1}")]
            string saveDataGet(InputData param1);
            [OperationContract]
            [WebInvoke(UriTemplate = "/InsertData")]
            string saveDataPost(InputData param1);
        }
        public class Service : ITest
        {
            public string saveDataGet(InputData param1)
            {
                return "Via GET: " + param1.FirstName + " " + param1.LastName;
            }
            public string saveDataPost(InputData param1)
            {
                return "Via POST: " + param1.FirstName + " " + param1.LastName;
            }
        }
        public class MyQueryStringConverter : QueryStringConverter
        {
            public override bool CanConvert(Type type)
            {
                return (type == typeof(InputData)) || base.CanConvert(type);
            }
            public override object ConvertStringToValue(string parameter, Type parameterType)
            {
                if (parameterType == typeof(InputData))
                {
                    string[] parts = parameter.Split(',');
                    return new InputData { FirstName = parts[0], LastName = parts[1] };
                }
                else
                {
                    return base.ConvertStringToValue(parameter, parameterType);
                }
            }
        }
        public class MyWebHttpBehavior : WebHttpBehavior
        {
            protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
            {
                return new MyQueryStringConverter();
            }
        }
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
            host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "").Behaviors.Add(new MyWebHttpBehavior());
            host.Open();
            Console.WriteLine("Host opened");
    
            WebClient client = new WebClient();
            Console.WriteLine(client.DownloadString(baseAddress + "/InsertData?param1=John,Doe"));
    
            client = new WebClient();
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            Console.WriteLine(client.UploadString(baseAddress + "/InsertData", "{\"FirstName\":\"John\",\"LastName\":\"Doe\"}"));
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    

    【讨论】:

    【解决方案2】:

    只能通过 POST 或 PUT 请求 (WebInvoke) 传递类(数据协定)。 GET 请求只允许将每个必须是UriTemplate 一部分的简单类型映射到方法中的参数。

    【讨论】:

    • 我尝试使用 webinvoke 和 post 方法。仍然无法正常工作。这是我的代码 ************ [OperationContract] [WebInvoke(UriTemplate="/InsertData/param1",Method="POST")] string saveData(inputData param1);这是我在浏览器中将值传递给 WCF 服务localhost:64475/RESTService1.svc/insertData/… 的方式
    • 类必须在请求体中。不在网址中。
    • 有可能吗,能否提供一些代码或示例。谢谢你的时间
    猜你喜欢
    • 2015-06-03
    • 2012-07-20
    • 2014-03-04
    • 2011-08-07
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2012-07-03
    • 2014-03-10
    相关资源
    最近更新 更多