Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need a big YES or NO from experts.

1. I want to write a code library. THe library contain set of functions interfacing with hardware. Its a growing library.
2. The third party applications talk to library to access hardware resources.

I am planning to write a WCF services library hosted over IIS. The third party appliations talk to libarary over http.


is this a good idea to go ahead?

thanks
Jis
Posted
Comments
Tomas Takac 17-Jan-15 12:13pm    
Is this all on the same PC? The client, the library the hardware you connect to?
Sergey Alexandrovich Kryukov 17-Jan-15 14:05pm    
The right answer would be: it doesn't matter.
I explain it in Solution 2, please see.
—SA
Jishnu Gee 18-Jan-15 4:46am    
Yes, the hardware is connected to the same PC. The Client can run from same or another PC.
[no name] 17-Jan-15 12:17pm    
In case Linux Servers are out of question then I see no Problem.
Sergey Alexandrovich Kryukov 17-Jan-15 14:10pm    
No, it is not out of question even for Linux. Or even Mac. There is WCF for Mono:
http://www.mono-project.com/docs/web/wcf.
I have good deal to experience to say that I consider the great benefits of this and similar approached well-proven.
Please see my Solution 2.
—SA

Many would say that WCF is unrelated to the hardware you are working with. That would be true.

At the same time, I'll say: in certain way, this is an excellent idea, if you take into consideration the strategy of development in larger scale, if you have big plans. I can tell that I used to put forward and actively develop a similar idea.

More exactly, you should develop the hardware control independently from WCF. It's very important that this hardware is isolated from any other aspects of programming as much as possible. Basically, there are two fields of programming which require thorough isolation: hardware control and UI. Isolate them.

Now, how to make sure that your hardware control units are really isolated from everything else, then there is no any coupling with the system, even occasionally introduced? One of possible solutions is: represent every integrated hardware unit with the control software packaged as a separate WCF service.

Of course, this architectural decision is by far not the simplest. (The simplest would be having some application controlling hardware in ad hoc manner, for the same of convenience. But you can do it as a first step of your development of each hardware control unit, and, when it is well debugged, migrate to a WCF service.) But let's consider the benefits.

First, get back to the important requirement of isolation. You can always put the WCF service on a brand new system on the LAN and make sure that there are no dependencies or coupling with whatever you have on the computer running your main application. Now, let's see what happens when you finally achieve the independent functionality of the hardware unit through a WCF service. Now you can develop different architectures with untied hands. The control of your hardware units became abstracted from the location of the host connected to this piece if hardware. Even though you do all the work on the same computer now, you will be sure, that at any time you can move this service to any other computer, without even any changes in software, only in some configuration files. Moreover, the clients of your service can be heterogeneous, use your hardware unit on many different platforms.

Even now, you could have a lean WCF-based client on a tiny tablet computer, take it anywhere with your, say, crawl in the dark place under some semi-disassembled machine, and perform your troubleshooting from that place. At the same time, you can come to deployment of a number of machines to some big customer, who will enjoy using just one or two front-end computers for controlling of the whole factory of your machines, if you manage to cell so many of them. :-)

Overall, this is how it looks. You pay extra effort on implementation of this architectural decision. But when it's done, it will pay off in large, but only if you developing big system where you can benefit from freedom and flexibility. And the flexibility you can get can be amazing. I can only add that in my case the benefits of this decision were well-proven.

[EDIT]

And yes, PIEBALDconsult made a good point. You only can have these benefits if you self-host the service. Never use IIS. The adequate form would be the Windows Service. It will give you some debugging problems. So, also look at my method of solving this problem: Configure Windows Service with UI[^].

—SA
 
Share this answer
 
v10
Comments
Tomas Takac 17-Jan-15 14:11pm    
+5 very good explanation
Sergey Alexandrovich Kryukov 17-Jan-15 14:17pm    
Thank you, Tomas.
—SA
[no name] 17-Jan-15 14:35pm    
+5
Sergey Alexandrovich Kryukov 17-Jan-15 14:38pm    
Thank you, Liju.
—SA
Jishnu Gee 18-Jan-15 11:54am    
SA - Thank you for detailed explanation and I accept your solution. However, I want you to throw some light on isolating hardware driver and WCF program.
Do you intend to expose these services to an internal application or even to the outside world?
Then yes - go for WCF.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Jan-15 13:59pm    
There are many reasons to go this way beyond those consideration.
I'm telling you as the one who uses the similar approach.
Please see Solution 2.
—SA
Jishnu - If your IIS server and the machine your are connecting your hardware device is same machine, then WCF is not a bad option.

