如何让我的Web应用程序与我的桌面应用程序通信?

时间:2022-11-24 15:50:06

I have developed a Web and windows application, both in C#.NET. Now, what I would like to do is when a button is clicked on a particular page in the web application, a signal has to be sent to the windows app saying a link has been clicked. How could I do this really easily? I would like to stay away from socket programming...

我在c# . net中开发了一个Web和windows应用程序。现在,我想做的是当一个按钮被点击到web应用程序中的一个特定页面时,必须向windows应用程序发送一个信号,表示一个链接已经被点击。我怎么能这么简单呢?我想远离socket编程…

I thought of the web app storing cookies and the desktop application monitoring the cookies folder but that seems a bit clumsy.

我想到了存储cookie的web应用程序和监视cookie文件夹的桌面应用程序,但这似乎有点笨拙。

6 个解决方案

#1


1  

The desktop app could poll the web app via normal http, but having an open connection would probably be better. You could also use something like .NET remoting or WCF to enable a IPC sort of communication.

桌面应用程序可以通过普通的http来轮询web应用程序,但是有一个开放的连接可能会更好。您还可以使用. net remoting或WCF之类的东西来支持IPC类型的通信。

You could also check out Named Pipes for IPC.

您还可以查看IPC的命名管道。

#2


2  

A simple web service should do the trick. Just create a service in ASP.NET and add it as a reference to your desktop application. There's no need to go overkill to accomplish such a simple task.

一个简单的web服务应该可以做到这一点。只需在ASP中创建一个服务。NET并将其添加为桌面应用程序的引用。没有必要为了完成这么简单的任务而过分劳累。

You could use WCF or Remoting or Sockets -- by why waste time when the learning curve is so much higher? Creating a web service will take no time at all and should give you all the functionality you need.

您可以使用WCF或Remoting或Sockets——为什么在学习曲线如此之高的时候浪费时间呢?创建web服务完全不需要花费时间,并且应该为您提供所需的所有功能。

Here's a brief walkthrough to get you up and running.

这里有一个简单的演练,让你启动并运行。

  1. In Visual Studio, create a new web site and select ASP.NET Web Service.
  2. 在Visual Studio中,创建一个新的web站点并选择ASP。净的Web服务。
  3. A stub method will be created called HelloWorld e.g.

    将创建一个名为HelloWorld的存根方法。

    [WebMethod]

    (WebMethod)

    public string HelloWorld() {

    公共字符串HelloWorld(){

    return "Hello World";
    

    }

    }

  4. Run your site.

    你的网站运行。

  5. Go to your desktop application.
  6. 转到桌面应用程序。
  7. Under your solution explorer, right click on the project and click Add Web Reference
  8. 在解决方案资源管理器下,右键单击项目并单击Add Web Reference
  9. Enter the URL of where you have your web service running and click Go**
  10. 输入web服务运行的URL并单击Go**
  11. Once your reference is found, rename it to something meaningful and click Add Reference.
  12. 找到引用后,将其重命名为有意义的内容,然后单击Add reference。
  13. You can now access the methods exposed by your web service as if they are local objects. You should be able to now see the HelloWorld method exposed by the service.
  14. 您现在可以访问web服务公开的方法,就好像它们是本地对象一样。现在应该可以看到服务公开的HelloWorld方法。

** Remember, in order for you to have a web reference with a static URL, you'll need to host the reference in IIS. When Visual Studio runs your service from the IDE, it will create a temporary URL and port, which WILL change on future runs. This is simply to show you how simple working with web services are.

**记住,为了使web引用具有静态URL,需要在IIS中托管该引用。当Visual Studio从IDE运行您的服务时,它将创建一个临时URL和端口,这将在以后的运行中更改。这只是为了向您展示使用web服务是多么简单。

You can add your own methods and return types in the same class as the HelloWorld method, by adding the [WebMethod] flag above your methods. If you change something in your service, go back to your desktop application, right click on the service, and click Update.

通过在方法之上添加[WebMethod]标志,您可以在与HelloWorld方法相同的类中添加自己的方法和返回类型。如果您更改了服务中的某些内容,请返回桌面应用程序,右键单击服务,然后单击Update。

#3


0  

