wcf 将一个类的多个实例返回给客户端

发布于 2024-11-09 03:28:31 字数 3332 浏览 0 评论 0原文

问题:我有一个 Web 服务启动并运行正常,我遇到的问题是我需要返回一个类的多个实例,并且不知道如何执行此操作。

我在服务中设置了一个循环:

If localtab.Rows.Count > 0 Then
            Do While i <= localtab.Rows.Count
                Mbr.Urn = localtab.Rows(i).Item(0) & vbNullString
                Mbr.Title = localtab.Rows(i).Item(1) & vbNullString
                Mbr.Initials = localtab.Rows(i).Item(2) & vbNullString
                Mbr.Surname = localtab.Rows(i).Item(3) & vbNullString
                Mbr.Address1 = localtab.Rows(i).Item(4) & vbNullString
                Mbr.Address2 = localtab.Rows(i).Item(5) & vbNullString
                Mbr.Address3 = localtab.Rows(i).Item(6) & vbNullString
                Mbr.Town = localtab.Rows(i).Item(7) & vbNullString
                Mbr.County = localtab.Rows(i).Item(8) & vbNullString
                Mbr.Country = localtab.Rows(i).Item(9) & vbNullString
                Mbr.Postcode = localtab.Rows(i).Item(10) & vbNullString
                Mbr.msg = "Success"

                i = i + 1
            Loop
        Else
            Mbr.msg = "Fail - no record found"
        End If 

我知道这工作正常,因为它返回数据库中列出的最后一个成员。执行诸如 Mbr(i).urn 等操作...不会起作用,因为当我循环 i 时,它只会返回第一个实例,并且一旦服务返回一次会停止。

下面是客户端调用服务的代码 - 足够简单,我现在所寻找的只是一个显示每个姓氏的消息框。

Dim abMem As New ArdbegMember
    Dim retMem As ArdbegMember

    abMem.Downloaded = "N"

    Try
        cc.Open()
        retMem = cc.MbrReq(abMem)
        MesgBox(retMem.Surname)
        cc.Close()

我需要知道的是如何将完整的记录集传递回客户端

编辑

所以根据下面的建议,我的代码现在看起来像这样 -

Dim results As List(Of ArdbegMember)
            Dim i As Integer = 0

            'assign values from the table to the ArdbegMember object
            If localtab.Rows.Count > 0 Then
                Do While i <= localtab.Rows.Count
                    Mbr.Urn = localtab.Rows(i).Item(0) & vbNullString
                    Mbr.Title = localtab.Rows(i).Item(1) & vbNullString
                    Mbr.Initials = localtab.Rows(i).Item(2) & vbNullString
                    Mbr.Surname = localtab.Rows(i).Item(3) & vbNullString
                    Mbr.Address1 = localtab.Rows(i).Item(4) & vbNullString
                    Mbr.Address2 = localtab.Rows(i).Item(5) & vbNullString
                    Mbr.Address3 = localtab.Rows(i).Item(6) & vbNullString
                    Mbr.Town = localtab.Rows(i).Item(7) & vbNullString
                    Mbr.County = localtab.Rows(i).Item(8) & vbNullString
                    Mbr.Country = localtab.Rows(i).Item(9) & vbNullString
                    Mbr.Postcode = localtab.Rows(i).Item(10) & vbNullString
                    Mbr.msg = "Success"

                    i = i + 1

                    results.Add(Mbr)
                Loop
            Else
                Mbr.msg = "Fail - no record found"
            End If

            'Tidy up
            dataAdapter.Dispose()
            Cmd.Dispose()
            oConn.Close()

            'Return the ArdbegMember object
            Return results

现在抛出的错误是 -

Value of type 'System.Collections.Generic.List(Of ArdbegWeb.ArdbegMember)' cannot be converted to 'ArdbegWeb.ArdbegMember'.

The problem: I have a web service up and running ok, the problem I have is that i need to return more than one instance of a class and have no real idea how to do this.

I have a loop set up in the service:

If localtab.Rows.Count > 0 Then
            Do While i <= localtab.Rows.Count
                Mbr.Urn = localtab.Rows(i).Item(0) & vbNullString
                Mbr.Title = localtab.Rows(i).Item(1) & vbNullString
                Mbr.Initials = localtab.Rows(i).Item(2) & vbNullString
                Mbr.Surname = localtab.Rows(i).Item(3) & vbNullString
                Mbr.Address1 = localtab.Rows(i).Item(4) & vbNullString
                Mbr.Address2 = localtab.Rows(i).Item(5) & vbNullString
                Mbr.Address3 = localtab.Rows(i).Item(6) & vbNullString
                Mbr.Town = localtab.Rows(i).Item(7) & vbNullString
                Mbr.County = localtab.Rows(i).Item(8) & vbNullString
                Mbr.Country = localtab.Rows(i).Item(9) & vbNullString
                Mbr.Postcode = localtab.Rows(i).Item(10) & vbNullString
                Mbr.msg = "Success"

                i = i + 1
            Loop
        Else
            Mbr.msg = "Fail - no record found"
        End If 

And i know that this works fine as it returns the last member listed in the DB. Doing something like Mbr(i).urn etc... won't work as when I loop through i, it is only ever going to return the first instance, and once the service has returned once it will stop.

Below is the code from the client end calling the service - simple enough, all I am looking for just now is for a message box with each surname to be displayed.

