I've been working on some web services lately in c# asp.net (3.5).
我最近在c#asp.net(3.5)上一直在研究一些Web服务。
My method is like so and returns a User object consisting of some basic user-related fields (name, age, i etc)..
我的方法是这样的,并返回一个User对象,包含一些基本的用户相关字段(名称,年龄,我等)。
[WebMethod, SoapHeader("AuthHeader")]
public user[] Employees(int count)
{
user[] myUsers = new user[count];
<logic here inc. checking if user is authorised>...
return myUsers;
}
If authorisation fails for the client consuming the web service id like to return an error within the web service, correctly formatted.
如果使用Web服务ID的客户端的授权失败,则在Web服务中返回错误,格式正确。
Whats the best practice way to achieve this? I guess simply pushing a Response.StatusCode or a Null return isnt good practice?
什么是实现这一目标的最佳实践方法?我想只是推送一个Response.StatusCode或Null返回不是很好的做法?
My current payload XML when auth'd looks like..
当auth'd看起来像我当前的有效负载XML
<ArrayOfUser>
<user>
<empid>57344</empid>
<firstname>Dave</firstname>
<surname>Johnson</surname>
</user>
<user>
<empid>17324</empid>
<firstname>Mike</firstname>
<surname>Doe</surname>
</user>
</ArrayOfUser>
If an error occurs should I be returning something like...?
如果发生错误,我应该返回类似......的内容吗?
<soap:error>
<errorcode>12345</errorcode>
<errorstring>Invalid username/password</errorstring>
</soap:error>
Or is there a better best practice way?
还是有更好的最佳实践方式?
Second issue is, how would I structure my method so I could return such a XML structure? At present my "Employees" method is of type "User[]" so must return an array of type "User", but if theres been an error I want to return a different type to simulate an XML structure as above or even a simple string stating an error has occured.
第二个问题是,我将如何构造我的方法,以便我可以返回这样的XML结构?目前我的“Employees”方法是“User []”类型,所以必须返回一个类型为“User”的数组,但如果这是一个错误,我想返回一个不同的类型来模拟上面的XML结构甚至是一个简单的字符串表示发生了错误。
How would I achieve this?
我怎么做到这一点?
Any help would be great! Cheers!
任何帮助都会很棒!干杯!
2 个解决方案
#1
2
Just have your method throw an exception - the .Net framework will convert that into a SOAP error message.
只是让您的方法抛出异常 - .Net框架将其转换为SOAP错误消息。
If you want more control over the SOAP error message returned then throw a SoapException
如果您想要更多地控制返回的SOAP错误消息,则抛出SoapException
#2
-3
For error handling : You need a good exception handling framework to support your application. If you are using enterpriselibrary exception handling use the below line of code.
对于错误处理:您需要一个良好的异常处理框架来支持您的应用程序。如果您正在使用corporateelibrary异常处理,请使用以下代码行。
enter code here
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "Client Service Policy");
}
It again depends on what message you want to show it to the user. You can log the actual error message and throw customized message to the end user, you can do with the exception handling framework.
它还取决于您要向用户显示的消息。您可以记录实际的错误消息并将自定义消息发送给最终用户,您可以使用异常处理框架。
You can get more details on implementing exception handling framework. http://www.devx.com/dotnet/Article/31463/1954
您可以获得有关实现异常处理框架的更多细节。 http://www.devx.com/dotnet/Article/31463/1954
For returning xml : you can create the elements as shown below. Finally you can convert the xelement to string and you can use the string in your caller method (the string is nothing but XML)
对于返回xml:您可以创建如下所示的元素。最后,您可以将xelement转换为字符串,并且可以在调用方法中使用该字符串(该字符串只是XML)
enter code here
string[] directoriesList = Directory.GetDirectories(System.IO.Path.GetFullPat ("\\mynetworkpath")));
XElement foldersXML = new XElement("Folders");
foldersXML.Add(from directory in valueList select new XElement("Folders", new XAttribute("name", directory.Split('\\')[directory.Split('\\').Length - 1])));
#1
2
Just have your method throw an exception - the .Net framework will convert that into a SOAP error message.
只是让您的方法抛出异常 - .Net框架将其转换为SOAP错误消息。
If you want more control over the SOAP error message returned then throw a SoapException
如果您想要更多地控制返回的SOAP错误消息,则抛出SoapException
#2
-3
For error handling : You need a good exception handling framework to support your application. If you are using enterpriselibrary exception handling use the below line of code.
对于错误处理:您需要一个良好的异常处理框架来支持您的应用程序。如果您正在使用corporateelibrary异常处理,请使用以下代码行。
enter code here
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "Client Service Policy");
}
It again depends on what message you want to show it to the user. You can log the actual error message and throw customized message to the end user, you can do with the exception handling framework.
它还取决于您要向用户显示的消息。您可以记录实际的错误消息并将自定义消息发送给最终用户,您可以使用异常处理框架。
You can get more details on implementing exception handling framework. http://www.devx.com/dotnet/Article/31463/1954
您可以获得有关实现异常处理框架的更多细节。 http://www.devx.com/dotnet/Article/31463/1954
For returning xml : you can create the elements as shown below. Finally you can convert the xelement to string and you can use the string in your caller method (the string is nothing but XML)
对于返回xml:您可以创建如下所示的元素。最后,您可以将xelement转换为字符串,并且可以在调用方法中使用该字符串(该字符串只是XML)
enter code here
string[] directoriesList = Directory.GetDirectories(System.IO.Path.GetFullPat ("\\mynetworkpath")));
XElement foldersXML = new XElement("Folders");
foldersXML.Add(from directory in valueList select new XElement("Folders", new XAttribute("name", directory.Split('\\')[directory.Split('\\').Length - 1])));