none
How to throw SoapException from ProcessMessage function of SoapExtension class RRS feed

  • Question

  • this way i am trying to throw exception to client side from web service ProcessMessage function but getting this error An exception of type 'System.Web.Services.Protocols.SoapException' occurred in EncryptASMX.dll but was not handled in user code

    here is my ProcessMessage function code. when password mismatch then i like to throw exception to web service client that Unauthorized access.

    public class AuthExtension : SoapExtension
        {
            public override void ProcessMessage(SoapMessage message)
            {
                var AfterDeserialize = "";
                bool flag = false;
                if (message.Stage == SoapMessageStage.AfterDeserialize)
                {
                    AfterDeserialize = "AfterDeserialize";
                    //Check for an AuthHeader containing valid
                    //credentials
                    foreach (SoapHeader header in message.Headers)
                    {
                        if (header is AuthenticateHeader)
                        {
                            AuthenticateHeader credentials = (AuthenticateHeader)header;
                            if (credentials.UserName.ToLower() ==
                                "jeff" &&
                                credentials.Password.ToLower() ==
                                "imbatman")
                                flag = true;
                                //return; // Allow call to execute
                            break;
                        }
                    }
    
                    // Fail the call if we get to here. Either the header
                    // isn't there or it contains invalid credentials.
                }
    
                if (AfterDeserialize == "AfterDeserialize")
                {
                    if (!flag)
                    {
                        throw new SoapException("Unauthorized", SoapException.ClientFaultCode);
                        //return;
                    }
                }
            }
    
            public override Object GetInitializer(Type type)
            {
                return GetType();
            }
    
            public override Object GetInitializer(LogicalMethodInfo info,
                SoapExtensionAttribute attribute)
            {
                return null;
            }
    
            public override void Initialize(Object initializer)
            {
            }
        }

    client side code how i am consuming and calling web service

                ServiceReference1.AuthenticateHeader oAuth = new ServiceReference1.AuthenticateHeader();
                oAuth.UserName = "Test";
                oAuth.Password = "Test";

                ServiceReference1.Test1SoapClient oClient =new ServiceReference1.Test1SoapClient();

                try
                {
                    oClient.Add(oAuth, 2, 3);
                }
                catch(SoapException SoapEx)
                {
                    //string strMsg=SoapEx.
                }
                catch (Exception Ex)
                {
                    
                }

    at client side not being able to access SoapEx or Ex. i like to capture inner exception. so please guide me. thanks

    Wednesday, December 21, 2016 1:18 PM

Answers

  • Hi Mou_inn,

    Here is the project which I made a test.

    Reference:https://1drv.ms/u/s!AsyeWQ2z51P3hBKDL9R-sqIeOdzh

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, December 23, 2016 2:12 AM
  • Hi Mou_inn,

    Sorry for this mistake, and I have updated this link in my above reply.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, December 28, 2016 9:13 PM
    Monday, December 26, 2016 5:04 AM

All replies

  • Hi Mou_inn,

    Do you mean you got error in ProcessMessage method or client side? I made a test with your code, it throws SoapException at client side which could be caught.

    Is there any other code? What is EncryptASMX.dll? I suggest you debug your code in service side to check whether there is any other error.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 22, 2016 7:24 AM
  • basically i am trying to know if i want to throw any exception from ProcessMessage function at service side then how can i capture that exception from client side where i am consuming web service and calling web service method.

    public override void ProcessMessage(SoapMessage message)
    {
    	throw new SoapException("Unauthorized", SoapException.ClientFaultCode);
    }

    EncryptASMX is my web service project name and when we compile this project then EncryptASMX.dll is created in bin directory.

    please post a small but complete code which show me how to capture exception from client side when ProcessMessage function at service side throw any soap exception. thanks

    Thursday, December 22, 2016 8:43 AM
  • Hi Mou_inn,

    Here is the project which I made a test.

    Reference:https://1drv.ms/u/s!AsyeWQ2z51P3hBKDL9R-sqIeOdzh

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, December 23, 2016 2:12 AM
  • wrong link u have shared. i was discussing my asmx and soap extension related things and u shared a project which is all about web api call. u should have checked before shared a link.
    Friday, December 23, 2016 8:16 PM
  • Hi Mou_inn,

    Sorry for this mistake, and I have updated this link in my above reply.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, December 28, 2016 9:13 PM
    Monday, December 26, 2016 5:04 AM