简体   繁体   中英

How do I access a .NET GUI application through a Web Service?

I've been searching for a while for an answer to my problem but so far have found no reliable links. What I'm trying to do is this: I've built a Windows .NET GUI application. I'd like to be able to access the functionality of the Form Controls through a Web Service. Are there any good links on how to do this?

Thanks for your help.

You can easily host a Windows Communication Foundation Web service in your WinForms application. See the MSDN article on How to Host a WCF Service in a Managed Application .

Note that if you want your service operations to interact with the UI controls (which I assume is the purpose of having the service in the app -- otherwise it's better to create a normal, non-visual service and host it in IIS or a Windows service) then you will need to use Control.Invoke or Control.BeginInvoke because service operations run on a background thread.

This is a pretty good example of hosting a service from a Winforms application.

http://www.codeproject.com/KB/WCF/WCFexample.aspx

You could also do something simple like this in your main method:

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        ServiceHost host = new ServiceHost(typeof(TestService));
        NetNamedPipeBinding namedPipe = new NetNamedPipeBinding();
        host.AddServiceEndpoint(typeof(ITest), namedPipe, "net.pipe://localhost/test");

        host.Open();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new AutoDeployApp());
    }

Then, use this type of thing inside the service to get at the running form:

        MyForm form = Application.OpenForms["MyForm"] as MyForm ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM