2012年7月17日 星期二

何時應該使用DCOM?


何時應該使用DCOM?


不能集中精神、心無旁騖、遵守紀律的生命,永遠成不了大器。
亨利 愛默生 霍士德

很多時候我們被仿間的書籍教導成我們可以直接寫好網頁,何必要要使用 DCOM 來多此一舉?況且 DCOM 又很難去架構和設定但他還有些無法被取代的好處:

  • 高流量時,可以大大增加performance
    COM 在 loading  因為它是分散式的系統,他可以將\讓不同的電腦做他自己專門的工作,比如Web Server , Application Server, SQL Server 各自可以做好自己的工作,如放自同一台電腦上,很容易會有資源衝突的情況發生,而且很難去查決問題出在哪。
  • COM Servers 會需要時自行啟動
    我知道這聽起來沒甚麼,但如果要做到電子商務流程順暢,所需要的資源必須自動啟動,不需要的資源他自己會不運行,來達到"節能減碳"。
  • 管理者控制權限
    COM servers 可以在主機上管理,也可以改變有幾個 INSTANCES 在執行。安裝時也不需要整個網站關起來很久(除非你搞砸了)。另外聽說也可使用 Web Connection swap executable files 來更新,改天可以來試試看如何操作。
  • Crash Protection
    物件管理員可以替你管理和設定你的元件,細節可參考MSDN。Pooling 和 Recycle 功能可以特別看一下。

壞處是一開始很難安裝,而且速度比in-process COM慢,但安全性和擴充性都比較好。


2012年7月14日 星期六

Regasm and Regsvcs

Below article is quoted from Link

Q: I have following question : Is it possible that .NET assembly will be regsitered both with Regasm and Regsvcs?

A:
In order to understand this, you'd have to conjure up the differences between a COM and a COM+ Application.
Register your types as COM - Creates object on demand everytime a code tries to initialize the object
Register your types as COM+ application - Creates object, supports object pooling, supports transactions, supports enhanced Windows security and more.
To understand pooling, I am borrowing a response from http://www.tek-tips.com/viewthread.cfm?qid=116249
COM+'s main method of adding scalability and performance is done through object pooling. Unfortunately, this requires a free-threaded component, which is something VB6 cannot do. But... .NET (any language) and C++ can.
What object pooling does is you tell MTS/COM+ to create a pool of available objects, from a minimum that gets created when COM+ starts, to some maximum (which I don't know if it's a hard maximum, or if it's flexible). What pooling does for you is provide a caller with a pre-initialized object. This is much faster than waiting for an object to be created (especially over a network). The caller connects to the object, makes method calls, and disconnects. The object then goes back into the pool.
It does require a fundamental change in program architecture. Before COM+, everyone would open a connection to a database and leave it open for the duration of the application. This was OK when the user population was <100, as the load on the server was managable (each connection takes up RAM). But for large populations or unknown quantities of users (e.g. users from the Internet), the database server gets overloaded quickly. Someone realized that each transactional user was actually doing real work a small percentage of the time -- the rest of the time they were idle.
So your programs must make a connection, make a request, get the results, and then disconnect (this also applies to non-database objects). This also implies that the application be stateless (program state is not maintained between requests). Because... the object that you're currently using belonged to someone else 200 milliseconds ago. And when you get through using the object, another user will be using it after you. So the objects cannot keep any information around -- they must be code only.
regasm - Registers a .net assembly types as COM. What this means is the regasm picks the publicly exposed types of your .Net assembly and then writes appropriate registry entries at HKCR....; (which is the way regsvr32 works).
  • regasm can generate a tlb file for you (just like tlbexp.exe)
  • regasm can codebase your .Net assembly. That is if you dont have your .Net assembly in the GAC, then you can codebase the COM to the file location where the .Net assembly is present. And from here the COM Marshaller will pick the execute the assembly with CLR.
  • regasm allows you to create a .reg file by double clicking which you can update the registry entries. you have to make sure the .Net assembly is installed to the GAC becase /regfile switch in regasm does not allow you to /codebase (if it does codebase then it is meaningless)
regsvcs - Creates a COM+ Application from a .Net assembly. What this means is the regsvcs picks the publicly exposed types of your .Net assembly and besides writing the appropriate registry entries, it also creates a COM+ application that you can manage via the Componet Services Manager Console (%systemroot%\system32\comexp.msc).
  • regsvcs creates a COM+ application based on the information passed to it from the command line, or based on the information avaialble from the .Net dll.
  • regsvcs allows you to merge your COM+ types to an existing COM+ application. Take a look at comexp.msc to traverse through to understand a COM+ application and and COmponets that a COM+ manages.
If you write a C# class with ComVisible(true) --> The public types of this class (Foo) is ready to be registered with a regasm for COM.
    // Set the COM visibility attribute to true
[ComVisibleAttribute(true)]
public class Foo{....}
If you write a C# class with ComVisible(true), inheriting from System.EnterpriseServices.ServicedComponent (and more setting of course..) --> This class (FooBar) is ready to be registered as a COM+ application.
    // Set the COM visibility attribute to true
[ComVisibleAttribute(true)]
  public class FooBar: System.EnterpriseServices.ServicedComponent{.....}
Creating a COM+ application from .Net - You can begin here. Remember COM+ provides advanced transaaction mangement for a COM exposed object.