Search code examples
c#wcfservicelan

How would I add a WCF service to an existing winforms application?


I have a winform application that handles some data entry and billing. I'd like to add a WCF service that is accessible over the local LAN only. I'd like my billing program to query my database and fetch some data for the client. It is important that this be done in the -same- program instead of creating another.

My question is it difficult to setup a WCF service like this where I'm starting from an existing winform application instead of creating a fresh WCF service. Is it a simple matter of putting the right using directives or is something else fundamentally missing since I didn't set it up as a WCF service from the get go?

Another concern is do I need to worry about threading or is that automatically handled by the WCF service? For instance, if 10 computers all query my winforms application at the same time will WCF seamlessly handle that or I do I need to implement additional functionality to handle this?

Thanks for reading


Solution

  • Please look at this article Hosting and Consuming WCF Services

    Windows service hosting the WCF ServiceHost (example from this article)

    using System;
    using System.ServiceModel;
    using System.ServiceProcess;
    using QuickReturns.StockTrading.ExchangeService;
    
    namespace QuickReturns.StockTrading.ExchangeService.Hosts
    {
        public partial class ExchangeWindowsService : ServiceBase
        {
            ServiceHost host;
    
            public ExchangeWindowsService()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                Type serviceType = typeof(TradeService);
                host = new ServiceHost(serviceType);
                host.Open();
            }
    
            protected override void OnStop()
            {
                if(host != null)
                   host.Close();
            }
        }
    }
    

    Another concern is do I need to worry about threading or is that automatically handled by the WCF service? For instance, if 10 computers all query my winforms application at the same time will WCF seamlessly handle that or I do I need to implement additional functionality to handle this?

    I think wcf will easily handle this load. But it depends on operations that you want to do on it.