2

2つの.netアプリケーションがあります。1つは通常のWindowsフォームアプリケーションで、もう1つはMicrosoftWordCOMアドインです。私はC#で両方のアプリケーションを開発しています。

互いに通信するには、これら2つのアプリケーションが必要です。これを達成するための最良の方法は何だろうかと思います。

私が最初にしたことは、これを行うには双方向の名前付きパイプを使用する必要があるということでしたが、名前付きパイプはシステム全体であり、接続を同じセッションで実行されているプロセスに制限する必要があります(これはターミナルサーバー)。

名前付きパイプを現在のセッションに制限する方法はありますか?選択肢がない場合はどうすればよいですか?

ありがとう

4

1 に答える 1

0

これを実現するローカル Web サービスを作成できます。

Web サービスを作成するには、次のようなことを行う必要があります。

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";
        }

    }
}

Web サービスを実行すると、次のように表示されます。

ここに画像の説明を入力

別のプログラム/スレッド (この場合はコンソール アプリケーション)

次のようにそのサービスに接続できるはずです。

ここに画像の説明を入力

ここに画像の説明を入力

ここに画像の説明を入力

最後に、作成したサービスの URL を入力します。

ここに画像の説明を入力

これで、このコンソール アプリケーションから Service1 クラスのオブジェクトを次のようにインスタンス化できます。

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();


        }
    }
}

この手法を使用すると、ほとんど何でも他のアプリケーションに渡すことができます (シリアライズ可能なものなら何でも)。したがって、この Web サービスを 3 番目のスレッドとして作成して、より整理することができます。お役に立てれば。

于 2011-10-21T03:51:45.357 に答える