You could use .NET remoting for such kind of tasks. That would mean that the Desktop client must start a .NET remoting server so that the Webserver can call methods of the Desktop client.

对于此类任务,可以使用. net remoting。这意味着桌面客户端必须启动。net remoting服务器,以便web服务器能够调用桌面客户端的方法。

But this may impose security risks. Additionally, routers or firewalls may block access to the clients. Maybe it is better to use polling, that is the desktop client queries the server periodically for new events. For that, a simple REST-API would suffice, although .NET remoting could be used here also (as well as SOAP or CORBA).

但这可能会带来安全风险。另外,路由器或防火墙可能会阻塞对客户端的访问。也许使用轮询更好,即桌面客户端定期查询服务器以获取新事件。为此,一个简单的REST-API就足够了,尽管这里也可以使用. net remoting(以及SOAP或CORBA)。

#4


0  

Probably equally clumsy as the cookies idea, but the web app could store information about the link that was clicked (when, by who, values of controls on the page, etc.) and store it in a database which the client application periodically polls.

可能和cookie想法一样笨拙,但是web应用程序可以存储关于被单击的链接的信息(由谁、由谁、页面上控件的值等),并将其存储在客户端应用程序定期轮询的数据库中。

#5


0  

Have your windows application harbour a self-hosted web service and call this web service from your web app whenever the user clicks the button. See e.g. here on how to do this. It's really easy and the way to go, imho.

让您的windows应用程序包含一个自托管的web服务,并在用户单击按钮时从web应用程序调用此web服务。看这里,如何做这个。这真的很容易,而且要走的路,伊姆欧。

(You don't want sockets... Well, there are sockets buried deep in this solution, but then there are in all the solutions suggested so far, a part from named pipes and shared files but those both have serious disatvantages. Anyway, you won't be bothered by these sockets, they are taken care of by the framework, no worries.)

(你不想套接字…在这个解决方案中有很多套接字,但是到目前为止所建议的所有解决方案中都有,一部分来自命名管道和共享文件,但它们都有严重的不利之处。无论如何,您不会被这些套接字所困扰,它们是由框架来处理的,不用担心)。

#6


0  

As always, there's more than one way to skin the cat. Here are my take on it.

和往常一样,剥猫皮的方法不止一种。这是我的看法。

if you don't need instantaneous response but require the scalability to support alot of desktop clients

如果您不需要即时响应,但需要可扩展性来支持大量的桌面客户端

the click can simply manifest itself as an event in a "command bus" (read: event aggregator). The items within the command bus is exposed through a web service. the desktop app then can simply "observe" the command bus by polling the web service if there's a new 'command' that it hasn't seen before.

单击可以简单地显示为“命令总线”(读取:事件聚合器)中的事件。命令总线中的项通过web服务公开。然后桌面应用程序就可以通过轮询web服务来“观察”命令总线,如果有一个新的“命令”,它以前没有见过。

Potentially using Reactive Extension for .Net can help here. The web exposes IObservable and the desktop client becomes the IObserver

对于。net使用反应性扩展可能有帮助。web公开了IObservable,桌面客户机变成了IObserver

if you need a more instantaneous response, but will only have limited number of desktop clients, and each client have permanent IP Addresses

如果您需要更快速的响应,但是只会有有限数量的桌面客户端,并且每个客户端都有永久的IP地址。

host wcf service in the desktop client. Bind this using net tcp against your public ip address (for e.g net.tcp://xxx.xxx.xxx.xxx:8523/Service1). When a user clicks a button on the website, the website will then send a request to the wcf service which is hosted within your desktop client. the details of this is pretty blurry to me, but seems like a logical approach.

在桌面客户端中托管wcf服务。使用net tcp对您的公共ip地址(用于e)进行绑定。g net.tcp:/ / xxx.xxx.xxx.xxx:8523 / Service1)。当用户单击网站上的一个按钮时,该网站将向位于桌面客户端的wcf服务发送一个请求。对我来说,细节是相当模糊的,但似乎是一种合乎逻辑的方法。

if you need a more instantaneous response, but will only have limited number of desktop clients, and each client do not have permanent IP Addresses, and willing to pay for cloud service Here's where a Azure Service Bus might come in handy. Everytime a user clicks on a link, a message gets pushed to the Azure Service Bus. Each of the desktop client can subscribe to events in the Azure Service Bus. Here's an example Azure service

