WebDev.WebServer.exe

朋友傳給我幾個 ASP.Net 網頁,是用 3.0/3.5 寫的,懶得用 VWD 2008 開起來看,所以就偷懶用 WebDev.WebServer.exe 來跑,可是卻無法跑起來,會有找不到 System.Linq 的錯誤。原本以為 3.0/3.5 有另一個 WebDev.WebServer.exe,但卻遍尋不著,最後還是用 VWD 2008 開了,當然也能正常執行了。不過我就好奇了,所以用 ProcessExplorer 查了一下,發現我沒錯,的確是用 WebDev.WebServer.exe 執行。再仔細想了一下,才想到,可能是 web.config 裡有鬼,朋友傳給我的檔案裡沒有 web.config,直接執行 WebDev.WebServer.exe ,會使用 .Net 2.0 預設的 web.config。但用 VWD 2008 開過以後,補上了 web.config,裡面有這麼幾行:

<system.web>
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</system.web>

一切真相大白,原來是補上了 3.0/3.5 所需的 Assembly,讓 WebDev.WebServer.exe 在跑 3.0/3.5 的 ASP.Net 網頁時沒有問題。

Mono rpms for CentOS/RHEL 4.x

雖然Mono官方不再提供 CentOS/RHEL 4.x 的 rpm 了,你還是可以自己重新 build。
在我看過 spec 檔案以後,我發現 Mono 開發團隊已經有加上對 Linux 發行套件的判斷,也就是說其實你可以直接下載 source rpm,安裝必要的函式庫之後,執行 rpmbuild –rebuild xxx.src.rpm 來 build 出 rpm。
source rpms 放在 這裡

如何在 .Net 調用Yahoo!搜索 斷章取義 API

無心工作,剛好又看到有人問怎麼用,所以就牛刀小試一下。基本上用 WebClient 就可以搞定:

