开发者

How can I make two process comunicate?

I have two .net applications. One is a normal Windows Forms application while the other is a Microsoft Word COM Add-in. I'm developping both applications with C#.

I need theses two application to comunicate with each other. I'm wondering what is the best way to achieve this.

The first thing I though was that I should use a bi-directional named pipe to do this, but named pi开发者_StackOverflow中文版pes are system-wide and I need the connection to be restricted to process runnings in the same session (This could and will be used on a terminal server).

Is there any way to restrict a named pipe to the current session? If there is not what alternatives do I have?

Thanks


you can create a local web service to achieve this.

To create your web service you must do something similar to:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService1
{        
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        public int myInt = 0;

        [WebMethod]
        public int increaseCounter()
        {
            myInt++;
            return myInt;
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

    }
}

when you run your web service you should see something like:

How can I make two process comunicate?

on a different program/thread (in this case console application)

you should be able to connect to that service as:

How can I make two process comunicate?

How can I make two process comunicate?

How can I make two process comunicate?

Lastly type the url of the service you just created:

How can I make two process comunicate?

Now you are able to instantiate an object from that class Service1 from this console application as:

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

namespace ConsoleApplication36
{
    class Program
    {
        static void Main(string[] args)
        {
            localhost.Service1 service = new localhost.Service1();

            // here is the part I don't understand..
            // from a regular class you will expect myInt to increase every time you call
            // the increseCounter method. Even if I call it twice I always get the same result.

            int i;
            i=service.increaseCounter();

            Console.WriteLine(i.ToString());

            // you can recive string data as:
            string s = service.HelloWorld();

            // output response from other program
            Console.WriteLine(s);

            Console.Read();


        }
    }
}

with this technique you will be able to pass mostly anything to your other application (anything that is serializable). so maybe you can create this webservice as a third thread to make it more organized. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