I have a C# solution which consists of 2 projects: a console project and a asp.net web-api project (REST service); if I change a static property in a REST class from a REST client (ex. Fiddler), and right after I retrieve the value of this static property by using its getter from a console's class I obtain a different value (below the code). Any suggestions welcome !
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestLayer.Controllers;
using System.Web.Http;
namespace ValController
{
class Program
{
static void Main(string[] args)
{
....
var t1 = Task.Factory.StartNew(() =>
{
string stat = "disabled";
while (true)
{
System.Threading.Thread.Sleep(100);
var stat = ValuesController.getState();
}
});
t1.Wait();
Console.WriteLine("t1 has completed.");
}
}
}
The REST service:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RestLayer.Models;
namespace RestLayer.Controllers
{
public class ValuesController : ApiController
{
private static String state = "enabled";
// POST api/values
public IEnumerable<string> Post(Command command)
{
if (command.Instruction == "enableBV")
{
state = "enabled";
}
else if (command.Instruction == "disableBV")
{
state = "disabled";
}
return new string[] { "state", state };
}
public static String getState()
{
return state;
}
public static void setState(String newState)
{
state = newState;
}
}
}
The REST client I am using is Fiddler. In summary, the value of the var stat I retrieve in Program.cs is different from the one (state) I set by consuming the POST.
答案 0 :(得分:2)
Static properties are only common to the current process. You have two processes here - a console app and a rest client.
To share the data between the two you could either persist the data to a database, but maybe better would be to have the console app make a call to the rest client and get the current state from it.
Your current "call to the rest client" just accesses the static property from the ValuesController class using import {LanguageForm} from '../components/LanguageForm';
. To get the value from the rest client process you need to make a call to the rest api process using WebClient (or similar).