110,545
社区成员
发帖
与我相关
我的任务
分享
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);
}
<?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>
<client>
<!-- &&&&&&&& 需要发布后修改的地方 -->
<wellknown type="MyServiceComponent.MyComponent,MyServiceComponent"
url="tcp://192.168.5.2:8088/HongjunguoRemotingService" />
</client>
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);