Dim abMem As New ArdbegMember
    Dim retMem As ArdbegMember

    abMem.Downloaded = "N"

    Try
        cc.Open()
        retMem = cc.MbrReq(abMem)
        MesgBox(retMem.Surname)
        cc.Close()

What I need to know is how to pass a full recordset back to the client

EDIT

So based on the suggestions below, my code now looks like this -

Dim results As List(Of ArdbegMember)
            Dim i As Integer = 0

            'assign values from the table to the ArdbegMember object
            If localtab.Rows.Count > 0 Then
                Do While i <= localtab.Rows.Count
                    Mbr.Urn = localtab.Rows(i).Item(0) & vbNullString
                    Mbr.Title = localtab.Rows(i).Item(1) & vbNullString
                    Mbr.Initials = localtab.Rows(i).Item(2) & vbNullString
                    Mbr.Surname = localtab.Rows(i).Item(3) & vbNullString
                    Mbr.Address1 = localtab.Rows(i).Item(4) & vbNullString
                    Mbr.Address2 = localtab.Rows(i).Item(5) & vbNullString
                    Mbr.Address3 = localtab.Rows(i).Item(6) & vbNullString
                    Mbr.Town = localtab.Rows(i).Item(7) & vbNullString
                    Mbr.County = localtab.Rows(i).Item(8) & vbNullString
                    Mbr.Country = localtab.Rows(i).Item(9) & vbNullString
                    Mbr.Postcode = localtab.Rows(i).Item(10) & vbNullString
                    Mbr.msg = "Success"

                    i = i + 1

                    results.Add(Mbr)
                Loop
            Else
                Mbr.msg = "Fail - no record found"
            End If

            'Tidy up
            dataAdapter.Dispose()
            Cmd.Dispose()
            oConn.Close()

            'Return the ArdbegMember object
            Return results

And the error being thrown is now -

Value of type 'System.Collections.Generic.List(Of ArdbegWeb.ArdbegMember)' cannot be converted to 'ArdbegWeb.ArdbegMember'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2024-11-16 03:28:31

您始终可以创建一个返回 List 的服务,前提是您为 定义了具体类型:

[ServiceContract]
public interface IYourService
{
   [OperationContract]
   public List<ArdbegMember> GetMembers();  
}

假设您有一个数据协定类:

[DataContract]
public class ArdbegMember
{
    [DataMember]
    public string Title { get; set; }
    [DataMember]
    public string Initials { get; set; }
    [DataMember]
    public string Surname { get; set; }
    .... (and so forth - define all the properties you need on your `ArdbegMember`)
}

在您的服务中实现,你可以这样写:

public class YourService : IYourService
{
   public List<ArdbegMember> GetMembers()
   {
       List<ArdbegMember> results = new List<ArdbegMember>();

       // load your `localtab` somehow
       foreach(DataRow row in localtab.Rows)
       {
           ArdbegMember member = new ArdbegMember();
           // set the properties, based on the "row"

           results.Add(member);
       }

       return results;
   }  
}

现在你有了一个 WCF 服务,它有一个名为 GetMembers() 的方法(你可能想在你的实际服务中传递一些搜索条件等)并返回返回 ArdbegMember 对象列表。

一些不错的 WCF 资源:

对一本书感兴趣?我会推荐 Michele Leroux Bustamante 的学习 WCF。她以一种非常容易理解和平易近人的方式涵盖了所有必要的主题。这将教您编写高质量、有用的 WCF 服务所需的一切知识 - 基础知识、中级主题、安全性、事务控制等等。

学习 WCF

You can always create a service that returns a List<T> given you define a concrete type for <T>:

[ServiceContract]
public interface IYourService
{
   [OperationContract]
   public List<ArdbegMember> GetMembers();  
}

Assuming you have a data contract class:

[DataContract]
public class ArdbegMember
{
    [DataMember]
    public string Title { get; set; }
    [DataMember]
    public string Initials { get; set; }
    [DataMember]
    public string Surname { get; set; }
    .... (and so forth - define all the properties you need on your `ArdbegMember`)
}

In your service implementation, you could then write:

public class YourService : IYourService
{
   public List<ArdbegMember> GetMembers()
   {
       List<ArdbegMember> results = new List<ArdbegMember>();

       // load your `localtab` somehow
       foreach(DataRow row in localtab.Rows)
       {
           ArdbegMember member = new ArdbegMember();
           // set the properties, based on the "row"

           results.Add(member);
       }

       return results;
   }  
}

So now you have a WCF service with a single method called GetMembers() (you probably want to pass in some search criteria, etc. - in your real service) and returns back a list of ArdbegMember objects.

Some good WCF resources:

Interested in a book? I would recommend Learning WCF by Michele Leroux Bustamante. She covers all the necessary topics, and in a very understandable and approachable way. This will teach you everything - basics, intermediate topics, security, transaction control and so forth - that you need to know to write high quality, useful WCF services.

Learning WCF

风吹短裙飘 2024-11-16 03:28:31

感谢您的帮助,马克,解决方案非常明显,我不应该错过它!

Do While i <= localtab.Rows.Count 应该阅读 Do While i <= localtab.Rows.Count-1。当它尝试将 Mbr 返回到客户端时,循环本身重复太多次,导致出现问题!

Thanks for your help marc, the solution was blindingly obvious and i shouldn't have missed it!

Do While i <= localtab.Rows.Count should have read Do While i <= localtab.Rows.Count-1. The loop was repeating on itself one too many times causing issues when it was trying to return Mbr to the client side!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文