ASMX Web 服务:如何从我喜欢从客户端捕获的 asmx 服务中抛出 soap 异常
ASMX Web Service: How to throw soap exception from asmx service which i like to capture from client side
我想如果客户端发送了错误的凭据然后服务抛出 soap 异常但我尝试了但仍然没有运气。
从这里查看我更新的代码 https://github.com/karlosRivera/EncryptDecryptASMX
任何人都可以在他们的 PC 上下载我的代码和 运行 来捕获问题。
看看这个区域
[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;
}
对于这一行throw new SoapException("Unauthorized", SoapException.ClientFaultCode);
响应 XML 正文未发生变化,我从 soapextension 进程消息函数中看到了这一点。
所以我现在有两个问题
1) 我想要 throw SoapException
来自需要更改 soap 响应的服务。
2) 从客户端我需要捕获 SoapException
请从 link 查看我的最新代码并告诉我要更改的内容。谢谢
考虑迁移并使用 WCF。这是 WCF 错误。
您可以使用SoapExtensions.AfterSerialize
或BeforeSerialze
方法替换错误消息或处理错误。
另一种选择是避免发送 SoapException
s,但更复杂的对象嵌入了错误语义。例如
[Serializable]
class Result<T>
{
public bool IsError { get; set; }
public string ErrorMessage { get; set; }
public T Value { get; set; }
}
在这种情况下,您的方法可能如下所示:
[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
可以开发为包含 field/input 个错误数组,以 return 与不正确的输入参数值相关的更精确的错误消息。
我想如果客户端发送了错误的凭据然后服务抛出 soap 异常但我尝试了但仍然没有运气。
从这里查看我更新的代码 https://github.com/karlosRivera/EncryptDecryptASMX
任何人都可以在他们的 PC 上下载我的代码和 运行 来捕获问题。
看看这个区域
[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;
}
对于这一行throw new SoapException("Unauthorized", SoapException.ClientFaultCode);
响应 XML 正文未发生变化,我从 soapextension 进程消息函数中看到了这一点。
所以我现在有两个问题
1) 我想要 throw SoapException
来自需要更改 soap 响应的服务。
2) 从客户端我需要捕获 SoapException
请从 link 查看我的最新代码并告诉我要更改的内容。谢谢
考虑迁移并使用 WCF。这是 WCF 错误。
您可以使用SoapExtensions.AfterSerialize
或BeforeSerialze
方法替换错误消息或处理错误。
另一种选择是避免发送 SoapException
s,但更复杂的对象嵌入了错误语义。例如
[Serializable]
class Result<T>
{
public bool IsError { get; set; }
public string ErrorMessage { get; set; }
public T Value { get; set; }
}
在这种情况下,您的方法可能如下所示:
[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
可以开发为包含 field/input 个错误数组,以 return 与不正确的输入参数值相关的更精确的错误消息。