日期:2014-05-18  浏览次数:20435 次

ASP.NET下,能使用命名管道连接运行的C#控制台程序吗
目前我这里有这么一种需求
asp.net运行平台网站。
经常性的有某种需求,需要跟运行的后台程序通讯。
我想用命名管道通讯,传递参数。
但是怎么都连不上去。
但是两个后台伺服程序却很容易就能互相连接。

请问asp.net下,能否直接跟经常调用的软件进行命名管道连接呢?

如果我需要asp.net经常调用一个应用程序。
还有什么其他的高效的方法?(每次都去实例化进程,很慢,我想把他做成伺服程序,然后asp.net只要传递参数就可以了。这样会高效很多)


------解决方案--------------------
可以通过下面模式实现:
用户网页->webservices->应用程序

在webservices中与应用程序交互的对象必须是静态单实例模式的,因为web调用的特殊性。
------解决方案--------------------
在asp.net里可通过web services 或remoting实现信息交换
------解决方案--------------------
http://www.codeproject.com/KB/threads/dotnetnamedpipespart1.aspx

把它的代码下载下来,然后编译运行服务器端。

aspx代码类似

 
C# code
protected void Page_Load(object sender, EventArgs e)
    {
        IInterProcessConnection clientConnection = null;
        try
        {
            clientConnection = new ClientPipeConnection("MyPipe", ".");
            clientConnection.Connect();
            clientConnection.Write("aa");
            Response.Write(clientConnection.Read());
            clientConnection.Close();
        }
        catch (Exception ex)
        {
            clientConnection.Dispose();
            Response.Write (ex.Message);
        }
    }

------解决方案--------------------
跟后台程序通讯最简单的办法还是通过database来做比较稳妥。
------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Text;
using AppModule.InterProcessComm;
using AppModule.NamedPipes;
using System.Threading;
namespace Server
{
    class Program
    {
        //**c#中用namedpipe进程间通信
        //**组件代码来自codeproject
        //**http://www.codeproject.com/KB/threads/dotnetnamedpipespart1.aspx 
        //**下载上面链接中的代码,编译AppModule.InterProcessComm和AppModule.NamedPipes两个dll
        //**引用这两个dll到本例中,运行如下代码作为服务器端测试
        //**测试代码by jinjazz(因为原作者的两个测试程序比较复杂,这里简化后供大家参考)
        static void Main(string[] args)
        {
            ServerPipeConnection PipeConnection = new ServerPipeConnection("np-test-by-jinjazz", 512, 512, 5000, false);
            Console.WriteLine("listening..");
            while (true)
            {
                try
                {
                    PipeConnection.Disconnect();
                    PipeConnection.Connect();
                    string request = PipeConnection.Read();
                    if (!string.IsNullOrEmpty(request))
                    {
                        Console.WriteLine("get:" + request);
                        PipeConnection.Write("get:" + request);
                        if (request.ToLower() == "break") break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }
            PipeConnection.Dispose();
            Console.Write("press any key to exit..");
            Console.Read();
        }

        //**客户端测试代码如下,可以是应用程序或者web程序
#if false
    /// <summary>
    /// 测试namepiped客户端
    /// </summary>
    /// <param name="request">发送命令</param>
    /// <returns>返回数据</returns>
    string SendRequest(string request)
    {
        string response="";
        IInterProcessConnection clientConnection = null;
        try
        {
            clientConnection = new ClientPipeConnection("np-test-by-jinjazz", "10.10.1.57");
            clientConnection.Connect();
            clientConnection.Write(request);
            response=clientConnection.Read();
            clientConnection.Close();
        }
        catch (Exception ex)
        {
            clientConnection.Dispose();
            response