[.NET] PHP 整合.NET元件 / PHP Integrate .NET of Assembly

  • 3676
  • 0
  • 2012-01-10

[.NET] PHP 整合.NET元件 / PHP Integrate .NET of Assembly

.NET開發條件

為了讓整個案子能迅速的結案,協同開發是有必要性的,所以與PHP整合的需求就應運而生,.NET元件要能給PHP整合要有以下要件

  • 全域物件

image

  • Make assembly COM-Visible要勾選

image

  • 方法名稱不得重覆
  • 建構子不能帶參數

參考來源:http://geekswithblogs.net/tkokke/archive/2009/04/24/how-to-use-.net-assemblies-in-php.aspx

當然,PHP 要有.NET的環境,我測試的環境是Windows 2008 / Apache,還有還有,PHP線上手冊也是很重要的http://us2.php.net/manual/en/class.dotnet.php


PHP需要什麼?

  • Public Key Token
  • Version

我們可以用sn.exe、Reflector或是VSCommand來取得Public Key Token

用sn –T 取Token那Version呢?有了VSCommand我就不想知道了,我承認我很懶^^

image

用Relfector

image

Demo大濕提供的好元件:VSCommand

image


.NET元件的部署

.NET 2.0:GAC的路徑是C:\Windows\assembly

.NET2.0~3.5都適用,把元件直接拖到這路徑

image

PS.在MSDN上寫著.NET4.0已經移除這個工具

image

.NET4.0:經過gacutil.exe註冊過後,會把檔案放到C:\Windows\Microsoft.NET\Assembly裡面

  • gacutil.exe需要由Visual Studio Command Prompt呼叫,若沒有Visual Studio Command Prompt,則用一般的Command Prompt,找到gacutil.exe路徑再透過Command Prompt呼叫gacutil.exe
  • 若找不到gacutil.exe,就必須要從Microsoft Windows SDK for Windows 7 and .NET Framework 4裡安裝

由Visual Studio Command Prompt執行gacutil.exe註冊成功畫面

image

由Command Prompt執行gacutil.exe

image

註冊成功後會在C:\Windows\Microsoft.NET\assembly\GAC_MSIL裡出現該元件

image

註冊完後,我在VS開發工具裡沒辦法看到我註冊的元件(知道的人請告訴我一下),不過把元件copy到C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0目錄底下就可以讓VS看到,不過這感覺很怪,應該是註冊後就能在VS裡使用了,不應該是把元件發佈在兩個位置,而且放在這個位置的元件不需要是強型別

image


MS套件

MS有出一套可以參考GAC的套件以及GAC的Viewer,應該可以讓我們在處理GAC的元件方便一些。

GAC Reference

image

GAC Viewer

image

PS.最後,PHP疑似看不懂.NET4.0,只看的懂2.0~3.5,我猜它只認得C:\Windows\assembly裡面的東西:https://bugs.php.net/bug.php?id=55847,最後宣告放棄,改用Web Service與PHP整合

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo