Sean Li
Sean Li

Reputation: 59

host a WCF services which returns a list of instances of a datacontract

I'm new to visual studio. I create a datacontract about book information. I create a WCF web services parsing a txt file and create a list of instance of that book information.

When I was trying to call this service to get book information and displace on a web form. I found I don't know how to access Datamember of those instance. Can anybody help me?

public interface IService1
{
    [OperationContract]
    List<Book> GetAllBooks();
    [OperationContract]
    String SearchBookById();
}

[DataContract]
public class Book
{
    [DataMember]
    public int Num { get; set; }
    [DataMember]
    public string ID { get; set; }
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public string author { get; set; }
    [DataMember]
    public int year { get; set; }
    [DataMember]
    public float price { get; set; }
    [DataMember]
    public int stock { get; set; }
}

public List<Book> GetAllBooks()
{
    var bookList = new List<Book>();
    int n = 1;

    StreamReader reader = new StreamReader(@"C:\infs 7204\prac3\books(1).txt");
    {
        string fileLine;
        while((fileLine = reader.ReadLine())!= null)
        {
            string[] bookInfo = fileLine.Split(',');
            int year = Int32.Parse(bookInfo[3]);
            float price = float.Parse(bookInfo[4].Trim('$'));
            int stock = Int32.Parse(bookInfo[5]);
            bookList.Add(new Book { Num = n, ID = bookInfo[0], name = bookInfo[1], author=bookInfo[2],
            year=year,price=price, stock=stock});
            n++;

        }
    }
    return bookList;
}

I need to display those book classes in a table on my webpage. Here is how I want to access the datacontract list. But I got an warrning says "Cannot implicitly convert type'prac3.ServiceReference1.Book[] to 'prac3.Book[]'"

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Service1Client client = new Service1Client();
        Book[] list= client.GetAllBooks();


    }
}

Also, Is there any segesstion about which conponent in toolbox should I use to display the table?

Upvotes: 1

Views: 124

Answers (2)

Farhan Nasim
Farhan Nasim

Reputation: 817

Your warning Cannot implicitly convert type'prac3.ServiceReference1.Book[] to 'prac3.Book[] indicates that very likely you have duplicate definition of type Book.

There are two possibilities:

  1. More likely, as Servé Laurijssen has already pointed out, your client project might have a project reference to your service project. Hence you have access to all of the public types defined within it (including Book.) To be sure if your client project has reference to service project: open your client project in Visual Studio, in Solution Explorer expand the References node under your client project, and find if your service project's name appear there.
  2. Or, less likely, you have redefined class Book in your client project.

In both cases, you get two definitions of Book: one prac3.Book (defined in service project) and another prac3.ServiceReference1.Book (generated by service reference in client project, usually in the file Service References\Service Namespace Name\Reference.cs under your client project directory.) In your case, you are trying to set a prac3.ServiceReference1.Book array to a prac3.Book one, hence causing a type mismatch.

For an immediate remedy change the following line in your client code

Book[] list= client.GetAllBooks();

to the following

prac3.ServiceReference1.Book[] list= client.GetAllBooks();

And consider removing the reference to the service project or the duplicate definition. It is very unusual a client has reference to a service assembly that it refers as well.

Upvotes: 0

Serve Laurijssen
Serve Laurijssen

Reputation: 9763

Try to include the full namespace of Book and see if it works. Have you added a project reference to the Book class and generated a service reference also? If that's thje case, better remove the project reference and let svcutil generate a common namespace with ädd service reference".

List<prac3.ServiceReference1.Book> books = client.GetAllBooks();

foreach (prac3.ServiceReference1.Book book in books)
{
  Console.WriteLine(book.author);
}

Upvotes: 1

Related Questions