远程处理配置失败,异常为“RemotingException: 试图重定向类型“MySC.MyComponent, MyServiceComponent”的激活,而该类型已被重定向

蝈蝈俊 2008-04-18 10:09:18
我想用 .net Remoting 实现这样的一个效果.

有三台PC: A,B,C
B 和 C 作为 服务器, 部署了同样的.net Remoting服务.
A PC 根据用户自定义的逻辑,自动调用B或者C 服务。

但是我一直没有测试通过这样的代码,
下面就是我最新的测试这个功能的代码,单独使用B或者 C 服务时, 没有任何问题,但是已经使用了一个,切换另外一个时,就报下面错误:


远程处理配置失败,异常为“System.Runtime.Remoting.RemotingException:
试图重定向类型“MyServiceComponent.MyComponent, MyServiceComponent”的激活,而该类型已被重定向。
在 System.Runtime.Remoting.RemotingConfigHandler.RemotingConfigInfo.AddWellKnownClientType(WellKnownClientTypeEntry entry)
在 System.Runtime.Remoting.RemotingConfigHandler.RegisterWellKnownClientType(WellKnownClientTypeEntry entry)
在 System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(WellKnownClientTypeEntry entry)
在 System.Runtime.Remoting.RemotingConfigHandler.RemotingConfigInfo.StoreRemoteAppEntries(RemotingXmlConfigFileData configData)
在 System.Runtime.Remoting.RemotingConfigHandler.ConfigureRemoting(RemotingXmlConfigFileData configData, Boolean ensureSecurity)”。





namespace MyServiceComponent
{
public class MyComponent : MarshalByRefObject
{
public string GetString(short s)
{
if (s <= 10)
return string.Format("<=10 {0}", GetIP());
else
return string.Format("大于10 {0}", GetIP());
}

protected string GetIP() //获取本地IP
{
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
return ipAddr.ToString();
}
}
}



服务器段
服务器段配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="Singleton" type="MyServiceComponent.MyComponent, MyServiceComponent"
objectUri="HongjunguoRemotingService" />
</service>
<channels>
<!-- 这里的配置方法,请参看MSDN中 远程处理安全通道技术示例 -->
<channel ref="tcp" port="8088" secure="true" impersonate="true" protectionLevel="EncryptAndSign" />
<serverProviders>
<formatter href="binary" typeFilterLevel="Full"/>
</serverProviders>
</channels>
</application>
<!--
只有把 customErrors 配置成 Off ,服务器端的详细错误异常,才能传递到客户端
默认是不传递完整的错误异常的。
-->
<customErrors mode ="Off" />
</system.runtime.remoting>
</configuration>


服务器端核心代码

RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false);



客户端

客户端核心调用代码:

private Dictionary<string, MyServiceComponent.MyComponent> dict =
new Dictionary<string, MyServiceComponent.MyComponent>(2);


private void button1_Click(object sender, EventArgs e)
{
short ss = 0;

if (!short.TryParse(this.textBox1.Text, out ss))
return;

// 根据用户的选则,切换 .net Remoting 服务, 不同的服务配置在不同的 config 文件中了
string key = string.Empty;
if (radioButton1.Checked)
key = "s1.config";
else if (radioButton2.Checked)
key = "s2.config";
else
return;

MyServiceComponent.MyComponent com = null;
if (!dict.TryGetValue(key, out com))
{
string configFile = Path.Combine(Path.GetDirectoryName(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile), key);

RemotingConfiguration.Configure(configFile, true);

com = new MyServiceComponent.MyComponent();

dict.Add(key, com);
}
else
{
if (com == null) return;
}


string www = com.GetString(ss);
MessageBox.Show(www);

}


客户端配置的一个简单代码
s1.config, 连接到 192.168.5.2 这台服务器。
s2.config, 的配置类似, 连接到另外一台服务器


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<!-- 这里的配置方法,请参看MSDN中 远程处理安全通道技术示例 -->
<!--
到部署环境的话,需要额外配置 username="ghj1976" password="******" domain="***"
这个技术请参看:

http://www.cnblogs.com/zhengyun_ustc/archive/2006/06/09/remoting_InvalidCredentialException.html
http://forums.asp.net/thread/1626741.aspx
-->
<!-- &&&&&&&& 需要发布后修改的地方 -->
<channel name="Channel1" ref="tcp" port="8081" secure="true" tokenImpersonationLevel="impersonation" protectionLevel="EncryptAndSign"
username="ghj1976" password="****" domain="***" />
<clientProviders>
<formatter ref="binary" typeFilterLevel="Full" />
</clientProviders>
</channels>
<client>
<!-- &&&&&&&& 需要发布后修改的地方 -->
<wellknown type="MyServiceComponent.MyComponent,MyServiceComponent"
url="tcp://192.168.5.2:8088/HongjunguoRemotingService" />
</client>
</application>
<customErrors mode ="Off" />
</system.runtime.remoting>
</configuration>







要实现上面的效果, 我该如何办呀???
...全文
1216 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yilanwuyu123 2008-04-18
  • 打赏
  • 举报
回复
MARK 楼主跟踪下源代码 估计应该在切换的时候创建新的实例吧。
nullpassword 2008-04-18
  • 打赏
  • 举报
回复
关注
蝈蝈俊 2008-04-18
  • 打赏
  • 举报
回复
这个问题的一个简单总结我写在这里了

http://blog.joycode.com/ghj/archive/2008/04/18/115078.aspx
蝈蝈俊 2008-04-18
  • 打赏
  • 举报
回复
找到解决方法了,步骤如下:

1、客户端 s1.config s2.config 配置中, 不配置下面信息,删除下面配置信息



<client>
<!-- &&&&&&&& 需要发布后修改的地方 -->
<wellknown type="MyServiceComponent.MyComponent,MyServiceComponent"
url="tcp://192.168.5.2:8088/HongjunguoRemotingService" />
</client>



2、客户端段调用用下面代码


string key = string.Empty;
string channel = string.Empty;
if (radioButton1.Checked)
{
key = "s1.config";
channel = "tcp://192.168.5.2:8088/HongjunguoRemotingService";
}
else if (radioButton2.Checked)
{
key = "s2.config";
channel = "tcp://192.168.5.7:8088/HongjunguoRemotingService";
}
else
return;

MyServiceComponent.MyComponent com = null;
if (!dict.TryGetValue(key, out com))
{
string configFile = Path.Combine(
Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile), key);

RemotingConfiguration.Configure(configFile, true);

//com = new MyServiceComponent.MyComponent();
com = (MyServiceComponent.MyComponent)
Activator.GetObject(typeof(MyServiceComponent.MyComponent), channel);

dict.Add(key, com);
}
else
{
if (com == null) return;
}


string www = com.GetString(ss);
MessageBox.Show(www);


蝈蝈俊 2008-04-18
  • 打赏
  • 举报
回复
我也怀疑是哪里。

但是那里被封装在
RemotingConfiguration.Configure(configFile, true);

这么一行代码中了。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