简体   繁体   English

如何从Web服务返回响应

[英]How to return response from webservice

I have made a web service and clients will call its methods. 我已经做了一个Web服务,客户将调用它的方法。 Suppose one method takes four parameters and I want to check that if any one of these is empty I want to send a response back to the client and I want to send it like an alert so client knows what argument is empty. 假设一种方法采用四个参数,并且我想检查一下其中的任何一个是否为空,我想将响应发送回客户端,并且希望像警报一样发送给客户端,以便客户端知道什么参数为空。 I can not force client to give all parameters accurate. 我不能强迫客户端提供所有准确的参数。 Is this possible? 这可能吗?

[WebMethod]
Public void Method1(string str1,string str2,string str3,string str4)
{
    if((str1=="") || (str2==""))
    {
        //send response back to client in form of an alert
    }
}

As far as I know, you can send some agreed-upon object back to the sender, which the sender should be prepared to receive. 据我所知,您可以将一些已同意的对象发送回发送者,发送者应准备好接收它。 It could be as simple as a string that contains "OK" or "ERROR", or it could be complex and have an error code, description, severity level, etc. What the sender does with it when they consume it is completely up to them though. 它可能很简单,例如包含“ OK”或“ ERROR”的字符串,也可能很复杂,并且包含错​​误代码,描述,严重性级别等。发件人在使用它时将如何处理它完全取决于他们虽然。

You could, for instance, make one of the fields in your response a severity level (maybe using something like Microsoft's Severity enum ) , which the sender could then use to determine whether they display an alert to their users or simply log the error somewhere on their system. 例如,您可以将响应中的一个字段设置为严重性级别(可能使用Microsoft的Severity枚举之类的东西) ,然后发送方可以使用该级别来确定他们是向用户显示警报还是仅将错误记录在某处。他们的系统。

You said you can't control the person calling the web method. 您说您无法控制调用Web方法的人。 As the method is of type void, the user will only know if something drastic happens. 由于该方法的类型为void,因此用户仅会知道是否发生了剧烈变化。 How about throwing an exception, and the error will propagate, to what detail it depends on the settings of the web server hosting the web service and the web server hosting the client (if the client is a web page). 如何引发异常以及错误将传播到什么细节,取决于托管Web服务的Web服务器和托管客户端(如果客户端是网页)的Web服务器的设置。

throw new Exception("str1 or str2 in Method1 is empty");

In the "best" case, the user will see the error message "str1 or str2 in Method1 is empty. In the "worst" case, the user will get the standard IIS/ASP.NET error page. Like you said you have no control over the caller, it depends on how the caller codes the client and how he deploys the application. But whatever the case, it gets his attention! 在“最佳”情况下,用户将看到错误消息“ Method1中的str1或str2为空。在“最差”情况下,用户将获得标准的IIS / ASP.NET错误页面。就像您说的那样,您没有对呼叫者的控制取决于呼叫者如何编码客户端以及他如何部署应用程序,但是无论如何,它都会引起他的注意!

I put best and worst in quotes because best and worst mean different things to different people. 我将最佳和最差用引号引起来,因为最佳和最差对不同的人意味着不同的事物。 For people who want maximum security, best is when the error page says nothing about the error. 对于需要最大安全性的人们,最好的方法是在错误页面上没有显示任何有关错误的信息。 For people who want user friendliness and fast troubleshooting, best is when the error message has a lot of details and what to do next. 对于需要用户友好和快速故障排除的人,最好的方法是当错误消息包含很多详细信息以及下一步做什么时。

Don't reinvent the wheel with custom objects. 不要使用自定义对象重塑轮子。 Look at this MSDN article . 看看这篇MSDN文章

The easiest thing is just to throw an exception (sounds like an ArgumentException is appropriate in your case) and the client will receive a SoapException . 最简单的方法就是抛出一个异常(在您的情况下,类似ArgumentException类的声音是合适的),并且客户端将收到SoapException

Or throw a SoapException yourself if you want more control, such as the ability to set a fault code. 如果需要更多控制,例如设置错误代码的能力,也SoapException自己抛出SoapException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM