花了好多時間建置和測試 C# 的 Serviced Component,測試和建置結果對於企業級的公司有很大的好處,為何呢?要做到3層式(或多層式)架構對企業來說是非常重要的,因為如果企業如果遇到要加新的功能,或 expose 原有的功能到新的 presentation layer (mobile 手機, iPad, 等)那你就一定要把 presentation - business - data , web service etc 等都分開來為不同的 layer,來取得可以改變程式架構的空間,當然複雜度也會增加,但因為不同的部分都會由不同的工程師來負責,所以也可以讓多人同時工作。
市面上 Serviced Component 書很少,不然就是很舊,然後 Microsoft 又一直說要改成 WCF,但根本就一樣概念的東西。。。。只是需要判斷不同的 techonogy 需要在何時情況才用。
好了,不廢話了,直接把結果說出來:
把既有的 business, data component 等 ( C# or vb6 or etc.),放在你已有的地方不要動,多做出一層 ServiceComLayer.cs 把你要 expose的 functions 使用 繼承 ServicedComponent 來 expose 出來給其他的不管是 Delphi, VB6, PHP, classic asp,只要是32-bit window,他都吃。(不用每個 class 繼承 servicedComponent)
我知道你會說,等等,不是要 64-bit 時代了,你還在管 32-bit?
1. 你不可能每次都做新的功能或案子,就不理舊的東西,企業需要延續性
2. 就算是新技術,此確保舊功能(dll 元件)都還可以用,在加上新的功能(技術)時,延伸就好了。因為你的核心已是C# 最新的framework,你只是 expose 給其他技術(或平台)做連接, e.g. PHP也可喔,ASP.NET無敵慢。
3. 連接 third party Component 然後 expose到你現有(32-bit app, vb6 etc) 或新的 C# application都可以。
4. 做成 web service給 application 或網站使用,執行效率太差了~
5. 如何服務 mobile application, 之後我再發表吧。。。。這樣一來,同一套程式,不用寫那麼多次。。。。只要改presentation layer就好了。
如果你覺得沒用,可能你貴公司不是在此企業架構下,就不用想那麼多啦~!
就算是 Wall Street Journal 也還有很多 classic asp (http://chinese.wsj.com/big5/index.asp)頁面,PHP頁面(https://secure.wsj-asia.com/subscription1/index.php),這不是一時三刻改得完,或怎是企業覺得沒有必要去換成新的網頁技術(因為舊的還很好用)。那以上文章可幫助你朝這方向前進。
2013年2月28日 星期四
2012年9月6日 星期四
How to use .Net assemblies in PHP
Quoted from Link
How to use .Net assemblies in PHP
Introduction
For my entry in the WinPHP Challenge I need to use some .Net assemblies I wrote a while ago. It wasn’t clear to me how this can be done. Here’s an example on how to do this. In short: First we create an assembly in visual studio, than we sign it, add it to the Global Assembly Cache or GAC and access it using PHP from there.
Details
Inside visual studio, create a new project. For the purpose of explanation I named the project DotNetTest.
Add the following method to the newly created Class1 class. Make sure the class is declared public.
public class Class1
{
public string SayHello()
{
return "Hello from .NET";
}
}
Now, go ahead an see if this compiles by clicking Build->Build Solution in the menu, or by hitting Ctrl+Alt+B on your keyboard. It may come as no surprise that it does… ;)
To be able to use it thru the GAC the assembly has to be signed. Signing can by done in visual studio. Go to the project properties by right-clicking the project and click Properties all the way at the bottom. Go to the Signing tab. Check theSign the assembly checkbox and select <New…> from the dropdown list beneath that. Give the key file a nice name, like DotNetTestKey and uncheck Protect my key file with a password, because security isn’t an issue in this demo. ClickOk to close the window and finish the signing.
PHP uses COM, even for .NET, we have to make sure the assembly is Com Visible. Go to the Application tab in theProperties windows and click the Assembly Information button. Check the box next to Make assembly COM-Visible and click Ok.
Build the solution again to make sure the assembly we’re about to add to the GAC is signed and configured correctly.
Next, we have to register the assembly in the GAC. The easiest way to do this is thru the command-line. Visual studio provides a short cut to the command prompt by right-clicking on the project in the solution explorer and click Open Command Prompt. Go to the debug folder by typing cd bin/debug. Enter the following command to install the assembly into the GAC:
gacutil -i DotNetTest.dll
The utility should tell you that the assembly as successfully been added to the cache.
Last, take your favorite PHP editor and create a new PHP file with the code below.
<?php
$class1 = new DOTNET("DotNetTest,"
."Version=1.0.0.0,"
."Culture=neutral,"
."PublicKeyToken=????????????????"
,"DotNetTest.Class1");
echo($class1->SayHello());
?>
The DOTNET class instantiates a class from a .Net assembly. The full assembly name must be provided to the constructor at the place of the ‘???’ in the example. This can be found by going to C:\Windows\Assembly using the Windows Explorer or by opening the .dll file in a tool like Reflector.
Place the file at a location accessible thru the browser and go there. If everything went well, it should say “Hello from .Net”
訂閱:
文章 (Atom)