Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据契约不匹配';不要在WCF中导致错误_C#_Wcf - Fatal编程技术网

C# 数据契约不匹配';不要在WCF中导致错误

C# 数据契约不匹配';不要在WCF中导致错误,c#,wcf,C#,Wcf,我有一个实现数据契约的WCF服务。然后,我有一个客户机,它使用自己的数据契约实现来使用该服务 如果数据契约不完全匹配,它不会生成任何类型的错误,也不会返回任何数据 public class RecipeClient : ClientBase<IRecipeService>, IRecipeService { public RecipeEntity[] GetAllRecipes() { var recipe = Channel.GetAllRecipe

我有一个实现数据契约的WCF服务。然后,我有一个客户机,它使用自己的数据契约实现来使用该服务

如果数据契约不完全匹配,它不会生成任何类型的错误,也不会返回任何数据

public class RecipeClient : ClientBase<IRecipeService>, IRecipeService
{
    public RecipeEntity[] GetAllRecipes()
    {
        var recipe = Channel.GetAllRecipes();
        return recipe;
    }
}
公共类RecipeClient:ClientBase,IRecipeService
{
公共互惠[]GetAllRecipes()
{
var recipe=Channel.GetAllRecipes();
返回配方;
}
}
在上面的示例中,调用完成后,recipe包含RecipeEntity的空数组


我希望它不会返回任何数据,但为什么它不会生成错误?

这是为了向后兼容。如果在现有服务的datacontract中添加一些非必需的属性,则所有现有客户端都将正常工作。

就像前面提到的那样,这是为了向后兼容,但您可以根据需要标记一些属性。如果消息中没有此类属性,则会引发异常:

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