自主服务堆栈,需要访问表单元素

时间:2013-03-08 00:32:04

标签: servicestack

我需要在winform应用程序中自我托管服务堆栈服务器,并且想知道访问表单元素和全局变量的正确方法。 在Run方法中,我如何引用Main来访问和修改主窗体中的AccessCount,UpdateLog,LogBox.Text?

这是我的基本工作样本:

namespace Test
{    
    public partial class Main : Form
    {
        private static readonly string ListeningOn = ConfigUtils.GetAppSetting("ListeningOn"); 

                    public int AccessCount;


        public Main()
        {
            InitializeComponent();

            var appHost = new AppHost();
            appHost.Init();
            appHost.Start(ListeningOn);

            UpdateLog("Started listening on: ");

            UpdateLog(string.Format("AppHost Created at {0}, listening on {1}", DateTime.Now, ListeningOn)); 


        }

        public void UpdateLog(string data)
        {
            LogBox.Text +=  DateTime.Now.ToString("hh:mm:ss:fff") + " - " + data + "\n";
        }

        public class AppHost : AppHostHttpListenerBase
        {

            public AppHost() : base("StarterTemplate HttpListener", typeof(HelloService).Assembly) { }


            public override void Configure(Funq.Container container)
            {

            }
        }
    }

    [Description("ServiceStack's Hello World web service.")]
    [Route("/hello")]
    [Route("/hello/{Name*}")]
    public class Hello : IReturn<HelloResponse>
    {
        public string Name { get; set; }
    }

    public class HelloResponse : IHasResponseStatus
    {
        public string Result { get; set; }
        public ResponseStatus ResponseStatus { get; set; }
    }

    public class HelloService : ServiceBase<Hello>
    {
        protected override object Run(Hello request)
        {
                            // HOWTO reference Main to access and modify AccessCount, UpdateLog, LogBox.Text in the main form?


            return new HelloResponse { Result = "Hello, " + request.Name };
        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:2)

我没有使用WinForms的实际经验,所以我通过打开Windows并调用该方法的循环解决方案可能不是一个现实的解决方案。至少,我认为这很有趣。我还做了一些其他的改动:

  • 使HelloService继承自Service(new Api)
  • 您的Run方法受到保护,我将其公开并更改为Any()

代码:

public partial class Main : Form
{
    private static readonly string ListeningOn = "http://*:1337/";
    public int AccessCount;

    public Main()
    {
        InitializeComponent();

        var appHost = new AppHost();
        appHost.Init();
        appHost.Start(ListeningOn);
        UpdateLog("Started listening on: ");

        UpdateLog(string.Format("AppHost Created at {0}, listening on {1}", DateTime.Now, ListeningOn)); 
    }

    public void UpdateLog(string data)
    {
        LogBox.Text +=  DateTime.Now.ToString("hh:mm:ss:fff") + " - " + data + "\n";
    }

}

public class AppHost : AppHostHttpListenerBase
{
    public AppHost() : base("StarterTemplate HttpListener", typeof(AppHost).Assembly) { }

    public override void Configure(Funq.Container container)
    {

    }
}

[Description("ServiceStack's Hello World web service.")]
[Route("/hello")]
[Route("/hello/{Name*}")]
public class Hello : IReturn<HelloResponse>
{
    public string Name { get; set; }
}

public class HelloResponse : IHasResponseStatus
{
    public string Result { get; set; }
    public ResponseStatus ResponseStatus { get; set; }
}

public class HelloService : Service
{
    public object Any(Hello request)
    {
        // HOWTO reference Main to access and modify AccessCount, UpdateLog, LogBox.Text in the main form?
        foreach(var fm in Application.OpenForms)
        {
            if (((Form)fm).Name == "Main") //Find the form by name
            {
                ((Main) fm).Invoke(new Action<Form, string>(UpdateLog), fm, request.Name); //Can only make changes to WinFrom controls from master thread so 'delegating' method back to form
            }
        }
        return new HelloResponse { Result = "Hello, " + request.Name };
    }

    public void UpdateLog(Form fm, string name)
    {
        ((Main)fm).AccessCount++;
        ((Main)fm).UpdateLog(name);
    }
}