【问题标题】:WCF Service cant seem to return Object to Form ApplicationWCF 服务似乎无法将对象返回到表单应用程序
【发布时间】:2017-06-07 14:26:37
【问题描述】:

我正在尝试在 Visual Studio 中使用 Windows 窗体应用程序作为客户端创建 WCF 服务。

问题是我在调用方法时似乎无法从我的服务接收数据(我想返回一个 Customer 对象)- 数据必须存储在服务中,而不是数据库。 (这可能吗?没有数据库?)

这里是 MyService.cs:

 public class MyService : IMyService
{
    Store store = new Store();

public void RegisterCustomer(string name, string pass)
    {
        Customer c = new Customer(name, pass);
        store.Customers.Add(c);
    }

  public Customer GetCustomer(string name)
    {
        Customer c = new Customer();
        foreach(Customer i in store.Customers)
        {
            if (i.UserName.Equals(name))
            {
                c = i;
            }                    
        }
        return c;           
    }

操作和数据成员在 IMyService 接口中声明

namespace WcfService
{
[ServiceContract]
public interface IMyService
{
 [OperationContract]
    // register - klant bestaat niet --> voeg toe aan klantenlijst -1.1-
    void RegisterCustomer(string name, string pass);

 [OperationContract]
    // login - klant bestaat niet --> voeg toe aan klantenlijst
    Customer GetCustomer(string name);

 [DataContract]
public class Customer
{
    string username;
    string password;
    int saldo;

    [DataMember]
    public string UserName { get{ return username; }set{ username = value; }
    }

    [DataMember]
    public string Password{get { return password; }set { password = value; }
    }

    [DataMember]
    public int Saldo{get { return saldo; }set { saldo = value; }
    }
    public Customer() { }

    public Customer(string name, string pass)
    {
        this.username = name;
        this.password = pass;
        this.saldo = 100;
    }
}

我们在form1中的引用:form

//reference
    ServiceReference1.MyServiceClient client = new ServiceReference1.MyServiceClient();

调用服务的表单应用

private void button2_Click(object sender, EventArgs e)
    {
        string name = textBox4.Text;
        string pass;

        if (client.CustomerExists(name))
        {
            label9.Text = "De opgegeven naam bestaat al";
        }
        else {
            pass = client.CreatePass(name); // does work
            client.RegisterCustomer(name,pass);    // does not work           

            Customer c = client.GetCustomer(name); // does not work
            label9.Text = "welkom: " + c.UserName + "\njouw pass: " +c.Password;
        } }

这行代码应该返回 Customer 的对象...但它没有。

Customer c = client.GetCustomer(name); // does not work

【问题讨论】:

  • 使用变量来存储服务上的数据应该与会话一起使用,我不建议将其用于长寿命数据,因为会话/服务引用并非旨在长时间保持活动状态。您是否尝试过修改您的 GetCustomer 以返回“假样本客户”?错误是什么(异常,什么都不返回,而不是预期)?您是否尝试调试您的服务以查看您的商店收藏的内容? WCF 不是小菜一碟,根据配置可能会有很多不同的行为!
  • 嗨,感谢您的评论。我已经尝试过在“client.GetCustomer(name)”方法中创建客户的想法,这一切都有效,并且确实返回了客户对象,并且可以在使用 c.UserName 和 c.Password 时设置为 label9 上的文本.唯一的问题是我想在“存储变量”中保存一个客户列表,每次我使用我的一种方法时都可以访问该列表。但是现在,一个接一个地调用方法,调用“存储变量”会给我留下一个“空”存储对象
  • 检查您的服务的 InstanceContextMode ServiceBehavior 属性是否设置为 PerSession。备注:当客户端关闭连接时,您的商店将被重置!

标签: c# winforms visual-studio wcf object


【解决方案1】:

解决方案:

InstanceContextMode 确实是问题所在:

[ServiceBehavior(InstanceContextMode= InstanceContextMode.Single)]

这是错误的:

 public class MyService : IMyService{
    Store store = new Store();

    //your methods here

}

这是对的:

[ServiceBehavior(InstanceContextMode= InstanceContextMode.Single)]
    Store store = new Store();

    //your methods here

}

+1 给马可

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 2010-10-25
    相关资源
    最近更新 更多