0

I want if client send wrong credentials then service throw soap exception but I tried but still no luck.

see my updated code from here https://github.com/karlosRivera/EncryptDecryptASMX

Anyone can download my code and run in their PC to capture the problem.

see this area

[AuthExtension]
[SoapHeader("CredentialsAuth", Required = true)]
[WebMethod]
public string Add(int x, int y)
{
    string strValue = "";
    if (CredentialsAuth.UserName == "Test" && CredentialsAuth.Password == "Test")
    {
        strValue = (x + y).ToString();
    }
    else
    {
        throw new SoapException("Unauthorized", SoapException.ClientFaultCode);

    }
    return strValue;
}

For this line throw new SoapException("Unauthorized", SoapException.ClientFaultCode);

The response XML body is not getting change which I have seen from my soapextension process message function.

So I have two issues now

1) I want throw SoapException from service for which soap response need to be changed.

2) From client side I need to catch the SoapException

Please view my latest code from the link and tell me what to change. thanks

Rajput
  • 2,597
  • 16
  • 29
Mou
  • 15,673
  • 43
  • 156
  • 275

2 Answers2

2

Consider migrate and using WCF. It was WCF Faults.

You can replace the error messages or handle erros using SoapExtensions.AfterSerialize or BeforeSerialze methods.

Source

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
Paulo Marques
  • 119
  • 1
  • 5
  • would u post any sample code to return error message to client using `soapExtensions.AfterSerialize or BeforeSerialze methods. ` please come up with solution. thanks and happy new year :) – Mou Jan 01 '17 at 11:28
  • Here: `public override void ProcessMessage(SoapMessage message) { if (message.Exception != null && message.Stage == SoapMessageStage.BeforeSerialize) { if (message.Action.EndsWith("YourAction", StringComparison.InvariantCultureIgnoreCase) && message.Exception.InnerException is InvalidOperationException) { if (message.Exception.InnerException.InnerException is FormatException) { var ne = new SoapException("Custom Error Message", message.Exception.Code, message.Exception.InnerException); message.Exception = ne; } }` – Paulo Marques Jan 30 '17 at 17:53
0

Another option is to avoid sending SoapExceptions, but more complex objects that embed error semantics. E.g.

[Serializable]
class Result<T>
{
    public bool IsError { get; set; }
    public string ErrorMessage { get; set; }

    public T Value { get; set; }
}

In this case your method could look like this:

[AuthExtension]
[SoapHeader("CredentialsAuth", Required = true)]
[WebMethod]
public Result<string> Add(int x, int y)
{
    string strValue = "";
    if (CredentialsAuth.UserName == "Test" && CredentialsAuth.Password == "Test")
    {
        return new Result<string> { Value = (x + y).ToString() };
    }
    else
    {
        return new Result<string> { IsError = true, ErrorMessage = $"Unauthorized -  {SoapException.ClientFaultCode}" };
    }
}

Result can be developed to include an array of field/input errors to return more precise error messages related to incorrect input parameter values.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • Why u use Result and sending and Result { Value = (x + y).ToString() }; can't we write code like return new Result { Value = (x + y).ToString() }; ? – Mou Jan 01 '17 at 11:25
  • happy new year :) – Mou Jan 01 '17 at 11:28
  • No, the type must be provided in this context. If `Result ` had a constructor taking a T parameter (`Result(T initialValue)`), your simplification would have been possible through type inference. – Alexei - check Codidact Jan 01 '17 at 11:34
  • sorry this is not clear why Result class has to be generic type ? – Mou Jan 01 '17 at 18:54
  • u said `your simplification would have been possible through type inference.` would u show me how simplification can be possible ? would u post some sample code if u can manage time. thanks – Mou Jan 01 '17 at 18:56
  • 1
    Generics allow to work with actual types, thus the advantage of compile time errors instead of run-time ones in some cases. I have just tested and constructors do not infer type (my bad). This is fully explained by Eric Lippert [here](http://stackoverflow.com/questions/3570167/why-cant-the-c-sharp-constructor-infer-type). – Alexei - check Codidact Jan 01 '17 at 19:04
  • is it not possible to work without generic for returning soap exception? – Mou Jan 01 '17 at 19:13
  • Yes, just cut the generic and use either `string Value` or `object Value` if this is supposed to work for other types, as well. – Alexei - check Codidact Jan 01 '17 at 19:14
  • if i cut the generic then result class would look like `[Serializable] class Result { public bool IsError { get; set; } public string ErrorMessage { get; set; } public T Value { get; set; } }` so return type will be as below `return new Result { IsError = true, ErrorMessage = $"Unauthorized - {SoapException.ClientFaultCode}" };` does the above code will work if we cut the generic. let me know your suggestion. thanks – Mou Jan 05 '17 at 12:51
  • if i miss understand what u try to say then please explain your point in more details. – Mou Jan 05 '17 at 12:52
  • It should work. You should have `string Value` instead of `T Value` in this case. – Alexei - check Codidact Jan 05 '17 at 12:53