一步一步学Remoting之四:承载方式 - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- 一步一步学Remoting之四:承载方式 (http://www.webasp.net/article/25/24490.htm) |
| -- 作者:未知 -- 发布日期: 2005-05-26 |
| 可能大家会觉得这个过程将是一个复杂的过程,其实不然,下面说一下实现方法,步骤非常少。 先来建立远程对象 using System; using System.Data; using System.Data.SqlClient; namespace RemoteObject { public class MyObject:MarshalByRefObject { public DataSet GetData() { SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["strconn"]); SqlDataAdapter da=new SqlDataAdapter("select * from UBI_ProvinceMaster",conn); DataSet ds=new DataSet(); da.Fill(ds); return ds; } } } 客户端仍然是一个控制台来进行测试: RemoteObject.MyObject app = (RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject),System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]); DataTable dt=app.GetData().Tables[0]; foreach(DataRow dr in dt.Rows) { Console.WriteLine(dr["iPrMId"]+" "+dr["vPrMName"]); } Console.ReadLine(); 服务端配置文件: <configuration> <appSettings> <add key="strconn" value="server=(local);uid=sa;pwd=;database=UBISOFT" /> </appSettings> <system.runtime.remoting> <application name="RemoteServer"> <service> <wellknown type="RemoteObject.MyObject,RemoteObject" objectUri="RemoteObject.MyObject" mode="SingleCall" /> </service> <channels> <channel ref="tcp" port="9999"/> </channels> </application> </system.runtime.remoting> </configuration> 运行程序,我们得到的是一个省市的列表: 一、windows服务承载 用vs.net制作一个windows服务的过程基本不超过10个步骤,所以我们不需要害怕。 1、建立一个新的windows服务项目RemoteServer1 2、打开Service1代码视图,找到OnStart部分,加入代码 System.Runtime.Remoting.RemotingConfiguration.Configure(AppDomain.CurrentDomain.BaseDirectory + "RemoteServer1.exe.config"); config和控制台方式的config是一样的,我们让这个windows服务做的仅仅是从config文件读出配置信息进行配置通道。别忘记添加配置文件。 3、切换到设计视图,右键-添加安装程序 4、切换到新生成的ProjectInstaller.cs设计视图,找到serviceProcessInstaller1对Account属性设置为LocalSystem,对serviceInstaller1的ServiceName属性设置为RemoteServer1(服务的名字),StartType属性设置为Automatic(系统启动的时候自动启动服务) 5、别忘记对添加RemoteObject的引用 6、建立一个新的安装项目RemoteServerSetup(我们为刚才那个服务建立一个安装项目) 7、右键-添加-项目输出-主输出-选择RemoteService1-确定 8、右键-视图-自定义操作-自定义操作上右键-添加自定义操作-打开应用程序文件夹-选择刚才那个主输出-确定 9、重新生成这个安装项目-右键-安装 10、在服务管理器中(我的电脑-右键-管理-服务和应用程序-服务)找到RemoteServer1服务,启动服务 现在就可以打开客户端测试了! 一些FAQ: 1、启动服务的时候系统说了类似“服务什么都没有做,服务已经被停止”表示什么? 表示windows服务出错了,一般是服务的程序有问题,检查服务做了什么?在我们这个程序中仅仅添加了一行代码,一般不会出现这个错误。 2、运行客户端出现“服务器无响应”? 先检查windows服务配置文件是不是正确设置了激活方式和激活对象,客户端服务端端口号是否统一? 3、运行客户端出现“无法找到程序集”? 检查windows服务配置文件是否正确配置了激活对象的类型和uri?服务是否添加了远程对象引用? 4、远程对象类中有用到System.Configuration.ConfigurationSettings.AppSettings["strconn"],但是远程对象并没有配置文件,它从哪里读取这个config的? 因为远程对象不是独立存在的,它是被windows服务承载的,因此它从windows服务的配置文件中读取一些配置信息,远程对象本生不需要配置文件。 5、安装的时候是不是要卸载服务? 不需要,安装程序会 停止服务端-》卸载服务-》安装服务 6、在正式使用的时候怎么部署我们的系统? 如果客户端是程序仅仅只要把安装项目下面3个文件传到服务器进行安装,配置好config文件(比如连接字符串),开启服务即可。如果客户端是网站,同样把服务在服务器安装,配置好config文件(比如连接字符串),开启服务,最后把网站传到web服务器(可能和service不是同一个服务器)。 7、部署的时候需要传远程对象dll吗? 不需要,可以看到安装项目中已经自动存在了这个dll。 8、这样的系统有什么特点? 一个web服务器,多个service服务器,多个sqlservice服务器,web服务器负担比较小,所有的逻辑代码都分布到不同的service服务器上面。 最后说一个测试的tip: 如果我们远程调用对象进行测试程序非常麻烦,我们需要这么做 修改了远程对象-》重新编译安装程序-》在自己机器重新安装服务-》启动服务-》查看结果 其实可以这么做: 1、修改远程对象中的连接数据库字符串,由于不是远程对象了,我们必须从本地读取连接字符串,比如上列我们直接修改为: SqlConnection conn=new SqlConnection("server=(local);uid=sa;pwd=;database=UBISOFT"); 2、修改客户端代码,直接实例化远程对象 //RemoteObject.MyObject app = (RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject),System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]); RemoteObject.MyObject app = new RemoteObject.MyObject();
这里来说一下iis承载方式,顺便简单说一下remoting的通道和【复杂对象】中的遗留问题。 <configuration> <appSettings> <add key="strconn" value="server=(local);uid=sa;pwd=;database=UBISOFT" /> </appSettings> <system.runtime.remoting> <application> <service> <wellknown type="RemoteObject.MyObject,RemoteObject" objectUri="MyObject.soap" mode="SingleCall" /> </service> <channels> <channel ref="http"/> </channels> </application> </system.runtime.remoting> </configuration> 来分析一下这个config: <configuration> <appSettings> <add key="ServiceURL" value="http://localhost/remoting/MyObject.soap"/> </appSettings> </configuration> iis承载方式默认是80端口,我们不需要在端口上做任何设置。还需要注意到的是iis方式,我们使用这样的格式作为地址: <configuration> <system.runtime.remoting> <application name="RemoteServer"> <service> <wellknown type="RemoteObject.MyObject,RemoteObject" objectUri="RemoteObject.MyObject" mode="Singleton" /> </service> <channels> <channel ref="tcp" port="9999"/> <serverProviders> <provider ref="wsdl" /> <formatter ref="soap" typeFilterLevel="Full" /> <formatter ref="binary" typeFilterLevel="Full" /> </serverProviders> </channels> </application> </system.runtime.remoting> </configuration>
using System; using System.Collections; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Serialization.Formatters;
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider(); BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider(); serverProvider.TypeFilterLevel = TypeFilterLevel.Full; IDictionary props = new Hashtable(); props["port"] = 0; TcpChannel channel = new TcpChannel(props,clientProvider,serverProvider); ChannelServices.RegisterChannel(channel);
• 若要对客户端进行身份验证,请使用 HTTP 通道,在 ASP.NET 中驻留对象,以及在 IIS 中禁用匿名访问。 |
| webasp.net |