使用 WebInvoke 在 WCF REST 服务主体中传递 XML 字符串
我是 WCF、REST 等的新手。我正在尝试编写一个服务和一个客户端。 我想将 xml 作为字符串传递给服务并获得一些响应。
我试图将正文中的 xml 传递给 POST 方法,但是当我运行客户端时,它只是挂起。
当我更改服务以接受参数作为 uri 的一部分时,它工作正常。 (当我将 UriTemplate 从“getString”更改为“getString/{xmlString}”并传递字符串参数时)。
我粘贴下面的代码。
Service
[ServiceContract]
public interface IXMLService
{
[WebInvoke(Method = "POST", UriTemplate = "getString", BodyStyle=WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string GetXml(string xmlstring);
}
// Implementaion Code
public class XMLService : IXMLService
{
public string GetXml(string xmlstring)
{
return "got 1";
}
}
Client
string xmlDoc1="<Name>";
xmlDoc1 = "<FirstName>First</FirstName>";
xmlDoc1 += "<LastName>Last</LastName>";
xmlDoc1 += "</Name>";
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(@"http://localhost:3518/XMLService/XMLService.svc/getstring");
request1.Method = "POST";
request1.ContentType = "application/xml";
byte[] bytes = Encoding.UTF8.GetBytes(xmlDoc1);
request1.GetRequestStream().Write(bytes, 0, bytes.Length);
Stream resp = ((HttpWebResponse)request1.GetResponse()).GetResponseStream();
StreamReader rdr = new StreamReader(resp);
string response = rdr.ReadToEnd();
有人可以指出其中有什么问题吗?
I'm a newbie to WCF, REST etc. I'm trying to write a service and a client.
I want to pass xml as string to the service and get some response back.
I am trying to pass the xml in the body to the POST method, but when I run my client, it just hangs.
It works fine when I change the service to accept the parameter as a part of the uri.
(when I change UriTemplate from "getString" to "getString/{xmlString}" and pass a string parameter).
I'm pasting the code below.
Service
[ServiceContract]
public interface IXMLService
{
[WebInvoke(Method = "POST", UriTemplate = "getString", BodyStyle=WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string GetXml(string xmlstring);
}
// Implementaion Code
public class XMLService : IXMLService
{
public string GetXml(string xmlstring)
{
return "got 1";
}
}
Client
string xmlDoc1="<Name>";
xmlDoc1 = "<FirstName>First</FirstName>";
xmlDoc1 += "<LastName>Last</LastName>";
xmlDoc1 += "</Name>";
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(@"http://localhost:3518/XMLService/XMLService.svc/getstring");
request1.Method = "POST";
request1.ContentType = "application/xml";
byte[] bytes = Encoding.UTF8.GetBytes(xmlDoc1);
request1.GetRequestStream().Write(bytes, 0, bytes.Length);
Stream resp = ((HttpWebResponse)request1.GetResponse()).GetResponseStream();
StreamReader rdr = new StreamReader(resp);
string response = rdr.ReadToEnd();
Could somebody please point out what's wrong in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更改您的操作合同以使用 XElement 和 Bare 的 BodyStyle
另外,我怀疑您的客户端代码应该包含(注意第一个 +=):
Change your operation contract to use an XElement and the BodyStyle of Bare
Additionally I suspect you client code should contain (note the first +=):
您仍然需要创建一个类:
您还可以使用 fiddler 检查序列化的 XML 是否可以作为参数传递。
You still need to create a class:
You can also use fiddler to check if the serialized XML could be passed as a parameter.
我相信问题在于您将
BodyStyle
设置为WrappedRequest
,这需要将传入的 XML 包装在
中定义您的服务契约的任何命名空间中的元素。如果您将BodyStyle
设置为Bare
并按照@Ladislav Mmka 在评论中建议的方式使用 XElement,那么您应该可以正常使用。I believe the problem is that you're setting the
BodyStyle
toWrappedRequest
which would require your incoming XML to be wrapped in a<GetXml>
element in whatever namespace your service contract is defined in. If you set theBodyStyle
toBare
and use XElement as @Ladislav Mmka suggested in the comment you should be good to go.您需要使用适当的 Microsoft XML 序列化命名空间将 XML 字符串包装在
标记中。这个问题之前已经被回答过,但是,我现在找不到它。You need to wrap your XML string in a
<string/>
tag with the appropriate Microsoft XML serialization namespace. This question has been answered before here on SO but, I can't find it at the moment.