问题
How do I create a program in c# that runs on a desktop but on certain occasions, is able to pass variables and objects to a server. The server will then process the variables before returning the outcome to the program on the desktop for further processing.
So for e.g., I want to pass the doubles 3.5 and 4.5 to the server. The server would then add 3.5 and 4.5 to return 8 to the program on the desktop. The 8 will then be further utilised by the program on the desktop.
Do I have to write two separate programs? One on the server and one on the desktop. And if so, how do I link them. i.e. how do I pass data from the program on the desktop to the program on the server and vice versa?
Thanks a lot for any help.
回答1:
You do not need two different programs. While it is easier that way you can stick with one that runs in different modes.
WCF
, WebApi
, ServiceStack
, MessageBus
can be used for that.
But you are making a jump that is hard to achieve at once.
You have a synchronous local application (everything runs in one app) and you want to get to an asynchronous remote application (everything is parallelized and executed on a different machine).
WCF
etc. gives you an easy start, however I would recommend smaller steps as it does not mitigate all issues.
First keep your synchronous application and make it asynchronous. So it issues some message (say the two doubles) to itself in a multi-threaded environment and waits for it without blocking.
This introduces its own set of issues (threading, waiting, locks).
After you've done that you should externalize your functionality to a separate application or the same application running in a "server" mode, resulting in an asynchronous local application. In this step you cross application boundaries while already having achieved threading and still being local.
After you've done that, start thinking about placing the server application on a remote machine and deal with that (communication, resource availability, protocols, etc.)
Each step is complex. By doing everything at once you will need to handle multithreading, blocking application, the complexity of WCF
, connection handling (timeouts, proxies, not available resources, connection interrupts, etc.) and outsourced processing at the same time at once.
回答2:
Yes, you do need two programs. As was mentioned, WCF and ASP.NET WEB API are the two most common Microsoft technologies to use, though you can use whatever technology you want. Basically you need to build out your server side application in WCF or ASP.NET, then you will add a service reference to your client application. When creating that service reference you will specify the URL of your service and Visual Studio will build out the classes needed to communicate between applications. Once that is done, you can make calls to your server application the same way you would use any normal method.
来源:https://stackoverflow.com/questions/21578993/passing-data-to-server-for-computation