如何从一个web服务返回多个值? [英] How to return multiple values from a webservice?

查看:198
本文介绍了如何从一个web服务返回多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的Web服务的世界,所以请大家多多包涵。
我创建使用的.asmx文件在Visual Studio 2010中一个非常简单的Web服务

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.

下面是我使用的代码:

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;
        }   
    }
}

当我调用这个并进入值约翰·史密斯它返回str参数:

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

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



我的问题是什么是用于Web服务方法返回多个值的最佳做法?如果值都是相同的数据类型,我应该使用一个数组?如果该值包含不同的数据类型,我需要创建一个自定义类?

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?

推荐答案

我相信,最好的设计是写一个类,并包括它在你的WSDL 。这将使你的服务的描述可沿类签名。这意味着一个客户端,尽管它的语言,就可以使用该类型的对象。

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.

在创建这个类,尽量不要使用。净内置自定义类型,如的DataSet 或电气特性的。尝试使用总是基本类型只要有可能。这将确保你的对象将是很容易序列化和反序列化,以及客户端使用比开发其它的.Net框架

When creating this class, try not to use .Net built-in custom types, like DataSet or 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.

请检查这个问题:的如何声明Web服务
它只一些代码和一个小的建议为好。

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

让我知道如果你需要任何进一步的支持。

Let me know if you need any further support.

更新

让我们说,你想返回,对于一个给定的WebMethod,下面的一组数据:

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


  • 学生姓名

  • 学生的出生日期

  • 该学生是当前分配给(他们的名字代表)

怎么看待服务将签署:

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>

和最好的是,由于此类签名是公共的(包括在WSDL),你可以做到以下是在不同的项目后,通过简单的处理WSDL:

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:

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

这篇关于如何从一个web服务返回多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