如果您需要更即时的响应,但是桌面客户端数量有限,并且每个客户端都没有永久的IP地址,并且愿意为云服务付费,那么Azure服务总线就派上用场了。每次用户单击一个链接时,都会将消息推送到Azure服务总线。每个桌面客户端都可以订阅Azure服务总线中的事件。下面是Azure服务的示例

#1


1  

The desktop app could poll the web app via normal http, but having an open connection would probably be better. You could also use something like .NET remoting or WCF to enable a IPC sort of communication.

桌面应用程序可以通过普通的http来轮询web应用程序,但是有一个开放的连接可能会更好。您还可以使用. net remoting或WCF之类的东西来支持IPC类型的通信。

You could also check out Named Pipes for IPC.

您还可以查看IPC的命名管道。

#2


2  

A simple web service should do the trick. Just create a service in ASP.NET and add it as a reference to your desktop application. There's no need to go overkill to accomplish such a simple task.

一个简单的web服务应该可以做到这一点。只需在ASP中创建一个服务。NET并将其添加为桌面应用程序的引用。没有必要为了完成这么简单的任务而过分劳累。

You could use WCF or Remoting or Sockets -- by why waste time when the learning curve is so much higher? Creating a web service will take no time at all and should give you all the functionality you need.

您可以使用WCF或Remoting或Sockets——为什么在学习曲线如此之高的时候浪费时间呢?创建web服务完全不需要花费时间,并且应该为您提供所需的所有功能。

Here's a brief walkthrough to get you up and running.

这里有一个简单的演练,让你启动并运行。

  1. In Visual Studio, create a new web site and select ASP.NET Web Service.
  2. 在Visual Studio中,创建一个新的web站点并选择ASP。净的Web服务。
  3. A stub method will be created called HelloWorld e.g.

    将创建一个名为HelloWorld的存根方法。

    [WebMethod]

    (WebMethod)

    public string HelloWorld() {

    公共字符串HelloWorld(){

    return "Hello World";
    

    }

    }

  4. Run your site.

    你的网站运行。

  5. Go to your desktop application.
  6. 转到桌面应用程序。
  7. Under your solution explorer, right click on the project and click Add Web Reference
  8. 在解决方案资源管理器下,右键单击项目并单击Add Web Reference
  9. Enter the URL of where you have your web service running and click Go**
  10. 输入web服务运行的URL并单击Go**
  11. Once your reference is found, rename it to something meaningful and click Add Reference.
  12. 找到引用后,将其重命名为有意义的内容,然后单击Add reference。
  13. You can now access the methods exposed by your web service as if they are local objects. You should be able to now see the HelloWorld method exposed by the service.
  14. 您现在可以访问web服务公开的方法,就好像它们是本地对象一样。现在应该可以看到服务公开的HelloWorld方法。

** Remember, in order for you to have a web reference with a static URL, you'll need to host the reference in IIS. When Visual Studio runs your service from the IDE, it will create a temporary URL and port, which WILL change on future runs. This is simply to show you how simple working with web services are.

**记住,为了使web引用具有静态URL,需要在IIS中托管该引用。当Visual Studio从IDE运行您的服务时,它将创建一个临时URL和端口,这将在以后的运行中更改。这只是为了向您展示使用web服务是多么简单。

You can add your own methods and return types in the same class as the HelloWorld method, by adding the [WebMethod] flag above your methods. If you change something in your service, go back to your desktop application, right click on the service, and click Update.

通过在方法之上添加[WebMethod]标志,您可以在与HelloWorld方法相同的类中添加自己的方法和返回类型。如果您更改了服务中的某些内容,请返回桌面应用程序,右键单击服务,然后单击Update。

#3


0  

You could use .NET remoting for such kind of tasks. That would mean that the Desktop client must start a .NET remoting server so that the Webserver can call methods of the Desktop client.

对于此类任务,可以使用. net remoting。这意味着桌面客户端必须启动。net remoting服务器,以便web服务器能够调用桌面客户端的方法。

But this may impose security risks. Additionally, routers or firewalls may block access to the clients. Maybe it is better to use polling, that is the desktop client queries the server periodically for new events. For that, a simple REST-API would suffice, although .NET remoting could be used here also (as well as SOAP or CORBA).

