我所有的GET端点都像冠军一样工作,但我正在尝试实现webinvoke方法=“POST”。
我认为我的格式有问题,但我不知道它是什么,有人可以帮忙吗?
[ServiceContract]
interface iFlowRate
{
[OperationContract]
[WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}",RequestFormat= WebMessageFormat.Xml)]
string AddFlowRate(string apikey,FlowRate flowrate);
}
当我调试它时,它甚至没有进入这个方法。 我正在调用这样的服务。
string postData = "<FlowRate ><wellname>wellname</wellname></FlowRate>";
//Setup the http request.
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentLength = postData.Length;
request.ContentType = "application/xml";
request.KeepAlive = true;
StreamWriter streamwriter = new
StreamWriter(request.GetRequestStream());
streamwriter.Write(postData);
streamwriter.Close();
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Read the response
StreamReader responsereader = new StreamReader(response.GetResponseStream());
string strResponseData = responsereader.ReadToEnd();
有什么想法吗?顺便说一下,使用WCF 4.0,非常感谢任何帮助。
答案 0 :(得分:1)
直到我刚才偶然发现答案时,这完全杀了我。
以下是我的调查结果的来源:Wrapped BodyStyle in WCF Rest
但我会切入好东西。
首先,设置ServiceContract的命名空间。
[ServiceContract(Namespace = "http://mytagservice")]
现在,我确信还有另一种方法可以让它发挥作用,但这就是我攻击它的方式。将BodyStyle设置为Wrapped。默认请求格式是XML,因此如果您不想这样做,则无需在此处进行设置。
[WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}", BodyStyle = WebMessageBodyStyle.Wrapped)]
然后更改您的xml以包含包装器和命名空间。请注意标签名称,因为它们区分大小写。
string postData = "<AddFlowRate xmlns='http://mytagservice'><flowrate><wellname>wellname</wellname></flowrate></AddFlowRate>";
因为它正在使用包装的消息类型,所以此解决方案将适用于您想要的任意数量的参数。