Skip to main content

How to handle exception in .net web services client when use ws-secureconversation!!!

No replies
sgboy
Offline
Joined: 2011-09-23
Points: 0

Hi everyone,

I'm testing a simple web services and deploy it with Websphere Application Server 7.0. The web services just has simple methods like this.

@WebService
public class MyServices

{

public String sayHello1()
{

return "Hello From Server";
}

public void sayHello2() throws MyException
{

MyFault faultBean = new MyFault();
faultBean2.setMessage("This is a test exception 1");
throw new MyException("This is a test exception 2", faultBean);
}

}

And the MyException and MyFault classes as below.

@WebFault
public class MyException extends Exception
{
private static final long serialVersionUID = 1L;

private MyFault faultInfo;

public MyException(String errMsg, MyFault faultInfo)
{
super(errMsg);
this.faultInfo = faultInfo;
}

public MyException(String errMsg, MyFault faultInfo, Throwable cause)
{
super(errMsg, cause);
this.faultInfo = faultInfo;
}

public MyFault getFaultInfo()
{
return faultInfo;
}
}

public class MyFault
{
private String errMsg;

public String getMessage()
{
return errMsg;
}

public void setMessage(String errMsg)
{
this.errMsg = errMsg;
}
}

I use a .NET client to call the webserices like this

public partial class MyWSNETClient
{

static void Main(string[] args)
{
MyServicesPortTypeClient server = new MyServicesPortTypeClient("MyServicePort");

try
{

Console.WriteLine("================Begin call server.Hello1()======================");
Console.WriteLine(server.Hello1());
Console.WriteLine("================End call server.Hello1()======================");

Console.WriteLine("================Begin call server.Hello2()======================");
server.Hello2();
Console.WriteLine("================Begin call server.Hello2()======================");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
}
}
}

When I run the client, the output is as my expectation like below.

================Begin call server.Hello1()======================
Hello From Server
================End call server.Hello1()======================

================Begin call server.Hello2()======================

This is a test exception 2

Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, Ti
meSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
...................................

Then I apply the ws-secureconversation and security at message level (use x509 at both sides for signing and encryption) and run the client again. And this time the output is NOT as my expectation, the client can't catch the exact exception that server raised in the sayHello2() call. Look at the result below.

================Begin call server.Hello1()======================
Hello From Server
================End call server.Hello1()======================

================Begin call server.Hello2()======================

Security processor was unable to find a security header in the message. This might be because the message is an unsecured fault or becaus
e there is a binding mismatch between the communicating parties. This can occur if the service is configured for security and the client is not usin
g security.

Server stack trace:
at System.ServiceModel.Security.SecurityStandardsManager.CreateReceiveSecurityHeader(Message message, String actor, SecurityAlgorithmSuite algorith
mSuite, MessageDirection direction)
at System.ServiceModel.Security.MessageSecurityProtocol.CreateSecurityHeader(Message message, String actor, MessageDirection transferDirection, Sec
urityStandardsManager standardsManager)
at System.ServiceModel.Security.MessageSecurityProtocol.ConfigureReceiveSecurityHeader(Message message, String actor, SecurityProtocolCorrelationSt
ate[] correlationStates, SecurityStandardsManager standardsManager, IList`1& supportingAuthenticators)
at System.ServiceModel.Security.InitiatorSessionSymmetricMessageSecurityProtocol.VerifyIncomingMessageCore(Message& message, String actor, TimeSpan
timeout, SecurityProtocolCorrelationState[] correlationStates)
at System.ServiceModel.Security.MessageSecurityProtocol.VerifyIncomingMessage(Message& message, TimeSpan timeout, SecurityProtocolCorrelationState[
] correlationStates)
at System.ServiceModel.Security.SecuritySessionClientSettings`1.ClientSecuritySessionChannel.ProcessIncomingMessage(Message message, TimeSpan timeo
ut, SecurityProtocolCorrelationState correlationState, MessageFault& protocolFault)
at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.ProcessReply(Message reply, TimeSpan timeout, Securit
yProtocolCorrelationState correlationState)
at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, Ti
meSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
..........................................

It seems that when ws-secureconversation is applied, the .NET client can't handle the exception from server like normal way. I think there maybe a different way to handle exceptions between client and webservices when the ws-secureconversation is applied.

Hopefully some of you can give me the advice or solution about this issue. thanks so much!!!