我现在有个WCF服务,里面有一些字段。
using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
namespace LeYi_ERP.Entity
{
/// 姓名
///<summary>
/// 姓名
///</summary>
[DataMember]
[Required]
public string UserName { get; set; }
这个字段我加了 Required 标识,但是在客户端却不行,不生效。客户端代码如下:
public static bool IsRequired<TEntity>(string propertyName)
{
return IsSet<RequiredAttribute>(typeof(TEntity), propertyName);
}
public static bool IsSet<TAttribute>(System.Type type, string propertyName) where TAttribute : ValidationAttribute
{
string[] path = propertyName.Split('.');
if (path.Length > 1)
{
PropertyInfo nestedPropertyInfo = type.GetProperty(path[0]);
if (nestedPropertyInfo == null)
return false;
return IsSet<TAttribute>(nestedPropertyInfo.PropertyType, string.Join(".", path.Skip(1)));
}
PropertyInfo propertyInfo = type.GetProperty(propertyName);
if (propertyInfo == null)
return false;
return propertyInfo.GetCustomAttributes(false).OfType<TAttribute>().Any();
}
这个方法永远返回false,但是在EF中却可以返回true,WCF中却不行,急死我了,前台要判断验证,现在验证无法通过。请问这是为什么啊!
------解决思路----------------------
哦,你原来是手动添加“服务引用”的,那么服务端实体类变更后你要更新服务引用啊,否则客户端原先的引用并不知道啊。
而且,我上面说的那个方法就是为了避开由于服务端实体类变化而要去更新服务引用。
你更新一下引用吧。
方法参照MSDN
https://msdn.microsoft.com/zh-cn/library/cc668183.aspx
------解决思路----------------------
就别那么折腾了,我做过直接将数据契约,服务契约单独作为dll,同时给服务端,客户端用
------解决思路----------------------

把WCF当成Socket来整就没这么多烦人的事了
把你要传输的对象序列化成byte[] 通过BinnaryRead BinnaryWrite 进行多参数的组合
------解决思路----------------------
用WCF或者学WCF,都很明显可以看到,使用VS生成的WCF client代理类中,会把所有用到的实体类也生成一套一样定义的代理类。你对服务器端的实体类做的很多定义都是无法在自动生成的代理类上自动生成出来的。
解决办法通常有2:
1:把实体类定义在一个独立的dll中,在客户端也引用这个dll,然后刷新wcf服务代理
2:自己写wcf代理类,同样公用或者定义两遍你的实体类。
1的缺点是vs有时候莫名奇妙就是无视你公用的实体类,特别是vs2012 rtm(更新到update4会改善很多)
2的缺点是有点麻烦,而且自己写的往往考虑不够全面。