WCF DataMember在客户端不可用或不可见我做错了什么? [英] WCF DataMember not available or visible in Client what am I doing wrong?

查看:73
本文介绍了WCF DataMember在客户端不可用或不可见我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从winforms WCF客户端看到属于DataService类的DataMember ..数据,我可以执行操作并且工作正常 - 我只是看不到这个DataMember对象{datas};我做错了什么?



下面是示例代码:



 [DataContract] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode。 Single )]
public DataService:IDataService
{ // 在支持领域也尝试了这个[DataMember]。
BindingList< Data> m_datas;
[DataMember]
public BindingList< Data>数据
{
获取
{
如果 .m_datas == null
{
this .m_datas = new BindingList< Data>();
}
返回 .m_datas;
}
set
{
m_datas = value ;
}
}
}





我的界面是

< pre lang =c#> [ServiceContract]
public interface IDataService
{
[OperationContract]
BindingList< Data>的GetData();
}

[DataContract]
public class 数据
{
[DataMember]
public string name;
}





我想这样做的原因;是我有一个不断运行和监控的服务 - 它然后更新这个内部对象列表,然后我希望我的客户端显示实时更新 - 我可以使用GetData显示数据,但这需要我处理这一切列表在服务中更改的时间。有没有更好的方法呢?

解决方案

你想在哪里访问数据您的服务属性?它是在客户端吗?如果是这样,看起来对事物的运作方式有一点误解。



当你为你的服务创建一个客户端时(使用ChannelFactory 添加服务引用),它创建一个实现 ServiceContract 的对象(在你的情况下: IDataService )并包含 OperationContract 的实现 - 没有转移任何属性。



如果你想要获得 datas 的值,你应该实现 GetData 来返回它。您应该将数据( Data )声明为 DataContract (不是服务...)


I cannot see the DataMember .. datas belonging to the DataService class from my winforms WCF Client, I can do the operations and that works fine - I just cannot see this DataMember object {datas} ; what am I doing wrong?

Below is the example code:

    [DataContract]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class DataService : IDataService  
    {   // have tried this too [DataMember] on backing field. 	
        BindingList<Data> m_datas;        
        [DataMember]
	public BindingList<Data> datas 
	{
		get 
		{
		  if (this.m_datas == null) 
		  {
		     this.m_datas = new BindingList<Data>();
		  }
		    return this.m_datas;
		}
		set 
		{
		   m_datas = value;
		}
	}
}



My interface is

[ServiceContract]
public interface IDataService
{
	[OperationContract]
	BindingList<Data> GetData();
}

[DataContract]
public class Data
{
   [DataMember]
   public string name;
}



The reason I want to do this; is that I have a service that is constantly running and monitoring - it then updates this internal list of objects, which I then want my client to show real time updates - I can show the data by using GetData but this requires me to handle this all the time whenever the list changes in the service. Isn't there a better way to do this ?

解决方案

Where do you want to access the datas property of your service? Does it in the client side? If so, It looks like there is a little misunderstanding of how the things works.


When you create a client to your service (using ChannelFactory or Add Service Reference), it creates an object that implements your ServiceContract (in your case: IDataService) and contains implementation for your OperationContracts - No properties are transfered.


If you want to get the value of datas, you should implement GetData to return it. You should declare the data (Data) as a DataContract (not the service...)


这篇关于WCF DataMember在客户端不可用或不可见我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