简体   繁体   中英

How to best expose methods in WinForm?

I have a C# form application that connects to a electronic device using the serial port. The class "'SerialCommunicationManager'" hooks up to the serial port on application startup and handles the dirty business of talkning to the device.

What I would like is to expose the following methods.

  • Write()
  • SerialDataReceived event
  • SerialDataTransmitted event

Primarily a local website running on the same machine is the one I want to expose the methods for, but in the future I imagine the need for external applications as well.

What is the easiest way to expose the functionality?

  • TCPIP client server?
  • Web service? (Can I create a web service inside a WinForm?)
  • other?

Big thanks

//David

在此输入图像描述

I would recommend self-hosting a WCF Service . This provides you a huge amount of flexibility in terms of how you serve and expose this information, including being able to change the method by which its served via configuration.

It seems to me, that if you would like to do it properly, you should break apart your forms app, and create:

  • a service that handles serial comm and has an API exposed through remoting
  • a Forms app that uses the API and makes a way with the service

Then, depending on the locality of your web site, if it will remain local (or near local - LAN):

  • web site should use remoting to call the service

else, if you plan to have multiple web sites:

  • web service hosted inside the IIS that will wrap remoting API
  • web site that will use web service

However, if it is too much work to do - just use remoting and expose needed methods to the web site.

In a recent project we did the following:

  • Write a Console application (or Windows Service, depending on your needs) that communicates with the electronic device.
  • Make the Console application host a .NET 4 WCF service.
  • Write a .NET 2 Windows Forms application to communicate through Web Services with the console application.

In this context, I could imagine the website you are mentioning to also use Web Services (WSDL) to communicate with the Console application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM