关于远程调用和GAC的问题,请高手解答!在线等!
我用C#写了一个程序,想要实现与COM Server相类似的功能,即内存中只有一个对象,由所有应用程序共享。
具体代码如下:
首先是一个.Net组件TextViewer,其中主要有一个FormTextViewer类:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace TextViewer
{
public class FormTextViewer : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textContent;
private System.ComponentModel.Container components = null;
public FormTextViewer()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.textContent = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textContent
//
this.textContent.Location = new System.Drawing.Point(0, 0);
this.textContent.Multiline = true;
this.textContent.Name = "textContent";
this.textContent.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textContent.Size = new System.Drawing.Size(288, 264);
this.textContent.TabIndex = 0;
this.textContent.Text = "";
//
// FormTextViewer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(288, 262);
this.ControlBox = false;
this.Controls.Add(this.textContent);
this.Name = "FormTextViewer";
this.Text = "Text Viewer";
this.ResumeLayout(false);
}
public void OutputText(string sText)
{
textContent.Text=sText;
}
}
}
然后是调用这个控件的程序TestGACApp,其中的主要部分是FormSender类,关键部分大体如下:
FormTextViewer myViewer=null;
if(IsRunning)
{
myViewer = (FormTextViewer)Activator.GetObject(typeof(FormTextViewer), "tcp://localhost:2234/GACServer");
}
else
{
ChannelServices.RegisterChannel(new TcpChannel(2234));
RemotingConfiguration.RegisterWellKnownServiceType(typeof(FormTextViewer), "GACServer", WellKnownObjectMode.Singleton);
myViewer=new FormTextViewer();
}
try
{
myViewer.Show();
myViewer.OutputText(textInput.Text);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
其中的IsRunning是一个通过互斥量来判断程序是否已启动过一次,从而决定是以server的方式启动,还是以client的方式启动。
出于某种原因,我不能把TextViewer.dll文件和TestGACApp.exe文件放在一个目录下,所以我给TextViewer加上了强名称,并把它加入了GAC中。
现在的问题是,第一次运行此程序时(server模式),程序运行良好,第二次运行时(client模式),在执行到myViewer.Show();时出错。从错误信息来看,.net还是只在当前目录下寻找TextViewer.dll,没有找到,所以报错了。在这种情况下,系统不是应该自动去GAC下寻找相应的dll吗?
困惑啊!希望高手解答!