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 ;
精彩评论