Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:58:08+00:00 2026-06-10T18:58:08+00:00

I am having some trouble trying to write code within a RESTful WCF service.

  • 0

I am having some trouble trying to write code within a RESTful WCF service. I have made a method available to a calling client application and I am receiving a message that is of the format Ax27834…… which is a Base64 Binary message. The issue is that following receiving this I need to be able to convert it back to the original xml version of that message that was sent from the client. How can I achieve this in the code snippet below. On line 6 below you will see where the code needs to go. I have searched for a solution but not managed to find anything suitable. I have to receive a message rather than a stream.

I should highlight that the service works fine in the respect of receiving the request. I am just struggling to get the message into a form that I can use.

The receiving code

public Message StoreMessage(Message request)
{
    //Store the message
    try
        {
        string message = [NEED SOLUTION HERE]

        myClass.StoreNoticeInSchema(message, DateTime.Now);
    }
    catch (Exception e)
    {
        log4net.Config.XmlConfigurator.Configure();

        ILog log = LogManager.GetLogger(typeof(Service1));

        if (log.IsErrorEnabled)
        {
            log.Error(String.Format("{0}: Notice was not stored. {1} reported an exception. {2}", DateTime.Now, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, e.Message));
        }
    }

    XElement responseElement = new XElement(XName.Get("elementName", "url"));

    XDocument resultDocument = new XDocument(responseElement);

    return Message.CreateMessage(OperationContext.Current.IncomingMessageVersion, "elementName", resultDocument.CreateReader());
}

The client code

public string CallPostMethod()
    {
        const string action = "StoreNotice/New";

        TestNotice testNotice = new TestNotice();

        const string url = "http://myaddress:myport/myService.svc/StoreNotice/New";

        string contentType = String.Format("application/soap+xml; charset=utf-8; action=\"{0}\"", action);
        string xmlString = CreateSoapMessage(url, action, testNotice.NoticeText);

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

        ASCIIEncoding encoding = new ASCIIEncoding();

        byte[] bytesToSend = encoding.GetBytes(xmlString);

        request.Method = "POST";
        request.ContentLength = bytesToSend.Length;
        request.ContentType = contentType;

        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(bytesToSend, 0, bytesToSend.Length);
            requestStream.Close();
        }

        string responseFromServer;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        using (Stream dataStream = response.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(dataStream))
                responseFromServer = reader.ReadToEnd();
            dataStream.Close();
        }

        XDocument document = XDocument.Parse(responseFromServer);
        string nameSpace = "http://www.w3.org/2003/05/soap-envelope";
        XElement responseElement = document.Root.Element(XName.Get("Body", nameSpace))
                                             .Element(XName.Get(@action + "Response", "http://www.wrcplc.co.uk/Schemas/ETON"));


        return responseElement.ToString();
    }

Code to create SOAP message

  protected string CreateSoapMessage(string url, string action, string messageContent)
    {
        return String.Format(
 @"<?xml version=""1.0"" encoding=""utf-8""?>
 <soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
 xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope""><soap12:Body>{0}</soap12:Body>
</soap12:Envelope>
", messageContent, action, url);
    }

NOTE: The TestNotice() object contains a large xml string which is the body of the message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:58:09+00:00Added an answer on June 10, 2026 at 6:58 pm

    With a Message object you usually use GetReaderAtBodyContents() to get an XML representation of the body content, unless you know what type the body has then you can use GetBody<>. Try using those to get the string, and then decode it if you still need to. Which you can do as follows:

    byte[] encodedMessageAsBytes = System.Convert.FromBase64String(requestString);
    
    string message = System.Text.Encoding.Unicode.GetString(encodedMessageAsBytes);
    

    From there you can reconstruct the xml from the string

    Edit: to answer the last part from the comment, the content type should be: text/xml

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble with this code. I am trying to write a function
I'm having some trouble trying to incorporate a read and write statement into evaluateStatementInner
I am having some trouble trying to checkout the BitSharp source code using SVN.
I'm trying to write a Greasemonkey script for Facebook and having some trouble with
I'm not a php expert so I'm having some trouble trying to write a
I am trying to write a recursive function within a class, but have some
I am having some trouble with a linq query I am trying to write.
I'm trying to write my first iPhone app and i'm having some trouble with
I am having some trouble with the code below. I am trying to figure
I am having some trouble trying to figure out, how to accomplish the following.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.