I am new to setting up WCF, I have it going in my project, but I have like 5 different 'services' in my one WCF project and I am wondering if I am doing the right thing. My services for now are 1-1 to my database tables. I end up having something like:
public class Projects : IProjects
{
public List<Project> GetAll()
{
return (from p in Connection.Data.Projects
select new Project {ID = p.id, Name = p.name}).ToList();
}
public Project GetByID(int id)
{
return (from p in Connection.Data.Projects
where p.id == id
select new Project {ID = p.id, Name = p.name}).First();
}
public Project AddProject(string name)
{
var project = new Data.Projects {name = name};
Connection.Data.AddToProjects(project);
Connection.Data.SaveChanges();
return new Project {ID = project.id, Name = project.name};
}
public void DeleteProject(int id)
{
var project = (from p in Connection.Data.Projects
where p.id == id
select new Project {ID = p.id, Name = p.name}).First();
Connection.Data.DeleteObject(project);
Connection.Data.SaveChanges();
}
}
I have a similar class for each of the tables in my project. Should I be finding a way to use 1 service connection with sub classes or keep it as 1 service class per table?
Remove Class1.cs that is auto generated from the solution in Solution Explorer. Then seelct "Add" >> "New Item" >> "WCF Service" then provide the name "MultipleService.cs". Click the Add Button. Now we have one interface IMultipleService.cs and one class file MultipleService.cs. Now when you open the IMultipleService.cs file it looks like that.
Give a name as Northwind. Add NorthwindServices WCF Service Library to Northwind solution by right click on solution -> Add -> New Project -> Choose Visual C# from Installed templates -> Select WCF -> Select WCF Service Library Delete default IService1.cs, Service1.cs files from library.
You need to follow same steps for Customer Service and Order Service. Create a folder Hosts in your service library application and place the CustomerServiceHost.svc and OrderServiceHost.svc before hosting the service in IIS. While hosting WCF service with netNamedPipeBinding in IIS some bindings errors might occur.
IMultipleConfidentialService: For those, who are inside of the firewall. We use here netTcpBinding. Now it is time to change the code above in both files. public class MultipleService : IMultiplePublicService, IMultipleConfidentialService return "I am watching Public Information over HTTP.";
"It depends!" :-) The standard answer for all IT and programming questions :-)
I don't see anything wrong with having those 5 separate services - you don't really gain anything by merging them all together into one big service, I'd say. I would prefer to keep them separate and "lean'n'mean".
If you have five separate services, you can also manage things like access permissions to them for each one separately, e.g. let certain user groups use one service, while not another.
Again: I think you're doing it just fine - I don't see any compelling reason or benefit from having one huge services vs. five smaller, nimbler ones.
Come to think of it - the only real change I might suggest is trying to design your services so that they are more closely matched to what your app wants to do (i.e. the operations you expect your app and thus your services to handle), rather than modelling them too closely to the database. Try to think "task-oriented" or in terms of operations, rather than the underlying store where they'll store their data.
Marc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With