2012-01-24 46 views
2

我有一個ASMX Web服務的一些網站的方法,目前是這樣的:添加自定義屬性到ASMX Web服務方法

[WebMethod(false, System.EnterpriseServices.TransactionOption.NotSupported)] 
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)] 
public XElement GetSomeData(int dataId) 
{ 
    // Do something. 
} 

我希望能夠做這樣的事情:

[WebMethod(false, System.EnterpriseServices.TransactionOption.NotSupported)] 
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)] 
[EnableSomeCustomSecurityCheck(true)] 
public XElement GetSomeData(int dataId) 
{ 
    // Do something. 
} 

其中「EnableSomeCustomSecurityCheck」告訴它應該有一個額外的令牌參數需要驗證。我基本上希望避免將此代碼複製到需要它的每種方法:

[WebMethod(false, System.EnterpriseServices.TransactionOption.NotSupported)] 
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)] 
public XElement GetSomeData(int dataId, string securityToken) 
{ 
    SomeClass.CheckSecurityToken(securityToken); 

    // Do something. 
} 

我對於從哪裏開始有點失落。任何人都可以點我正確的方向,我可以如何添加此功能而不會丟失asmx處理已有的任何功能?

+0

向每種方法添加一行代碼與爲每種方法添加一行屬性之間有什麼區別?你試圖達到什麼樣的好處? – pmartin

+0

使用WCF你會更容易。 –

+0

@pmartin:因爲我不相信它描述了該方法本身需要如何工作,而是該框架應該如何反應。 – Ocelot20

回答

1

您可以將該屬性放在該方法上,然後讓Soap Extension檢查該屬性並相應地執行操作。