'IHttpModule'에 해당되는 글 1건
- 2009.06.10 웹 서비스 Exception 공통 처리
IHttpModule을 이용하여 공통 처리하는 것이 좋겠지만
웹 서비스의 경우에는 Exception을 "html/xml"으로 변경 처리하기 때문에
여기의 Error에서 Exception을 확인할 수 없습니다.
public class ExceptionManager : IHttpModule
{
public void Init(HttpApplication webApp)
{
webApp.Error += new EventHandler(webApp_Error);
}
protected void webApp_Error(object sender, EventArgs eventArgs)
{
HttpApplication webApp = (HttpApplication)sender;
Exception e = webApp.Server.GetLastError().GetBaseException();
// 예외 처리
}
public void Dispose()
{
}
}
그래서 SoapExtension을 확장하여 Exception 공통 처리를 하도록 변경하여야 합니다.
public class ExceptionExtension : SoapExtension
{
public override object GetInitializer(Type serviceType)
{
return null;
}
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return null;
}
public override void Initialize(object initializer)
{
}
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
SoapException ex = message.Exception as SoapException;
if (ex != null)
{
// 예외 처리
}
break;
}
}
}
SoapExtension을 상속받아 추상화 부분을 작성하면 됩니다.
그 중 ProcessMessage의 message.Stage가 SoapMessageStage.BeforeSerialize이거나
SoapMessageStage.AfterSerialize이면 웹 서비스에서 발생한 Exception을 확인할 수 있습니다.
두 가지 중 BeforeSerialize을 이용한 것은 이곳에서는 Exception을 변경하여도 되지만
AfterSerialize에서는 Exception을 변경하면 안되기 때문입니다.
HttpModule의 등록장소는 Web.Config 파일의 다음과 같지만
<configuration>
<system.web>
<httpModules>
<add name="ExceptionManager" type="Darkin.ExceptionManager, Darkin" />
</<httpModules>
</system.web>
<!-- IIS 7.0
<system.webServer>
<modules>
<add name="ExceptionManager" type="Darkin.ExceptionManager, Darkin" />
</modules>
</system.webServer>
-->
</configuration>
SoapExtension의 경우에는 다음과 같습니다.
<configuration>
<system.web>
<webServices>
<soapExtensionTypes>
<add type="Darkin.ExceptionExtension, Darkin" priority="1" group="High" />
</soapExtensionTypes>
</webServices>
</system.web>
</configuration>

