如何从客户端在WCF服务中设置属性 [英] How to set a property in a WCF service from client side
问题描述
大家好,
首先,我是WCF的新手.我知道这是我的问题的基本问题!
我想打电话给我的WCF服务,并且可以正常运行.
我想要的是对服务的每次调用都包含一个已填充的LoginUser对象.
Hello everyone,
first of all I am new to WCF. That is the basic problem to my problem, I know!
I want to make a call to my WCF service and in that works OK.
What I want is that each call to the Service contains a filled LoginUser object.
[Serializable]
[DataContract]
public class LoginUser
{
[DataMember]
public string UserName { get; set; }
[DataMember]
public string Password { get; set; }
}
在我的界面中,我说:
In my Interface I state:
BLL.LoginUser LoginUser { [OperationContract] get; [OperationContract]set; }
在我的服务班里,我有:
In my service class I have:
public BLL.LoginUser LoginUser { [OperationBehavior]get; [OperationBehavior]set; }
在客户端中,我有2种方法(get_LoginUser()和set_LoginUser()),但我想查看一个属性.
我使用这些方法进行了测试,可以使用该方法设置UserName和Password的值,但是当调用下一个方法时,我的Loginuser对象再次为NULL.
这就是为什么我要设置一个属性,并且在下一次调用中LoginUser对象仍然具有该数据的原因.
我不想在WCF调用中将LoginUser对象添加为参数,或者这是唯一的选择吗?
我将对服务的呼叫实例化为:
In the client I get 2 methods (get_LoginUser() ans set_LoginUser()) but I want to see a property.
I tested with these methods and I can set the values for UserName and Password with the method, but when calling the next method my Loginuser object is NULL again.
That is why I want to have a property to set and that in the next call the LoginUser object still has the data.
I do not want to add the object of LoginUser as a parameter in the WCF call or is that the only option?
I instantiate my call to the service as:
srPersonal.IStatistics sc = new srPersonal.StatisticsClient();
然后我想做一个
I then want to do a
srPersonal.BLL.LoginUser lu = new srPersonal.BLL.LoginUser();
lu.UserName = "hi";
lu.Password = "*****";
sc.LoginUser = lu;
var result = sc.GetMyData();
我必须更改什么?
What do I have to change?
推荐答案
WCF不允许接口上的属性. "get"和"set"将作为方法工作,而不是作为属性工作.
对于您而言,这将起作用:
WCF don''t allow properties on interfaces. The ''get'' and ''set'' will work as a methods, but not as a property.
In your case, this will work:
ServiceReference1.IService1 proxy = new ServiceReference1.Service1Client();
ServiceReference1.LoginUser lu =
new ServiceReference1.LoginUser() { UserName = "hi", Password = "****" };
proxy.set_UserLogin(lu);
var result = proxy.get_UserLogin();
您可以进行常规的基本身份验证.这是一些工作,但是它是处理用户名和密码的更好方法.
You could do a regular basic authentication. It is some work, but it is a better way to handle user name and passwords.
这篇关于如何从客户端在WCF服务中设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!