C# WCF服务似乎无法将对象返回到表单应用程序

C# WCF服务似乎无法将对象返回到表单应用程序,c#,winforms,visual-studio,wcf,object,C#,Winforms,Visual Studio,Wcf,Object,我正在尝试在VisualStudio中使用windows窗体应用程序作为客户端创建WCF服务 问题是,调用该方法时,我似乎无法从我的服务接收数据(我希望返回一个Customer对象)- 数据必须存储在服务中,而不是数据库中。(这可能吗?没有数据库?) 这里是MyService.cs: public class MyService : IMyService { Store store = new Store(); public void RegisterCustomer(string

我正在尝试在VisualStudio中使用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;
    }
}
我们在表单1中的引用:表单

//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
解决方案:

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

}
这是对的:

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

    //your methods here

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

    //your methods here

}

+1到Marco

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