Factory Pattern在.Net Remoting Architecture中的实现 | |||
|
|||
|
|||
|
|||
|
远程对象的激活方式 | |||
2、远程对象的激活方式
在访问远程类型的一个对象实例之前,必须通过一个名为Activation的进程创建它并进行初始化。这种客户端通过通道来创建远程对象,称为对象的激活。在Remoting中,远程对象的激活分为两大类:服务器端激活和客户端激活。 (1) 服务器端激活,又叫做WellKnow方式,很多又翻译为知名对象。为什么称为知名对象激活模式呢?是因为服务器应用程序在激活对象实例之前会在一个众所周知的统一资源标识符(URI)上来发布这个类型。然后该服务器进程会为此类型配置一个WellKnown对象,并根据指定的端口或地址来发布对象。.Net Remoting把服务器端激活又分为SingleTon模式和SingleCall模式两种。 SingleTon模式:此为有状态模式。如果设置为SingleTon激活方式,则Remoting将为所有客户端建立同一个对象实例。当对象处于活动状态时,SingleTon实例会处理所有后来的客户端访问请求,而不管它们是同一个客户端,还是其他客户端。SingleTon实例将在方法调用中一直维持其状态。举例来说,如果一个远程对象有一个累加方法(i=0;++i),被多个客户端(例如两个)调用。如果设置为SingleTon方式,则第一个客户获得值为1,第二个客户获得值为2,因为他们获得的对象实例是相同的。如果熟悉Asp.Net的状态管理,我们可以认为它是一种Application状态。 SingleCall模式:SingleCall是一种无状态模式。一旦设置为SingleCall模式,则当客户端调用远程对象的方法时,Remoting会为每一个客户端建立一个远程对象实例,至于对象实例的销毁则是由GC自动管理的。同上一个例子而言,则访问远程对象的两个客户获得的都是1。我们仍然可以借鉴Asp.Net的状态管理,认为它是一种Session状态。 (2) 客户端激活。与WellKnown模式不同,Remoting在激活每个对象实例的时候,会给每个客户端激活的类型指派一个URI。客户端激活模式一旦获得客户端的请求,将为每一个客户端都建立一个实例引用。SingleCall模式和客户端激活模式是有区别的:首先,对象实例创建的时间不一样。客户端激活方式是客户一旦发出调用的请求,就实例化;而SingleCall则是要等到调用对象方法时再创建。其次,SingleCall模式激活的对象是无状态的,对象生命期的管理是由GC管理的,而客户端激活的对象则有状态,其生命周期可自定义。其三,两种激活模式在服务器端和客户端实现的方法不一样。尤其是在客户端,SingleCall模式是由GetObject()来激活,它调用对象默认的构造函数。而客户端激活模式,则通过CreateInstance()来激活,它可以传递参数,所以可以调用自定义的构造函数来创建实例。 (1) SingleTon模式 对于WellKnown对象,可以通过静态方法RemotingConfiguration.RegisterWellKnownServiceType()来实现: RemotingConfiguration.RegisterWellKnownServiceType( (2)SingleCall模式 注册对象的方法基本上和SingleTon模式相同,只需要将枚举参数WellKnownObjectMode改为SingleCall就可以了。 RemotingConfiguration.RegisterWellKnownServiceType( (3)客户端激活模式 对于客户端激活模式,使用的方法又有不同,但区别不大,看了代码就一目了然。 RemotingConfiguration.ApplicationName = "ServiceMessage"; 为什么要在注册对象方法前设置ApplicationName属性呢?其实这个属性就是该对象的URI。对于WellKnown模式,URI是放在RegisterWellKnownServiceType()方法的参数中,当然也可以拿出来专门对ApplicationName属性赋值。而RegisterActivatedServiceType()方法的重载中,没有ApplicationName的参数,所以必须分开。 2、客户端获得远程对象。 与服务器端相同,不同的激活模式决定了客户端的实现方式也将不同。不过这个区别仅仅是WellKnown激活模式和客户端激活模式之间的区别,而对于SingleTon和SingleCall模式,客户端的实现完全相同。 (1) WellKnown激活模式 要获得服务器端的知名远程对象,可通过Activator进程的GetObject()方法来获得: ServerRemoteObject.ServerObject serverObj = (ServerRemoteObject.ServerObject)Activator.GetObject( 首先以WellKnown模式激活,客户端获得对象的方法是使用GetObject()。其中参数第一个是远程对象的类型。第二个参数就是服务器端的uri。如果是http通道,自然是用http://localhost:8080/ServiceMessage了。因为我是用本地机,所以这里是localhost,你可以用具体的服务器IP地址来代替它。端口必须和服务器端的端口一致。后面则是服务器定义的远程对象服务名,即ApplicationName属性的内容。 (2) 客户端激活模式 如前所述,WellKnown模式在客户端创建对象时,只能调用默认的构造函数,上面的代码就说明了这一点,因为GetObject()方法不能传递构造函数的参数。而客户端激活模式则可以通过自定义的构造函数来创建远程对象。 客户端激活模式有两种方法: 1) 调用RemotingConfiguration的静态方法RegisterActivatedClientType()。这个方法返回值为Void,它只是将远程对象注册在客户端而已。具体的实例化还需要调用对象类的构造函数。 RemotingConfiguration.RegisterActivatedClientType( 2) 调用进程Activator的CreateInstance()方法。这个方法将创建方法参数指定类型的类对象。它与前面的GetObject()不同的是,它要在客户端调用构造函数,而GetObject()只是获得对象,而创建实例是在服务器端完成的。CreateInstance()方法有很多个重载,我着重说一下其中常用的两个。 a、 public static object CreateInstance(Type type, object[] args, object[] activationAttributes); 参数说明: 这里的参数args是一个object[]数组类型。它可以传递要创建对象的构造函数中的参数。从这里其实可以得到一个结论:WellKnown激活模式所传递的远程对象类,只能使用默认的构造函数;而Activated模式则可以用户自定义构造函数。activationAttributes参数在这个方法中通常用来传递服务器的url。 假设我们的远程对象类ServerObject有个构造函数: ServerObject(string pName,string pSex,int pAge) 那么实现的代码是: object[] attrs = {new UrlAttribute("tcp://localhost:8080/ServiceMessage")}; b、public static ObjectHandle CreateInstance(string assemblyName, string typeName, object[] activationAttribute); 参数说明: 参数说明一目了然。注意这个方法返回值为ObjectHandle类型,因此代码与前不同: object[] attrs = {new UrlAttribute("tcp://localhost:8080/EchoMessage")}; 这个方法实际上是调用的默认构造函数。ObjectHandle.Unwrap()方法是返回被包装的对象。 说明:要使用UrlAttribute,还需要在命名空间中添加:using System.Runtime.Remoting.Activation; |
|||
|
|||
|
|||
|
2005-7-26 | |||
NET Web services vs. remoting: Which one will work best for your app? | |||
Microsoft has made a tremendous amount of noise about building applications with Web services, and its .NET Framework simplifies the task of working with them. But while Web services represent a great way to build applications, they are ideally suited for clients outside the firewall calling components on your server over the Internet.
If both your clients and components are inside the firewall, Web services may work fine; however, all of your data travels through a Web server, which can slow performance. To speed things up, Microsoft provides a binary mechanism called remoting. Let's take a look at how remoting works, and I'll share some code examples that show you how to set it up in a sample Web service. How Web services work with .NET For a brief explanation on how Web services work, and specifically how they work in .NET, check out this sidebar. .NET remoting While Web services are arguably the best way for clients outside of your organization to access components, what about components within your organization? Many companies are building Web services and using them internally. There is nothing wrong with this approach, but it doesn't provide the best performance. If components are created in .NET and the client applications are .NET, you can place components on shared servers and access them via remoting. Remoting is the .NET technology that replaces DCOM allowing you to communicate between a client application and components in a binary format. As a result, remotable components are faster than Web services. However, creating remotable components is more difficult because you must add additional code to your components. This code isn't much more complicated than its Web service counterpart, but you cannot directly instantiate a remote component. Instead, you must create a host application that instantiates the component and listens for requests. The good news is that this host can be a Windows service, a Windows application, a console application, or anything that can run and hold the object open. Not only do you have to create a host application, you must also make several decisions about the remotable object, such as which channel to use. .NET supports both HTTP and TCP channels. The HTTP channel actually uses the SOAP protocol to transport messages to and from remotable objects; this means all messages are serialized to XML. The TCP channel uses a binary stream to transport the messages. Next, you must choose between two activation modes: Singleton and SingleCall. Singleton types have only one instance of an object at any time. All client requests are serviced by that single instance. This allows you to "share" data between requests or, more likely, maintain state between requests. SingleCall types, on the other hand, create a new instance of the object for each client request. SingleCall objects are more like Web services because they are stateless and are created and destroyed for each request. .NET is architected in such a way that remotable components can change channels without being recompiled. You can place the channel information in a configuration file and change from TCP to HTTP or vice versa without recompiling the application. Similarly, you can change a configuration file for the client to match the channel that the host is using. Quick code comparisons Here is the code to implement the same thing with a remotable component: The component looks simpler. In fact, the only difference is that it inherits from System.MarshalByRefObject. But remember, you need to build a host application that instantiates the object and listens for requests. The code for the host object could look like this: In this case, the host application is a console application. You start the application and it launches a console application and creates the object. The console application runs until someone presses the [Enter] key, and the object is available until needed. As you can see, the amount of work required to create the remotable component is more than the Web service. Make the choice But, if you need to allow access to clients other than .NET, you'll need to use Web services regardless of whether or not the client is inside or outside the firewall. In the end, the choice is left to the developer, so you'll need thorough knowledge of both technologies to make a proper decision. |
|||
|
|||
|
|||
|
Web services in .NET | |||
Web services, called XML Web services by Microsoft, are the components that are instantiated via HTTP. Data is passed to, and received from, Web services using HTTP and SOAP, which is clear text and based upon XML. Thanks to Web services, passing data between different operating systems on different platforms has been simplified.
Web services use HTTP to communicate, and this requires a Web server. Requests must be turned into SOAP messages that are sent to the Web server. There, they are processed, the object is instantiated, the method is executed, and the data is placed in an XML format to be returned to the client. The client application processes the XML as necessary.
Given this scenario, it is no great surprise that Web services are slow when compared to direct binary access of objects. Without Web services, data does not have to be serialized to XML and deserialized on the client. Requests and returns do not have to flow through a Web server.
Of course, Web services do offer advantages. First, they can be called by almost any client. The client does not have to be a .NET application. In fact, the client machine does not need to be running a Windows operating system. Second, because Web services use SOAP, the data flows across firewalls easily; all data is passed as XML. Finally, as far as .NET is concerned, the .NET Framework makes it incredibly easy to create Web services, and you can do so with just a few lines of code.
|
|||
|
|||
|
|||
|
2005-7-25 | |||
Building Distributed Apps? Use XML Web Services, Not Remoting (Mostly) | |||
If you have been wondering whether to use XML Web services or .NET Remoting, here's a simple answer: Use XML Web services most of the time. XML Web services are the same as marshal-by-value .NET Remoting, but using them is much easier than using Remoting from scratch. Marshal-by-value remoting (XML Web services) means that you get XML-serialized data from your remote server. Sometimes, you may need dynamic event behavior or you may not want to move massive amounts of data and need a little more control. In this instance, you peel back the XML Web services layer and start building directly on top of .NET Remoting with marshal-by-reference objects and event sinks. Mostly not, though. This article looks at the basic technologies that support XML Web services and provides a brief demonstration of how to produce and consume Web services. XML Web Services Will DoXML Web services in .NET are built on top of .NET Remoting. For all intents and purposes, a Web service is marshal-by-value .NET Remoting. The rumor mill also suggests that .NET Remoting may be completely concealed by XML Web services in the future, making advanced features of Remoting like event sinks available using Web services. I hope you are more comfortable with XML Web services. Now, when you have to choose between Remoting and Web services, you are prepared to make a decision. Most of the time when you create distributed applications, XML Web services will do. |
|||
|
|||
|
|||
|
XML Web services .NET Remoting and System.NET Communication Guidelines | |||
1. An XML Web Service call is essentially a SOAP message sent over the Http Protocol
2. .NET Remoting is a communication infrastructure that provides access to remote objects over the TCP protocol, Http protocol or some other custom protocol 3. XML Web Services uses SOAP (Simple Object Access Protocol) which is an XML message to communicate over the widely used Http protocol. 4. .NET Remoting provides object services over the Http or TCP protocol. .NET Remoting could be a SOAP message over a Http protocol (similar to XML Web service) or a TCP protocol or a Binary Formatted message over the TCP Protocol or Http Protocol. 5. When objects are remoted over a TCP Protocol using Binary Formatting or Encoding you have the best performance over any .NET Remoting Service or XML Web service. 6. If you intend to send SOAP messages over the Http protocol, do not use .NET Remoting, use XML Web services. The medium is the same in both instances and XML Web services deliver more speed than .NET Remoting service using SOAP Formatting over a Http protocol. 7. Using IIS as a Host or server offers scalability to both Remoted objects and XML Web Services. XML Web Services use IIS (Internet Information Server) as a Host and .NET Remoting can use IIS, Windows Services, Windows Form, Console Application as a Host or Server. 8. You can build your own interprocess communication solution using classes from the System.NET namesapce. 9. Using IIS as a host for XML Web services or .NET Remoting service offers security features like authentication and also state management. 10. Binary Formatting over the TCP protocol requires additional ports other that port 80 (which IIS uses) to be opened on your web server. These additional ports could pose security considerations. 11. Using .NET Remoting offers some flexibility for the activation of objects (they can be activated on the server or client), object life time management using leases and object instantiation management (SingleCall or Singleton modes.) 12. .NET Remoting offers access to a richer object oriented programming model than .XML Web services. 13. XML Web services offers a richer set of options for formatting SOAP messages than SOAP Formatting in a .NET Remoting Context. 14. XML Web services uses SOAP to communicate between two computers and is better suited for exchanging messages with applications not built with .NET while .NET Remoting is optimized for exchanging messages between .NET clients. |
|||
|
|||
|
|||
|
设计 .NET 应用程序 | ||
升级到 Microsoft .NET Paul D. Sheriff 出处:Microsoft 摘要:本文概要介绍 .NET 应用程序中的各种典型物理结构之间的区别,这些结构已被证明是很有用的。针对每种结构介绍了其适用方案、实现方式和优缺点。本文同时介绍了两层、三层和 N 层应用程序。
目标
前提条件
目录 两层应用程序结构 两层应用程序结构典型的两层应用程序是使用 ADO.NET 直接与数据库服务器(如 Microsoft SQL Server?)进行通信的客户端应用程序(参见图 1)。除 ADO.NET 外,在客户端应用程序和数据库之间没有任何其他层。有关 ADO.NET 的详细信息,请参阅 .NET 框架文档、本系列的其他文章或使用 MSDN 搜索引擎。 图 1:两层应用程序包括客户端应用程序和数据存储(如 Microsoft SQL Server) 何时使用两层结构两层应用程序适用于没有或只有少量窗体的小型应用程序。对于使用本文中介绍的其他 N 层技术的应用程序,其原型也可算是两层应用程序。但是,两层应用程序不太适用于企业环境,因为开发和维护的时间及成本不好控制。 典型的实现方式开发两层应用程序时可以采用多种技术。所有技术均使用 ADO.NET、一个客户端界面(如桌面或基于 Web 的应用程序)和一个数据库(如 SQL Server)。要使用两层应用程序结构,可以采用以下方式:
优点两层应用程序具有以下优点:
缺点两层应用程序开发方法具有以下缺点:
使用 XML Web Service 的三层应用程序另一种设计方式是使用 XML Web service,将数据库的访问单独分给另一个组件,该组件将把数据返回到前端应用程序。图 2 显示了这种设计方式。 有关使用 XML Web services 开发三层应用程序的详细信息,请使用 MSDN 搜索引擎。
何时使用此技术使用 XML Web service 的三层应用程序适用于基于 Web 的应用程序或 Microsoft Windows? 应用程序。如果需要桌面应用程序的丰富功能,而用户连接自多个不同的位置,并通过 HTTP 界面访问数据,则很适合使用此技术。 典型的实现方式要创建三层/XML Web service 应用程序,通常采用以下开发技术:
优点使用 XML Web service 的三层应用程序具有以下优点:
缺点此设计的缺点与典型的两层应用程序的缺点基本相同,因为它并没有分离业务规则,而只是分离了数据层。此外,表中的列名称也没有被提取到类中(将在下一节中介绍)。
使用 .NET Remoting 的三层应用程序这种类型的应用程序结构与使用 XML Web service 的三层应用程序几乎完全相同。唯一的区别是使用 .NET Remoting 代替 XML Web service 来包装数据访问层。图 3 显示了这种设计方式。 有关 .NET Remoting 的详细信息,请参阅本系列的其他文章或使用 MSDN 搜索引擎。 图 3:在 LAN 环境中,可以使用 .NET Remoting 来包装数据访问层 何时使用三层 .NET Remoting 技术使用 .NET Remoting 的三层应用程序适用于必须在 LAN 中的计算机之间分布的应用程序。这可能是出于业务原因,或者考虑到所涉及的工作成本,采用网络呼叫比较合理。 典型的实现方式要创建这种应用程序,通常采用以下开发技术:
优点使用 .NET Remoting 的三层应用程序与使用 XML Web service 的三层应用程序具有相同的基本优点。
缺点这种三层设计的缺点与使用 XML Web service 的三层设计的缺点相同。
逻辑 N 层应用程序使用 .NET 创建应用程序的最好方法是将所有逻辑进程分为不同的类。在典型的业务应用程序中,这通常包含业务规则组件、数据层组件和使用这些组件的前端代码。图 4 显示了这一方法。 有关设计 N 层应用程序的详细信息,请参阅本系列的其他文章或使用 MSDN 搜索引擎。
何时使用 N 层结构逻辑 N 层开发策略适用于所有类型的应用程序。无论是小型、中 |