C# 如何从网络服务返回多个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12038900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:10:30  来源:igfitidea点击:

How to return multiple values from a webservice?

c#visual-studio-2010web-servicesasmxwebmethod

提问by Baxter

I am very new to the world of web services so please bear with me. I am creating a very simple web service in Visual Studio 2010 using .asmx files.

我对网络服务的世界很陌生,所以请耐心等待。我正在使用 .asmx 文件在 Visual Studio 2010 中创建一个非常简单的 Web 服务。

Here is the code I am using:

这是我正在使用的代码:

namespace MyWebService
{
    [WebService(Namespace = "http://www.somedomain.com/webservices")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string simpleMethod(String str)
        {
            return "Hello " + str;
        }   
    }
}

When I invoke this and enter a value "John Smith" for the the str parameter it returns:

当我调用它并为它返回的 str 参数输入一个值“John Smith”时:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.somedomain.com/webservices">Hello John Smith</string>

My question is what is the best practice for returning more than 1 value for a web service method? If the values are all the same data type should I use an array? If the the values contain different data types would I need to create a custom class?

我的问题是为 Web 服务方法返回超过 1 个值的最佳实践是什么?如果值都是相同的数据类型,我应该使用数组吗?如果这些值包含不同的数据类型,我是否需要创建自定义类?

采纳答案by Andre Calil

I believe that the best design is to write a class and include it on your WSDL. This will make the class signature available along with the description of your service. This means that a client, despite of it's language, will be able to use an object of that type.

我相信最好的设计是编写一个类并将其包含在您的 WSDL 中。这将使类签名与您的服务描述一起可用。这意味着客户端,不管它是什么语言,都可以使用该类型的对象。

When creating this class, try not to use .Net built-in custom types, like DataSetor anyother. Try always using basic types whenever possible. This will ensure that your object will be easily serialized and deserialized, as well as used by clients developed frameworks other than .Net.

创建此类时,尽量不要使用 .Net 内置自定义类型,例如DataSet或任何其他类型。尽可能尝试始终使用基本类型。这将确保您的对象可以轻松地序列化和反序列化,以及由 .Net 以外的客户端开发的框架使用。

Please, check this question: How to Declare Complex Nested C# Type for Web ServiceIt does show a little code and a small advice as well.

请检查这个问题:如何为 Web 服务声明复杂的嵌套 C# 类型它确实显示了一些代码和一个小建议。

Let me know if you need any further support.

如果您需要任何进一步的支持,请告诉我。



UPDATE

更新

Let's say that you want to return, for a given webmethod, the following set of data:

假设您想为给定的 webmethod 返回以下数据集:

  • Student's name
  • Student's birth date
  • A list of the courses that the student is current assigned to (represented by their names)
  • 学生的名字
  • 学生的出生日期
  • 学生当前分配到的课程列表(以他们的名字表示)

Look at how the service would be signed:

看看服务将如何签名:

public class WebService1 : System.Web.Services.WebService
{
    public class Course
    {
        public string Name { get; set; }
    }

    public class Student
    {
        public string Name { get; set; }
        public DateTime BirthDate { get; set; }
        public List<Course> CurrentCourses { get; set; }
    }

    [WebMethod]
    public Student HelloWorld()
    {
        Student Baxter = new Student();

        Baxter.Name = "Baxter";
        Baxter.BirthDate = new DateTime(1986, 04, 22);
        Baxter.CurrentCourses = new List<Course>();
        Baxter.CurrentCourses.Add(new Course() { Name = "SOAP Webservices 101" });
        Baxter.CurrentCourses.Add(new Course() { Name = "Mastering C#" });
        Baxter.CurrentCourses.Add(new Course() { Name = "Why you (and I) suck at Javascript" });

        return Baxter;
    }
}

After calling it, this is the result:

调用后,结果如下:

<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
    <Name>Baxter</Name>
    <BirthDate>1986-04-22T00:00:00</BirthDate>
    <CurrentCourses>
        <Course>
            <Name>SOAP Webservices 101</Name>
        </Course>
        <Course>
            <Name>Mastering C#</Name>
        </Course>
        <Course>
            <Name>Why you (and I) suck at Javascript</Name>
        </Course>
    </CurrentCourses>
</Student>

And the best is that, because this class signature is public (included in the WSDL), you can do the following at a different project, by simply processing the WSDL:

最好的是,因为此类签名是公开的(包含在 WSDL 中),您可以通过简单地处理 WSDL在不同的项目中执行以下操作:

        ServiceReference1.WebService1SoapClient SoapClient = new ServiceReference1.WebService1SoapClient();
        ServiceReference1.Student IsThisBaxter = SoapClient.HelloWorld();