使用 WebInvoke 在 WCF REST 服务主体中传递 XML 字符串

发布于 2024-11-14 01:10:01 字数 1479 浏览 7 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

離人涙 2024-11-21 01:10:01

更改您的操作合同以使用 XElement 和 Bare 的 BodyStyle

[WebInvoke(Method = "POST", 
    UriTemplate = "getString", 
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml, 
    ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string GetXml(XElement xmlstring);

另外,我怀疑您的客户端代码应该包含(注意第一个 +=):

string xmlDoc1="<Name>";
xmlDoc1 += "<FirstName>First</FirstName>";
xmlDoc1 += "<LastName>Last</LastName>";
xmlDoc1 += "</Name>";

Change your operation contract to use an XElement and the BodyStyle of Bare

[WebInvoke(Method = "POST", 
    UriTemplate = "getString", 
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml, 
    ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string GetXml(XElement xmlstring);

Additionally I suspect you client code should contain (note the first +=):

string xmlDoc1="<Name>";
xmlDoc1 += "<FirstName>First</FirstName>";
xmlDoc1 += "<LastName>Last</LastName>";
xmlDoc1 += "</Name>";
亚希 2024-11-21 01:10:01

您仍然需要创建一个类:

public class Test
{

    public string xmlstring{ get; set; }

}

您还可以使用 fiddler 检查序列化的 XML 是否可以作为参数传递。

You still need to create a class:

public class Test
{

    public string xmlstring{ get; set; }

}

You can also use fiddler to check if the serialized XML could be passed as a parameter.

梦醒灬来后我 2024-11-21 01:10:01

我相信问题在于您将 BodyStyle 设置为 WrappedRequest ,这需要将传入的 XML 包装在 中定义您的服务契约的任何命名空间中的元素。如果您将 BodyStyle 设置为 Bare 并按照@Ladislav Mmka 在评论中建议的方式使用 XElement,那么您应该可以正常使用。

I believe the problem is that you're setting the BodyStyle to WrappedRequest 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 the BodyStyle to Bare and use XElement as @Ladislav Mmka suggested in the comment you should be good to go.

落花随流水 2024-11-21 01:10:01

您需要使用适当的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文