Dean
Dean

Reputation: 1550

Data Contracts Not Matching Doesn't Cause An Error in WCF

I have a WCF service that implements a data contract. I then have a client that consumes that service with it's own implementation of the data contract.

If the data contracts don't match exactly, it doesn't generate any sort of error, nor does it return any data.

public class RecipeClient : ClientBase<IRecipeService>, IRecipeService
{
    public RecipeEntity[] GetAllRecipes()
    {
        var recipe = Channel.GetAllRecipes();
        return recipe;
    }
}

In the above example, after the call is made, recipe contains an empty array of RecipeEntity.

I would expect it to not return any data, but why doesn't it generate an error?

Upvotes: 0

Views: 94

Answers (2)

Igor Tkachenko
Igor Tkachenko

Reputation: 1120

As if was mentioned it's for backward compatibility, but you can mark some properties as required. And if there is no such property in a message, an exception will be thrown:

[DataContract]
public class Recipe
{
    [DataMember]
    public string Name { get; set; }
    [DataMember(IsRequired = true)]
    public string Rank { get; set; }
}

Upvotes: 1

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

It is for backward compatibility. If you add in datacontract of existing service some not required properties, all existing clients will work without errors.

Upvotes: 1

Related Questions