如何在WCF中读取查询字符串值?

时间:2013-06-14 13:04:37

标签: wcf

这可能非常简单,但我找不到在WCF休息服务中读取查询字符串值的方法。我试过以下但没有快乐

HttpContext.Current.Request.QueryString["name"]

1 个答案:

答案 0 :(得分:1)

这样的事情对你有用。你需要使用UriTemplate。以下是WCF服务

[ServiceContract]
  interface IPing
  {
    [OperationContract]
    [WebInvoke(Method="POST", UriTemplate="stuff?n={name}&q={quantity}")]
    void AddStuff(string name, string quantity, Stream data);
  }

  class PingService : IPing
  {
    public void AddStuff(string name, string quantity, Stream data)
    {
      Console.WriteLine("{0} : {1}", name, quantity);
      Console.WriteLine("Data ...");
      using (StreamReader sr = new StreamReader(data))
      {
        Console.WriteLine(sr.ReadToEnd());
      }
    }
  }

和客户

 static void Main(string[] args)
    {
      WebRequest req = WebRequest.Create("http://localhost:9000/ping/stuff?n=rich&q=20");
      req.Method = "POST";
      req.ContentType = "text/html";
      using (StreamWriter sw = new StreamWriter(req.GetRequestStream()))
      {
        sw.WriteLine("Hello");
      }

      req.GetResponse();
    }