Reputation: 5905
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
Upvotes: 1
Views: 9097
Reputation: 754220
Basically, to create a WCF service, you need three things:
a service contract (typically expressed as a .NET interface) to define the methods the service provides. This also includes what datatypes the methods expect (and possibly return)
[ServiceContract(Namespace="http://services.yourcompany.com/Service/2012/08")]
interface IMyService
{
[OperationContract]
SomeReturnType ThisIsYourMethod(string input, int value, .....);
}
[DataContract(Namespace="http://data.yourcompany.com/Service/2012/08")]
public class SomeReturnType
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
}
a service implementation that creates the actual service code to be called. This is just a plain .NET class the implements the service contract
public class MyServiceImplementation : IMyService
{
SomeReturnType ThisIsYourMethod(string input, int value, .....)
{
/// .... do some processing, fetch data etc.
return ......
}
}
a service host to actually host the WCF runtime and spin up the whole WCF processing; this is a ServiceHost
instance (or derived class) that will be able to host your service. This class needs to be instantiated and opened somewhere in the startup process of your Winforms application. Once the service host is open, your services are available to be called from the outside world. You'll need to make sure to close the service host when your Winforms application is closing down.
and you might need - in addition - some configuration settings in your app.config
file to define what endpoints (address, binding, contract) your WCF service offers up to the world.
So this is really quite simple - just create those items in your Winform project, and you're done.
Upvotes: 7
Reputation: 79
For services hosted as windows services and web-services, your clients also need a proxy class to gain access to exposed contract members.
Upvotes: 1
Reputation: 5483
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.
Upvotes: 4