在上节中,我们创建了一个简单的分布式应用,这一节我们将介绍如何通过配置文件实现上节中的服务端和客户端,由于对配置文件的修改,不需要修改代码,重新编译程序,所以我们建议在以后的应用中,尽可能的使用配置文件来定义通道、端口、对象等等。
上节中服务端应用,在项目中添加配置文件RemotingServerSao.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown
mode="SingleCall" type="Enterprise.Northwind.Business.Remoting.RemotingCustomersBO,
Enterprise.Northwind.Business.Remoting"
objectUri="CustomersBO.soap"
/>
</service>
<channels>
<channel ref="http" port="1234"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
服务端代码修改为:
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("RemotingServerSao.config",false);
Console.WriteLine("Server starts as Sao. Press Enter to exit...");
Console.ReadLine();
}
}
客户端应用中,添加配置文件RemotingClientSao.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown
type="Enterprise.Northwind.Business.Remoting.RemotingCustomersBO, Enterprise.Northwind.Business.Remoting"
url="http://localhost:1234/CustomersBO.soap"
/>
</client>
<channels>
<channel ref="http">
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
客户端代码修改为:
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("RemotingClientSao.config", false);
ICustomersBO customersBO = new RemotingCustomersBO();
Console.WriteLine(customersBO.GetCustomerNameByID(3));
Console.ReadLine();
}
}
关于对象生命期、ClientProviders、ServerProviders的配置我们会在后面的章节中使用到的时候再介绍。
注:本系列文章乃作者原创,转载请注明出处,谢谢。