【问题标题】:Wcf Client: Passing XML string in the WCF REST service using WebInvokeWcf 客户端:使用 WebInvoke 在 WCF REST 服务中传递 XML 字符串
【发布时间】:2012-04-17 20:17:20
【问题描述】:

没有显示方法的参数,它在浏览器中工作,即http://localhost:2617/UserService.svc/test

当我添加一个参数时,我也无法浏览它。

我有以下合同。

[ServiceContract]
public interface IUserService
{
    [OperationContract]
    [WebInvoke(Method="PUT",UriTemplate = "/tes/{name}",   
    BodyStyle=WebMessageBodyStyle.WrappedRequest)]
    string Display(string name);
}

public string Display(string name)
{
        return "Hello, your test data is ready"+name;
}

我正在尝试使用以下代码调用

         string url = "http://localhost:2617/UserService.svc/test"; //newuser
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        string xmlDoc1 = "<Display xmlns=\"\"><name>shiva</name></Display>";
        req.Method = "POST";
        req.ContentType = "application/xml";
        byte[] bytes = Encoding.UTF8.GetBytes(xmlDoc1);
        req.GetRequestStream().Write(bytes, 0, bytes.Length); 

        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        Stream responseStream = response.GetResponseStream();
        var streamReader = new StreamReader(responseStream);

        var soapResonseXmlDocument = new XmlDocument();
        soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());

我无法为此获得输出。请帮助我。

【问题讨论】:

  • 您在客户端的方法是“POST”,但在服务器端您有 Method="PUT" - 我原以为它们必须相同 - 尝试将您的服务器更改为 POST也许?
  • 我也更改了 POST... 我尝试了不同的方式,但它不起作用..
  • 你没有声明命名空间,所以命名空间应该是tempuri.org - 而不是空白。
  • 您是否有任何理由希望名称成为查询字符串。我认为当您执行 POST/PUT 时,您不能有查询字符串参数。

标签: wcf wcf-data-services wcf-ria-services wcf-client wcf-rest-starter-kit


【解决方案1】:

您的代码中有些地方不太正确。

客户

在客户端上,您需要将命名空间指定为 tempuri,因为您没有明确声明一个,所以您的客户端代码需要是这样的:

string url = "http://localhost:2617/UserService.svc/test"; //newuser
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string xmlDoc1 = "<Display xmlns=\"http://tempuri.org/\"><name>shiva</name></Display>";
req.Method = "POST";
req.ContentType = "application/xml";
byte[] bytes = Encoding.UTF8.GetBytes(xmlDoc1);
req.GetRequestStream().Write(bytes, 0, bytes.Length);

HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream responseStream = response.GetResponseStream();
var streamReader = new StreamReader(responseStream);

var soapResonseXmlDocument = new XmlDocument();
soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());

服务

在服务上,UriTemplate 不太正确 - 您指定了 /tes/{name},因此会期待像 http://localhost:2617/UserService.svc/tes/shiva 这样的 URL,但您希望将 XML 数据发布到正文中,因此您应该更改它到UriTemplate = "/test"(我假设你的意思是测试而不是你的问题中的测试)。

此外,如果您想向其 POST 数据,该方法应该是 POST(客户端需要匹配服务,我假设您在客户端拥有的就是您想要的)。

因此,总而言之,您的 IUserService 应该如下所示:

[ServiceContract]
public interface IUserService
{        
    [OperationContract]
    [WebInvoke(Method = "POST",
               UriTemplate = "/test",
               BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string Display(string name);
}

【讨论】:

    【解决方案2】:

    你仍然需要创建一个类

    public class Test
    {
    
        public string name { get; set; }
    
    }
    

    您也可以使用 fiddler 来检查 {name:999} 是否可以作为参数传递。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 2011-08-07
      相关资源
      最近更新 更多