IDesign C#编码规范(之九)

80酷酷网    80kuku.com

  编码|规范4.5远程操作 Remoting
1.宁可使用管理配置(配置文件)而不使用程序自动配置。
Prefer administrator configuration to programmatic configation.
2.总是在单独调用对象里完成IDisposable。
Always implement IDisposable on single call objects.
3.远程操作时总是选用TCP信道和二进制格式
Always prefer TCP channel and binary format when using remoting.
a)除非设置了防火墙
Unless a firewall is present.
4.总是为一扎对象提供一个null租约。
Always provide a null lease for a singleton object.
Public class MySingleton : MarshalByRefObject
{
public override object InitializeLifetimeService()
{
return null;
}
}
5.总是为客户端激活的对象提供sponsor。这个sponsor应该返回初始租约时刻。
Always provide a sponsor for client activated object. The sponsor should return the initial lease time.
a) See Chapter 10 of Programming .NET Components.
6.在客户端应用程序停止时总是不要注册sponsor。
Always unregister the sponsor on client application shutdown.
7.总是将远程对象放在类库里。
Always put remote objects in class libraries.
8.避免使用SoapSuds。
Avoid using SoapSuds.
9.避免宿主IIS.
Avoid hosting in IIS.
10.避免使用单一定向的信道。
Avoid using uni-directional channels.
11.总是在Main()方法里载入远程配置文件,即使该文件为空。并且该应用程序没有使用远程操作。
Always load a remoting configuration file in Main() even if the file is empty, and the application does not use remoting.
a)允许在该文件重新定义远程调用,配置信道和改变应用程序发布(添加信道等)等。
Allow the option of remoting some types later on, post deployment, and changing the application topology.
Static void Main()
{
RemotingConfiguration.Configue(“MyApp.exe.config”);
/*Rest of Main()*/
}
12.避免对远程对象激活时使用Activator.GetObject() and Activator.CreateInstance()。而是使用new。
Avoid using Activator.GetObject() and Activator.CreateInstance() for remote objects activation. Use new instead.
13.总是在客户端注册port(),目的是允许递归调用。
Always register port() on the client side, to allow callbacks.
14.总是将客户端和服务器端的类型过滤设置为Full,使其可以递归调用。
Always elevate type filtering to full on both client and host to allow callbacks.
Host Config file:
<channels>
<channel ref = “tcp” port = “8005”>
<serverProviders>
<formatter ref = “soap” typeFilterLevel = “Full”/>
<formatter ref = “binary” typeFilterLevel = “Full”/>
</serverProviders>
</channel>
<channel ref = “http” port = “8006”>
<serverProviders>
<formatter ref = “soap” typeFilterLevel = “Full”/>
<formatter ref = “binary” typeFilterLevel = “Full/”>
</serverProviders>
</channel>
</channels>
Client Config file:
<channels>
<channel ref = “tcp” port = “0”>
<serverProviders>
<formatter ref = “soap” typeFilterLevel = “Full”/>
<formatter ref = “binary” typeFilterLevel = “Full”/>
</serverProviders>
</channel>
</channels>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: