|
楼主发表于: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)”。 - C# code
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 code
<?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>
服务器端核心代码 - C# code
RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false);
客户端 客户端核心调用代码: - C# code
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 code
<?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>
要实现上面的效果, 我该如何办呀??? |
|
该帖子于2008-04-18 10:13:45被管理员修改 |
|