WCF MVC字段保持为空..为什么

本文关键字:为什么 MVC 字段 WCF | 更新日期: 2023-09-27 18:03:59

测试

public string username { get; set; }
public void Test(string test)
{
    this.username = test;
}
public string Get()
{
     return this.username   
}

ITest

[OperationContract]
public string Get();
[OperationContract]
public void Test(string test);

测试项目

var webapi3 = new v3.TestClient("BasicHttpBinding_IProductData1");
webapi3.Test("TestString");
var u = webapi3.Get();

问题

为什么u仍然是空的,不管我怎么做?

WCF MVC字段保持为空..为什么

public void Test(string test) { this.username = test; // test not username? }

第二个调用Get()可能会在另一个线程上被调用。如果希望服务器上的状态,则需要将username设置为静态。

您正在通过HTTP(一种无状态协议(进行通信。

除了使字段保持静态之外,还有其他选择,但这至少可以使测试通过。

我假设

public void Test(string test)
{
   this.username = username;
}

应为:

public void Test(string test)
{
   this.username = test;
}