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 9105409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:06:02+00:00 2026-06-17T02:06:02+00:00

I’m working on a WCF RESTful web service hosted in IIS. I’m currently working

  • 0

I’m working on a WCF RESTful web service hosted in IIS. I’m currently working on a fairly simple post request, sending the following XML to the endpoint:

<StockListRequestData xmlns="http://myWebService.com/endpoint">
<UserID>2750</UserID>
<StockDatabase>stockLeekRoadVenue</StockDatabase>
<InStockOnly>true</InStockOnly>
</StockListRequestData>

This XML matches a DataContract on my web service:

[DataContract(Namespace = "http://myWebService.com/endpoint")]
public class StockListRequestData
{
    [DataMember]
    public string UserID { get; set; }

    [DataMember]
    public string StockDatabase { get; set; }

    [DataMember]
    public bool InStockOnly { get; set; }
}

The problem is the <InStockOnly>true</InStockOnly> element, when I set this to true or false it will always be interpreted as false…

Here is the code that handles the request:

public StockListResponseData GetListOfProducts(StockListRequestData requestData)
    {
        var stockList = new StockList(requestData.InStockOnly, requestData.StockDatabase);
        StockListResponseData response;
        if (stockList.Any())
        {
            var stockArray = new Stock[stockList.Count];
            var i = 0;
            foreach (var s in stockList)
            {
                stockArray[i] = s;
                i++;
            }
            response = new StockListResponseData
                           {
                               StockList = stockArray,
                               WasSuccessful = true,
                           };
            return response;
        }
        response = new StockListResponseData
                       {
                           WasSuccessful = false
                       };
        return response;
    }

The StockList class:

[DataContract]
public class StockList : List<Stock>
{
    public StockList(bool inStockOnly, string stockDb)
    {
        if (inStockOnly)
        {
            // Get only products that are in stock
            var conn = AndyServerDatabase.ConnectToStockMovementByDb(stockDb);
            conn.Open();
            // Compile SQL query
            var q = new SqlCommand(null, conn) { CommandText = "SELECT StockID, Name, PerBox FROM Stock WHERE InStock = 1;" };

            // Execute query
            var rdr = q.ExecuteReader();

            // Check that the output isn't null
            if (rdr.HasRows)
            {
                while(rdr.Read())
                {
                    var id = Convert.ToInt32(rdr[0]);
                    var name = rdr[1].ToString();
                    var perBox = Convert.ToInt32(rdr[2]);
                    Add(new Stock(id, name, perBox));
                }
                conn.Close();
            }
            // Output is null
            conn.Close();
        }
        else
        {
            // Get all products
            // Get only products that are in stock
            var conn = AndyServerDatabase.ConnectToStockMovementByDb(stockDb);
            conn.Open();
            // Compile SQL query
            var q = new SqlCommand(null, conn) { CommandText = "SELECT StockID, Name, PerBox FROM Stock;" };
            q.Prepare();

            // Execute query
            var rdr = q.ExecuteReader();

            // Check that the output isn't null
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    var id = Convert.ToInt32(rdr[0]);
                    var name = rdr[1].ToString();
                    var perBox = Convert.ToInt32(rdr[2]);
                    Add(new Stock(id, name, perBox));
                }
                conn.Close();
            }
            // Output is null
            conn.Close();
        }
        // Add();
    }
}

Resultant XML:

<StockListResponseData xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <StockList xmlns:a="http://schemas.datacontract.org/2004/07/SMS">
        <a:Stock>
            <a:Id>1</a:Id>
            <a:Name>Smirnoff Vodka (70cl)</a:Name>
            <a:PerBox>6</a:PerBox>
            <a:Quantity>0</a:Quantity>
            <a:Remains>0</a:Remains>
        </a:Stock>
        <a:Stock>
            <a:Id>2</a:Id>
            <a:Name>Jagermeister (70cl)</a:Name>
            <a:PerBox>6</a:PerBox>
            <a:Quantity>0</a:Quantity>
            <a:Remains>0</a:Remains>
        </a:Stock>
    </StockList>
    <WasSuccessful>true</WasSuccessful>

I hope this is enough to go on, I’ve been stumped for ages and just can’t figure out why it’s behaving in such a way.. if you need additional code I haven’t included please feel free to ask.

Many thanks,

Andy

Edit:

To add some context to show what is happening:

For example, I know that:

    <a:Stock>
        <a:Id>2</a:Id>
        <a:Name>Jagermeister (70cl)</a:Name>
        <a:PerBox>6</a:PerBox>
        <a:Quantity>0</a:Quantity>
        <a:Remains>0</a:Remains>
    </a:Stock>

Has its InStock row set to 0, which means that this should not be displayed in the resultant XML if I pass in true.

I have changed the StockList constructors if(inStockOnly) to if(!inStockOnly) – I then pass in <InStockOnly>true</InStockOnly> – when it gets to the StockList constructor it is then inverted and the correct data is displayed – so it must be reading it as false by the time it gets to this if statement.

If I pass in <InStockOnly>false</InStockOnly> it still displays the “correct” results, therefore it is reading it as false until it gets to the inversion!

Likewise if I leave it as if(inStockOnly) and pass in <InStockOnly>false</InStockOnly> it displays the data for false!

I have also added requestData.InStockOnly to the StockListResponseData DataContract and there it displays it outputs the value of requestData.InStockOnly as false.

  • 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-17T02:06:04+00:00Added an answer on June 17, 2026 at 2:06 am

    Your discovery has led me to the explanation, and someone with a problem similar to yours:

    WCF DataContract DataMember order?
    http://msdn.microsoft.com/en-us/library/ms729813.aspx

    Next in order are the current type’s data members that do not have the Order property of the DataMemberAttribute attribute set, in alphabetical order.

    When the order of data members is not explicitly specified, their serialization order is alphabetical. That explains why InStockOnly worked when it was moved to the top, because it’s first alphabetically. On the other hand, why StockDatabase worked is a bit of a mystery, because that’s after UserId alphabetically (does AndyServerDatabase.ConnectToStockMovementByDb() use a default value if StockDb is null?).

    For the sake of argument, if for whatever reason you wanted to keep the order you have there, you could do this:

    [DataContract(Namespace = "http://myWebService.com/endpoint")]
    public class StockListRequestData
    {
        [DataMember(Order = 0)]
        public string UserID { get; set; }
    
        [DataMember(Order = 1)]
        public string StockDatabase { get; set; }
    
        [DataMember(Order = 2)]
        public bool InStockOnly { get; set; }
    }
    

    In fact, it’s probably a good practice to explicitly indicate the order, so adding new properties later doesn’t break anything.

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

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm interested in microtypography issues on the web. I want a tool to fix:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.