简体   繁体   English

WCF Web调用的错误请求

[英]bad request from WCF web invoke

All my GET endpoints work like a champ, but I'm trying to implement a webinvoke method="POST". 我所有的GET端点都像冠军一样工作,但我正在尝试实现webinvoke方法=“ POST”。

I think there is something wrong with my format, but I can't tell what it is, could someone help? 我认为我的格式有问题,但是我不知道它是什么,有人可以帮忙吗?

[ServiceContract]
interface iFlowRate
{
     [OperationContract]
     [WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}",RequestFormat= WebMessageFormat.Xml)]
     string AddFlowRate(string apikey,FlowRate flowrate);
}

when I debug this it doesnt even get into this method. 当我调试它时,它甚至都没有进入该方法。 I'm calling the service like this. 我这样打电话给服务。

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();

Any ideas? 有任何想法吗? BTW, using WCF 4.0, any help is much appreciated. 顺便说一句,使用WCF 4.0,非常感谢您的帮助。

This was totally killing me until moments ago where I finally stumbled upon the answer. 直到不久前我终于迷失了答案,这才完全杀死了我。

Here is the source for my findings: Wrapped BodyStyle in WCF Rest 这是我的发现的来源: WCF Rest中的包装的BodyStyle

But I'll cut to the good stuff. 但是我会尽力而为。

First, set the Namespace of your ServiceContract. 首先,设置ServiceContract的命名空间。

[ServiceContract(Namespace = "http://mytagservice")]

Now, I'm sure there is another way to get this to work but this is how I hacked it. 现在,我敢肯定还有另一种方法可以使它起作用,但这就是我的方法。 Set the BodyStyle to Wrapped. 将BodyStyle设置为Wrapped。 The default request format is XML so you don't need to set it here if you don't want to. 默认请求格式为XML,因此如果您不需要,则无需在此处进行设置。

 [WebInvoke(Method="POST",UriTemplate = "Add?apikey={apikey}", BodyStyle = WebMessageBodyStyle.Wrapped)]

Then change your xml to include the wrapper and namespace. 然后更改您的xml以包括包装器和名称空间。 Be extra careful of the tag names as they are case sensitive. 标签名称要特别小心,因为它们区分大小写。

string postData = "<AddFlowRate xmlns='http://mytagservice'><flowrate><wellname>wellname</wellname></flowrate></AddFlowRate>";

Because it's using the wrapped message type this solution will work for as many parameters as you can want. 因为它使用包装的消息类型,所以该解决方案将可以使用尽可能多的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM