- PR -

IPCチャネルを使った通信でサーバの再起動時のエラーについて

1
投稿者投稿内容
longhorn7
会議室デビュー日: 2008/03/09
投稿数: 13
投稿日時: 2008-08-22 16:40
こんにちは。アべです。

IPC通信で、サーバを再起動すると「
System.Runtime.Remoting.RemotingException はハンドルされませんでした。
Message="IPC ポートを作成できません。アクセスが拒否されました。\r\n"
Source="System.Runtime.Remoting"
StackTrace:」
が発生し困っています。

手順
1)サーバを起動
2)クライアントを起動し、クライアントからサーバを停止。
3)サーバを再起動する。←ここで、エラーが発生する。

当方、
RemotingServices.Unmarshal(_remoteObject);

ChannelServices.UnregisterChannel(_servChannel);
などの削除処理をしてみたのですが、うまく動作しませんでした。

あとは、WellKnownObjectModeを変更した入り、
セキュリティをいじったりしても再起動に失敗し、
困っております。

何とかサーバを再起動し、続けて処理たいのですが、可能なのでしょうか?

何か、ご存知の方、アドバイスをお願いします。
よろしくお願いします。

サーバ
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;

using ClassLibrary;
using System.Threading;

namespace IPCServer
{
public partial class Form1 : Form
{
// Invoke() 用デリゲート
private delegate void DelegateThread();

IpcServerChannel _servChannel;

RemoteMessage _msg = new RemoteMessage();

ObjRef _remoteObject;

public Form1()
{
InitializeComponent();
}

/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
// IPC Channelを作成

_servChannel = new IpcServerChannel("remote");

// リモートオブジェクトを登録
ChannelServices.RegisterChannel(_servChannel, true); //falseは試したがNGだった。

// ChannelのURIを表示
Console.WriteLine("Listening on {0}", _servChannel.GetChannelUri());

// Expose an object
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteMessage), "mesage", WellKnownObjectMode.Singleton);


// イベントを登録
_msg.eventCall += new RemoteMessage.CallHandler(msg_eventCall);

_remoteObject = RemotingServices.Marshal(_msg, "message", typeof(RemoteMessage));


}


void msg_eventCall(string str)
{
new Thread(new ThreadStart(delegate
{
Invoke((DelegateThread)delegate
{ // 匿名メソッドを Del 型にキャストし, それを呼び出す。
textOuptut.Text = str; // コントロールの操作
});

if (str == "show")
{
Invoke((DelegateThread)delegate
{ // 匿名メソッドを Del 型にキャストし, それを呼び出す。
Visible = true;
});
}

if (str == "hidden")
{
Invoke((DelegateThread)delegate
{ // 匿名メソッドを Del 型にキャストし, それを呼び出す。
Visible = false;
});
}

if (str == "end")
{
Invoke((DelegateThread)delegate
{ // 匿名メソッドを Del 型にキャストし, それを呼び出す。
RemotingServices.Unmarshal(_remoteObject);

ChannelServices.UnregisterChannel(_servChannel);

Application.Exit();
});
}

})).Start();


}
}
}

クライアント
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Ipc;

using ClassLibrary;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;

namespace IPCClient
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void textInput_TextChanged(object sender, EventArgs e)
{
// オブジェクトを作成
RemoteMessage _msg;

// IPC Channel を作成
IpcClientChannel clientChannel = new IpcClientChannel();

// リモートオブジェクトを登録
ChannelServices.RegisterChannel(clientChannel, true);//falseは試したがためだった。

// Expose an object
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteMessage), "mesage", WellKnownObjectMode.Singleton);

// オブジェクトを作成
_msg = (RemoteMessage)Activator.GetObject(typeof(RemoteMessage), "ipc://remote/message");

try
{

_msg.Call(textInput.Text);
}
catch (RemotingException ex)
{
//if (ex.
//MessageBox.Show("サーバが動いていない" + ex.ToString());
MessageBox.Show("サーバが動いていない:" + ex.Message);
//textInput.Text += Environment.NewLine + "PG終了済み";
}
finally
{
//毎回コールを実行
ChannelServices.UnregisterChannel(clientChannel);
}

}

}
}
共通クラス


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

namespace ClassLibrary
{
public class RemoteMessage : MarshalByRefObject
{
public delegate void CallHandler(string str);
public event CallHandler eventCall;

public void Call(string str)
{
eventCall(str);
}

}
}

なちゃ
ぬし
会議室デビュー日: 2003/06/11
投稿数: 872
投稿日時: 2008-08-23 01:38
うーん、こりゃまたなんとも妙なプログラムですが…

とりあえず、サーバが終了したと思われる時点で、サーバのプロセスが生きてないか確認してみてください。
longhorn7
会議室デビュー日: 2008/03/09
投稿数: 13
投稿日時: 2008-08-25 09:36
アドバイス有難うございます。
サーバが終了した時点でプロセスは残っていませんでした。
(デバック実行、EXE実行の両方で試してみました。)

どうも、クライアントを停止しておくとサーバが正ししく再起動します。
クライアントが何かをつかんでいるため、サーバの起動時にエラーがでているようです。
なゆた
会議室デビュー日: 2007/06/05
投稿数: 11
投稿日時: 2009-01-04 20:08
<del>
超亀レスですが、同じ問題にぶつかった他の方のために。
クライアント側でActivator.GetObjectで取得したオブジェクトを、使い終わった後に明示的に破棄してみてください。
vb.netだとobj = Nothingで解決しました。
スコープで暗黙で破棄されそうに思えるのですが、どうも持続時間が経過するまで破棄されないようです。
</del>


端末を変えたらエラーが出るようになってしまったのでこの情報は誤りでした。

[ メッセージ編集済み 編集者: なゆた 編集日時 2009-01-05 14:42 ]
q
ベテラン
会議室デビュー日: 2009/01/06
投稿数: 54
投稿日時: 2009-01-06 15:23
(利用規約違反のため削除いたしました。@ITクラブメンバーシップセンター)
q
ベテラン
会議室デビュー日: 2009/01/06
投稿数: 54
投稿日時: 2009-01-06 15:42
(利用規約違反のため削除いたしました。@ITクラブメンバーシップセンター)
q
ベテラン
会議室デビュー日: 2009/01/06
投稿数: 54
投稿日時: 2009-01-06 15:43
(利用規約違反のため削除いたしました。@ITクラブメンバーシップセンター)
1

スキルアップ/キャリアアップ(JOB@IT)