我的winform应用程序中有一个自托管的Web API。我想从我的自托管Web API调用mainFrom的方法。收到请求后,我想调用“ goNightMode”方法。
问题在于,通过在api控制器类中创建mainForm对象,我可以拥有一组新的UI和非UI组件。我正在寻找一种解决方案,无需使用mainForm或任何其他可能的方式创建新对象即可访问组件! 这是我到目前为止所拥有的:
我的mainForm:
namespace MyProject.Forms
{
public partial class MainForm : Form
{
private void MainForm_Load(object sender, EventArgs e)
{
//Initialzation of Web API
}
public bool goNightMode(string pName)
{
textBox1.text = pName; //nothig gets changed!
//Time consuming job
//its result is either true or false
return result;
}
}
}
这是我用于网络API的api控制器:
namespace MyProject
{
public class ValuesController: ApiController
{
[HttpGet]
public string Execute(string pName)
{
MainForm form = new MainForm();
if(form.goNightMode(pName))
return "Night Mode is activated successfully!";
else
return "A problem occured!";
}
}
顺便说一句,我不想使用事件处理程序,因为我需要阻止api控制器等待耗时工作的结果并相应地发送HTTP响应。