跨应用程序通信(C#) [英] Cross Application Communication (C#)

查看:92
本文介绍了跨应用程序通信(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为在同一服务器上运行的一组应用程序开发软件解决方案.

应用程序松散相关,并共享一个事件日志.我们遇到的问题是性能,每个应用程序每次需要记录事件时都对数据库进行调用.

我想做的是通过删除应用程序对数据库的直接调用来分离整个过程,并通过运行在计算机上的服务路由它们,该服务的唯一目的是处理来自计算机上多个应用程序的事件. /p>

最终,我的目标是在事件"帮助器对象中实现某种系统,该系统将允许这些对象直接与事件"服务进行通信.

我的第一个直觉是使用您的典型事件",但是从我所做的研究中可以看出,不可能在一个过程中调用事件而要在另一个过程中进行处理.

作为我研究的一部分,我遇到了监听另一个应用程序中的事件 C#Win32消息传递与SendMessage .

Sendmessage看起来是一个很有前途的解决方案,但我想确定一下,所以我与一位接近该项目的同事交谈(原始开发人员在此项目完成之前转移到了一个新项目中),他提供了有关情况的其他信息.他们显然已经决定使用WCF并将其构建为Web服务.除了服务器本身的位置和安全级别,这可能行得通.

他认为有可能在操作系统级别实现WCF系统,而不必在Web服务环境中使用它.

我的问题是...是否可以在操作系统级别使用WCF?"和在此情况下,列出的哪个选项最有效?"请记住,这必须解耦,并且应用程序不能与数据库本身中的事件日志进行任何交互.

WCF更新:

所以我开始将一些东西放在一起,这就是我的想法.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace SelfHost
{
    [ServiceContract]
    public interface ISelfHostingService
    {
        [OperationContract]
        string SelfHost(string name);
    }
    public class SelfHostingService : ISelfHostingService
    {
        public string SelfHost(string name)
        {
            return string.Format("Hello, {0}", name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8080/SelfHost");
            ServiceHost host = new ServiceHost(typeof(SelfHostingService), baseAddress);
            host.AddServiceEndpoint(typeof(SelfHost.ISelfHostingService), new BasicHttpBinding(), baseAddress);
                host.Open();
                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();
        }
    }
}

但是有一个问题.这行:

Uri baseAddress = new Uri("http://localhost:8080/SelfHost");

我保证服务器不会允许该服务注册该本地地址(已经尝试过并且是失败的).

所以我的新问题是...是否有一种方法不涉及更改服务器本身上的配置设置?"

MSMQ更新:

这绝对是一个选项,但是... [怀孕的暂停]我们确实将消息队列用于其他功能.我唯一的犹豫是开销.我希望它已经完全解耦,我正在寻找一个应用程序到应用程序解决方案.我希望服务是监听"而不是去获得.

最终

我做了很多研究,因此我决定使用WCF是我的最大利益. 作为Windows服务的一部分,我计划为事件日志记录服务添加app.config,然后将服务配置为通过localhost使用命名管道.

感谢所有帮助

跟进

对于任何可能感兴趣的人.这很漂亮. net.pipe处于活动状态,我能够创建事件并将其从多个应用程序发送到服务,而处理时间很少或没有.

将wcf服务封装在一个非常简单的Windows服务中,该服务只是打开服务管道.在客户端,我能够轻松发现并实施该服务.我要做的就是调用客户端类,它在数据库中实时显示我的事件.

再次感谢.

解决方案

您可以在没有Web托管和IIS的情况下执行WCF,这些WCF服务将是TcpIp,并且可以在您的本地网络中正常工作,这样您就不会没有SOAP序列化.

您还可以使用一种方法(我曾在一家拥有多服务器多层分布式应用程序的前公司中使用过)是使您的事件帮助器对象简单地将消息排队到将驻留在MSMQ上的MSMQ.在某些服务器上,此方法效果很好,因为它不像SendMessage那样同步,因此即使侦听器应用程序不可用且未运行或仅处于繁忙状态,您的应用程序也可以正常工作.

然后,您可以在该计算机上运行Windows Service(我们将其称为LoggingManager),该服务从队列中窥视消息并以自己的速度和安宁的方式在数据库中创建日志;-)

I'm working on a software solution to for a group of applications running on the same server.

The applications are loosely related, and share an event log. The problem that we are running into is performance, with each application making calls to the database every time they need to log an event.

What I'm trying to do is decouple the entire process by removing the applications direct calls to the database, and routing them through a service running on the machine whos sole purpose is processing events from the multiple applications on the machine.

Ultimately my goal is to implement some sort of system in the "Event" Helper Object that would allow those objects to communicate directly with the "Event" Service.

My first instinct was to use your typical "event", but it would appear from the research that I've done that it is not possible to invoke an event in one process to be handled in another process.

As a part of my research I came across Listen for events in another application and C# Win32 messaging with SendMessage.

Sendmessage looks like a promising solution, but I wanted to be sure so I spoke with one of my colleagues who was close to the project (the original developer who was moved on to a new project before the completion of this one) and he imparted some additional information as to the situation. They had apparently attmped to use WCF and build it as a web service. This probably would have worked except for the location and security level of the server itself.

He believes it MAY be possible to implement a WCF system at the OS level without having to use it in a web service environment.

My question is... "Is using WCF possible at the OS level?" and "Which of the options listed would be the most efficent under the scenario?" Keeping in mind that this MUST be decoupled and the applications cannot have any interaction with the event log in the database itself.

WCF Update:

So I started putting something together and this is what I came up with..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace SelfHost
{
    [ServiceContract]
    public interface ISelfHostingService
    {
        [OperationContract]
        string SelfHost(string name);
    }
    public class SelfHostingService : ISelfHostingService
    {
        public string SelfHost(string name)
        {
            return string.Format("Hello, {0}", name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8080/SelfHost");
            ServiceHost host = new ServiceHost(typeof(SelfHostingService), baseAddress);
            host.AddServiceEndpoint(typeof(SelfHost.ISelfHostingService), new BasicHttpBinding(), baseAddress);
                host.Open();
                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();
        }
    }
}

But there is a problem. This line:

Uri baseAddress = new Uri("http://localhost:8080/SelfHost");

I guarantee that the server will not allow the service to register that local address (its been tried already and it was a flop).

So my new question is... "Is there a way around that that does not involve changing configuration settings on the server itself?"

MSMQ Update:

This is deffinately an option but... [pregnant pause] We do use the message queue for other pieces of functionality. My only hesitation is the overhead. I'd rather that it was completely decoupled I'm looking for an application to application solution. I'd rather that the service was "listening" instead of going to get.

Finale

I did a lot more research and I've decided that using WCF is in my best interest. As a part of the windows service I plan on adding an app.config for the event logging service and then configuring the service to use named pipes over localhost.

thanks for all the help

Follow-Up

For anyone who might be interested. This works beautifuly. The net.pipe is active and I am able to create events and send them to the service from multiple apps with little or no processing time.

The wcf service is encased in a very simple windows service that simply opens the service pipe. on the client side i was able to discover and implement the service easily. All i have to do is make a call to the client class and it shows my events in real time in the database.

Thanks again.

解决方案

you could do WCF without web hosting and without IIS, those WCF services will be TcpIp and will work in your local network with no problems, in this way you don't have SOAP Serialization.

One approach you could also use ( and I did use in a former company where we had a multi server multi-tier distributed application ), is to make your Event Helper Object to simply queue messages to a MSMQ which will reside on a certain server, this method works very well because is not synchronous like SendMessage so your application works even if the listener application is not available and not running or simply busy.

Then you could have a windows Service running on that machine ( we used to call it LoggingManager ) which peeks the messages from the queue and created the log in the databases at his own speed and peace ;-)

这篇关于跨应用程序通信(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