[csharp]
//
// Yahoo!搜尋『斷章取義』API http://tw.developer.yahoo.com/cas/
// Yahoo!搜尋『斷章取義』API 技術文件 http://tw.developer.yahoo.com/cas/api.php
//
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Net;
namespace Yahoo {
public class CAS {
private string _GetString( byte[] bytes ) {
return Encoding.UTF8.GetString(bytes);
}
private WebClient GetWebClient() {
WebClient client = new WebClient();
// client.Proxy = new WebProxy(“localhost”, 8000);
client.Encoding = Encoding.UTF8;
return client;
}
private NameValueCollection GetParameters() {
NameValueCollection data = new NameValueCollection();
// TODO: Place your appid here.
data.Add(“appid”, “APgdNPnV34E7WQlhpBYaQvaRWPjwvd8exe094Q_r_7GWEOBFh9UDQY6vqNgZVwhc”);
return data;
}
public string WordSegmentation(string content) {
NameValueCollection data = GetParameters();
data.Add(“content”, content);
WebClient client = GetWebClient();
byte[] responseArray = client.UploadValues(“http://asia.search.yahooapis.com/cas/v1/ws”, “POST”, data);
return _GetString(responseArray);
}
public string Authenticate() {
NameValueCollection data = GetParameters();
WebClient client = GetWebClient();
byte[] responseArray = client.UploadValues(“http://asia.search.yahooapis.com/cas/v1/AuthBootUp.php”, “POST”, data);
return _GetString(responseArray);
}
public string KeywordExtraction( string content, int threshold, int maxnum ) {
NameValueCollection data = GetParameters();
data.Add(“content”, content);
data.Add(“threshold”, threshold.ToString());
data.Add(“maxnum”, maxnum.ToString());
WebClient client = GetWebClient();
byte[] responseArray = client.UploadValues(“http://asia.search.yahooapis.com/cas/v1/ke”, “POST”, data);
return _GetString(responseArray);
}
public static void Main() {
Yahoo.CAS cas = new Yahoo.CAS();
// 如果你重新申請 appid 的話,要先作 Authenticate
// Console.WriteLine( cas.Authenticate() );
Console.WriteLine( cas.WordSegmentation( “your_text” ));
Console.WriteLine( cas.KeywordExtraction( “your_text”, 30, 10));
// 得到的字串是 XML,要轉為 DataSet 的話,可以這樣作
/*
string text = cas.WordSegmentation( “your_text” );
TextReader stringReader = new StringReader(text);
DataSet dataSet1 = new DataSet();
dataSet1.ReadXml(stringReader);
// 轉好以後應會有兩個 DataTable,資料都在第二個 Table 裡,也就是 dataSet1.Tables[1]
*/
}
}
}
[/csharp]

列出 log4net 設定裡所有的 repository 跟 appender

Debug 用的,主要是看自己的 log4net 設定對不對。

StringBuilder sb = new StringBuilder();
ILoggerRepository[] repos = LogManager.GetAllRepositories();
foreach( ILoggerRepository repo in repos )
{
sb.AppendLine( "=====" );
sb.AppendLine( string.Format( "{0} - configured={1}", repo.Name, repo.Configured.ToString() );
sb.AppendLine( "Appenders:" );
foreach( IAppender appender in repo.GetAppenders() )
{
sb.AppendLine( string.Format( "\t{0}", appender.Name ) );
}
}
sb.AppendLine( "=====" );
Console.WriteLine( sb.ToString() );

debuan/ubuntu nant-0.85 的 SMP bug

最近在 Ubuntu 下用 NAnt 時,有 50% 的機率會遇到類似這樣的錯誤:

The current runtime framework 'mono-2.0' is not correctly configured in the NAnt configuration file.
Function call failed.
Expression: ${path::combine(prefix, 'lib/mono/1.0')}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Illegal characters in path
Parameter name: path1

找了好久,都不知道原因所在,因為有時候是正常執行的。去Google試了好幾個關鍵字,都找不到解。今天用 nant “Illegal characters in path” 去找,終於找到了:#483073 – nant: race condition on SMP systems – Debian Bug report logs
裡面提供的暫時解法是利用環境變數 MONO_NO_SMP,把 SMP 關掉,再執行即可。所以只要編輯 /usr/bin/nant,在 exec 之前加上 export MONO_NO_SMP=1 即可。

最近用 MySql Connector/Net 的幾個心得

  1. 連線字串加上 charset=utf8,如此一來,完全不用更改 MySQL 伺服器設定,只要確定建 database/table 時有指定 utf-8 編碼即可。
  2. SQL 參數在使用 @ 時,若碰到問題,不妨改用 ? 試試。我的確碰到這問題,它卡了我兩、三天,而且改用 ? 也解決了。忘了在哪兒看到,剛好有提到這點,真的是幸好我有看到…
  3. MySQL 有提供 MySqlHelper 類別,省掉寫 Helper 的麻煩…
  4. MySQLConnector/Net下載網站上沒有給 Linux 的版本,事實上,是通用的,你可以直接拿給Mono用,不需要作任何改動。

.NET framework essential Chapter 7/8-ASP.Net/Windows form

各自只有一個章節,不過作者很簡明扼要地把該介紹的東西都介紹了。
ASP.Net 除了講 HtmlControl、WebControl 以外,還介紹了 Directive、Session 設定等等。
Windows form 則是簡單的介紹如何開始、Layout 的配置,以及比較容易讓人混淆的 MDI Form。
這本書真的很不錯,講的都很基本,看完對 .NET 會有一定程度的了解,不至於在茫茫大海似的類別庫裡淹死。

App_Offline.htm

今天 Trace Mono System.Web.HttpRuntime 時看到的,如果在你 ASP.Net 2.0 網站目錄下放置一個 app_offline.htm 時,不管你瀏覽什麼網頁,都只會看到 app_offline.htm 的內容。
詳情可以參考 ScottGu 大 的文章:App_Offline.htm
很鳥的是,.NET Documentation 裡完全沒提到這個。

Mono 與音訊、視訊

mplayer 的 -input 可以指定 file,man 裡面說明了你可以給一個 FIFO 的檔案。
藉著這個,於是就可以寫程式來控制 mplayer 來播放影片或是音樂。
你可以參考致遠管理學院資工系專題研究計畫研究成果報告計畫: Linux C# 設計 …
你還可以使用 GStreamer#Mono team 的人已經幫你將 gstreamer 函式庫包裝為 GStreamer#,因此使用 GStreamer# 也可以進行播放影片和音樂,最好的例子是 Banshee
不過這只適用於 Linux。

.NET framework essential Chapter 6-Web Services

最前面是一堆有點誇張的介紹,不過在當時大家真的都以為 Web Services 很棒…
這一章介紹 Web Services 的觀念與實作,由於微軟是規格制定者,所以 .NET 對 Web Services 的支援不能少,下了很多功夫。
主角是 WebService 類別與 WebMethodAttribute,方法是繼承 WebService 類別,然後在要被呼叫的方法上,標上 WebMethod attribute。
以前以為 Web Services 的底層是 SOAP,所以只能用 SOAP,沒想到也可以用 GET/POST 的方式來呼叫,所以 javascript 也能輕易地呼叫 Web Services,然後解析回傳的 XML 即可。
Security 方面,以前也以為沒有 session,現在也知道可以用了。另外還介紹了兩種層級的保護,系統層級是利用 IIS 管理介面設定權限與 SSL 來達成,Application Level,則是利用 .NET 的 web.config 來達成。
SSL 對於 Web Service 來說,是個稍嫌沈重的負擔,所以他也建議使用 authentication token 的方式,也就是提供一個架構在 SSL 上,可以取得 authentication token 的方法,取得 token 之後,後續的呼叫都要帶入這個 token,以作為識別。
這樣的方式,在目前有提供 Open API 的網站上很常見。