However, you may have to think about Concurrency - by using HTTP, multiple users may send WCF requests at the same time, i don't think that your hardware device can service multiple requests.

I would recommend 2 approaches
1. Use WCF in conjunction with a Queue (MSMQ), where all user requests will be processed one by one
2. or Mask your hardware device from WCF service, i mean WCF service will not talk to your hardware device directly but with a database or XML file or a temp storage. You can think about creating a Windows service which monitor your hardware and interact with your temporary storage.
 
Share this answer
 
Jishnu Gee asked:

SA — Thank you for detailed explanation and I accept your solution. However, I want you to throw some light on isolating hardware driver and WCF program.

Well, those two things should be isolated as well, but this is simpler. So far, we mostly discussed architecture in the aspect of module and deployment isolation (tiers, actually). What you are asking now is a very different and, in my understandings, simpler aspect: layers architecture.

Basically, all units you create (assemblies, in this case), should be put in a stratified structure represented by some artifacts. In this case, projects and, say, directory structure where you put your project. Say, when you create a module which uses WCF to connect hardware units of certain type, you create some particular solution on some semantic layer. Why, after all, it is different from just the WCF? Because you bind semantic features of your hardware interface with WCF by creating some artifact, such as service contract. This service contract is the semantic artifact which carry knowledge of how exactly, say, the instructions directed to the hardware are expressed in the form of data/service contract, how it is structured, and how the hardware response is structured. Roughly speaking, you will have WCF types for control and acquisition.

For general approach and the motivation of appropriate architectural solutions, please see my past answer: How Do I Divide My Code Appropriately?[^].

Please locate my considerations of vertical and horizontal arrangement of units. I probably did not mention one aspect: the build. If you simply put all your units in one solution, your structure is always built by dependencies, and more universal layers are built first, then less universal and more semantic. The dependency of units in the same layer should be undefined (otherwise they should be split into different layer).

Let's back to the place of your hardware and WFC. In the semantic layer (or one of those), you should define something which is specific to the hardware. What? 1) interfaces for one or another type of hardware, 2) semantic WCF types describing acquisition/control for this type of hardware. These two kinds of units should be on the closed semantic layers, but WCF types are a bit upper: WCF types can depend on the interfaces, but the hardware interfaces cannot depend on WCF types. (It gives you plenty of benefits. Don't couple to WCF too much. What if you still need hardware which is never connected to WCF? What if you later migrate from WCF to some newer technology of future? It's more likely that your hardware interfaces would be the same or change less.)

Now, horizontally, you have different types of hardware and different types of WCF. There horizontal units does not have to be even different assemblies. It just depends on the complexity of your interfaces. Let's see it on a simple example. Say, you have 1) motion system, 2) cameras and 3) digital I/O for control and acquisition of simple sensors. Minimally, you can have two projects and two library assemblies covering these topics: 1) set of hardware interfaces for motion systems, cameras and digital I/O, 2) set of WCF types (contracts, etc) covering control and acquisition for these types of hardware. So, each of these 3 types should be represented by two projects and two assemblies, but hardware of different types can be united with different hardware types in one unit. No way you should put any implementations of hardware control in these layers! Instead, you should create sets of interfaces each covering wide class of motion, visual and I/O hardware from different manufacturers. This is the most important idea here.

Now, where should you put implementations? On the very top, in plug-ins layer! You can have a separate plug-in on each type of hardware of each manufacturer or, in some cases, separate for different product like of different manufacturer. It's important to avoid one over-simplification seduction here. One may decide: "Oh, we are using only one I/O product of only one manufacturer. This product is very good, so I decided to use only this one, so please don't waste time, put it in your software directly, assuming there will be only one". Fight such arguments as strongly as you can. If it tends to happen, your role should be to save your team from vendor lock-in, and not only that. Assuming that the hardware should always be the same is one of the worst typical mistakes. All hardware units should be represented as plug-ins. But now, a plug-in does not mean a separate project and separate plug-in assembly. Usually plug-in technologies allow putting more than one plug-in (usually for the same set of implemented interfaces), so you can put them together, too. It's enough to have code for different units in different, say, source file directories.

See also my past answer related to plug-ins and other related answers on plug-in architectures:
Projects and DLL's: How to keep them managable?[^].

See also at this specific open-source plug-in technology project:
http://en.wikipedia.org/wiki/Managed_Extensibility_Framework[^],
http://mef.codeplex.com[^].

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900