但这可能会带来安全风险。另外,路由器或防火墙可能会阻塞对客户端的访问。也许使用轮询更好,即桌面客户端定期查询服务器以获取新事件。为此,一个简单的REST-API就足够了,尽管这里也可以使用. net remoting(以及SOAP或CORBA)。

#4


0  

Probably equally clumsy as the cookies idea, but the web app could store information about the link that was clicked (when, by who, values of controls on the page, etc.) and store it in a database which the client application periodically polls.

可能和cookie想法一样笨拙,但是web应用程序可以存储关于被单击的链接的信息(由谁、由谁、页面上控件的值等),并将其存储在客户端应用程序定期轮询的数据库中。

#5


0  

Have your windows application harbour a self-hosted web service and call this web service from your web app whenever the user clicks the button. See e.g. here on how to do this. It's really easy and the way to go, imho.

让您的windows应用程序包含一个自托管的web服务,并在用户单击按钮时从web应用程序调用此web服务。看这里,如何做这个。这真的很容易,而且要走的路,伊姆欧。

(You don't want sockets... Well, there are sockets buried deep in this solution, but then there are in all the solutions suggested so far, a part from named pipes and shared files but those both have serious disatvantages. Anyway, you won't be bothered by these sockets, they are taken care of by the framework, no worries.)

(你不想套接字…在这个解决方案中有很多套接字,但是到目前为止所建议的所有解决方案中都有,一部分来自命名管道和共享文件,但它们都有严重的不利之处。无论如何,您不会被这些套接字所困扰,它们是由框架来处理的,不用担心)。

#6


0  

As always, there's more than one way to skin the cat. Here are my take on it.

和往常一样,剥猫皮的方法不止一种。这是我的看法。

if you don't need instantaneous response but require the scalability to support alot of desktop clients

如果您不需要即时响应,但需要可扩展性来支持大量的桌面客户端

the click can simply manifest itself as an event in a "command bus" (read: event aggregator). The items within the command bus is exposed through a web service. the desktop app then can simply "observe" the command bus by polling the web service if there's a new 'command' that it hasn't seen before.

单击可以简单地显示为“命令总线”(读取:事件聚合器)中的事件。命令总线中的项通过web服务公开。然后桌面应用程序就可以通过轮询web服务来“观察”命令总线,如果有一个新的“命令”,它以前没有见过。

Potentially using Reactive Extension for .Net can help here. The web exposes IObservable and the desktop client becomes the IObserver

对于。net使用反应性扩展可能有帮助。web公开了IObservable,桌面客户机变成了IObserver

if you need a more instantaneous response, but will only have limited number of desktop clients, and each client have permanent IP Addresses

如果您需要更快速的响应,但是只会有有限数量的桌面客户端,并且每个客户端都有永久的IP地址。

host wcf service in the desktop client. Bind this using net tcp against your public ip address (for e.g net.tcp://xxx.xxx.xxx.xxx:8523/Service1). When a user clicks a button on the website, the website will then send a request to the wcf service which is hosted within your desktop client. the details of this is pretty blurry to me, but seems like a logical approach.

在桌面客户端中托管wcf服务。使用net tcp对您的公共ip地址(用于e)进行绑定。g net.tcp:/ / xxx.xxx.xxx.xxx:8523 / Service1)。当用户单击网站上的一个按钮时,该网站将向位于桌面客户端的wcf服务发送一个请求。对我来说,细节是相当模糊的,但似乎是一种合乎逻辑的方法。

if you need a more instantaneous response, but will only have limited number of desktop clients, and each client do not have permanent IP Addresses, and willing to pay for cloud service Here's where a Azure Service Bus might come in handy. Everytime a user clicks on a link, a message gets pushed to the Azure Service Bus. Each of the desktop client can subscribe to events in the Azure Service Bus. Here's an example Azure service

如果您需要更即时的响应,但是桌面客户端数量有限,并且每个客户端都没有永久的IP地址,并且愿意为云服务付费,那么Azure服务总线就派上用场了。每次用户单击一个链接时,都会将消息推送到Azure服务总线。每个桌面客户端都可以订阅Azure服务总线中的事件。下面是Azure服务的示例