开发者

How can I post plain text to a WCF service i.e. not wrapped in an XML tag?

Here's my problem :

I have to build a webservice which accepts plain text in the body of the httppost.

I want to use wcf but it looks like wcf is only made for xml/json.

Does anyone have a method which i can use to post plain text through a http post?

Note that i cannot use soap or wrap the text inside xml tags, i have to follow certain guideli开发者_如何学Cnes to be compatible with existing service consumers.

Thanks in advance.


The trick is you call your method with param Stream xxx

In the iContract

[WebInvoke(Method = "POST",
    UriTemplate = "InterpSvcTest",
    BodyStyle = WebMessageBodyStyle.Bare)] // this is key for combining params with Stream

Stream InterpretORUTest(Stream ORU);

In your WebService class

    public Stream InterpretORUTest(Stream ORUMessageStream)
    {
        string hl7 = StreamToString(ORUMessageStream);

        return StringToStream(hl7);

    }

   public Stream StringToStream(string s)
        {
            return new MemoryStream(Encoding.UTF8.GetBytes(s));

        }

        public string StreamToString(Stream ms)
        {
            try
            {
                using (ms)
                {
                    var sr = new StreamReader(ms);
                    var myStr = sr.ReadToEnd();
                    sr.Dispose();
                    return myStr;
                };
            }
            catch (Exception e)
            {
                c.WriteToLog("StreamToString error - " + e.Message);
                return "";

            }

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