【问题标题】:What does the xml look like for a wrapped wcf post call?包装好的 wcf 后调用的 xml 是什么样的?
【发布时间】:2010-01-19 21:33:44
【问题描述】:

我有一个 wcf restful 服务,其操作合同包含两个值 - 一个 int 和一个字符串。这也是一个post call。

如果我使用 BodyStyle = WebMessageBodyStyle.Wrapped 来包装呼叫。我应该假设 xml 请求现在看起来像什么?

【问题讨论】:

    标签: xml wcf post


    【解决方案1】:

    我遇到了同样的问题,我相信有几种方法可以考虑这样做。如果还有更多,我很想听听其他人的意见。

    首先,我会告诉你我是如何做到这一点的。第一步是构建一个实用程序库,其中包含数据对象结构的类定义,并使用适当的 get 方法和构造函数来初始化对象。此外,您必须使类可序列化,即

    [Serializable]
    public class myDataObject
    {
        private int _n1;
        private string _s1;
    
        public myDataObject(int n, string s)
        {
            this._n1 = n;
            this._s1 = s;
        }
    
        public int getN1()
        {
            return this._n1;
        }
        public string getS1()
        {
            return this._s1;
        }
    }
    

    }

    将它放在一个库中很重要,这样您就可以从客户端和服务器端引用它。

    完成后,将您的 WCF 服务方法更改为类似于以下内容:

    [WebInvoke(Method = "POST", UriTemplate = "yourDesignedURI")]
    [OperationContract]
    public bool doSomething(myDataObject o)
    { 
        //implement your service logic, accessing the parameters from o
        int i = o.getN1();
        string s = o.getS1();
        //...etc
        return true;
    }
    

    完成后,发布您的服务,然后使用服务的帮助页面查看所需的 XML 语法。然后,您可以使用 Linq to XML 构建您的 XML,从而通过 Web 将您的数据对象作为请求发送。这消除了包装和公开您的请求 xml 语法的需要:

    <myDataObject xmlns="http://schemas.datacontract.org/2004/07/DemoXMLSerialization">
        <_n1>2147483647</_n1>
        <_s1>String content</_s1>
    </myDataObject>
    

    在您的客户端中引用实用程序库,稍后在您的数据中创建一个方法来调用服务方法,在您的方法中创建元素。

    public void callService(int n1, string s1)
        {
            myDataObject o = new myDataObject(n1, s1);
            string serviceURL = "yourBaseURL";
            string serviceURI = "yourDesignedURI";
    
            using (HttpClient client = new HttpClient(serviceURL))
            {
                client.DefaultHeaders.Add("Content-Type", "application/xml; charset=utf-8");
    
                XNamespace xns = "http://schemas.datacontract.org/2004/07/DemoXMLSerialization";
                XDocument xdoc = new XDocument(
                                    new XElement(xns + "myDataObject",
                                    new XElement(xns + "_n1", o.getN1())
                                    , new XElement(xns + "_s1", o.getS1())));
                using (HttpResponseMessage res = client.Post(serviceURI, HttpContent.Create(xdoc.ToString(SaveOptions.DisableFormatting))))
                {
                    res.EnsureStatusIsSuccessful();
                    //do anything else you want to get the response value
                }
            }
        }
    

    您可以做的另一件事是将您的 uri 重新设计为如下所示: 你设计的URI/{myInt}/{myString}

    然后使用 JSON 序列化来发送对象。如果添加行

    BodyStyle=WebMessageBodyStyle.Bare
    

    对于您的 WebInvoke 属性,帮助页面将公开完整的 URI 以便于查看。

    我确信可能也有一种方法可以使用 JQuery 来做到这一点。希望看到其他解决方案!

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 2021-01-24
      • 2016-05-30
      • 2016-12-29
      • 1970-01-01
      相关资源
      最近更新 更多