0

I have an ASP.NET Web Forms application regarding a bookstore. I have created a database, and included some books inside. My original ASP.NET application works just fine and I'm able to view my books, in Books page that I have created. In that page I'm able to see their name, thumb image, and price along with a link saying "Add To Cart", which in turn when pressed puts the book in the cart that I have created.

Then I created a WCF service, where I want to have my book catalog as a service. I added a .dll reference of my original project to my WCF service application, and here is the files in my service, with a single service that I want to implement.

Catalog.svc.cs

public class Catalog : ICatalog
{
    public IQueryable<Bookstore.Models.Book> GetBooks()
    {
        var _db = new Bookstore.Models.BookContext();
        IQueryable<Book> query = _db.Books;

        return query;
    }

ICatalog.cs

[ServiceContract]
public interface ICatalog
{
    [OperationContract]
    IQueryable<Book> GetBooks();
}

Then I created a third project, another ASP.NET Web Forms application, where I want to use my previously created WCF project, and display the catalog of my first project in my current third ASP.NET project. I created a new page called Catalog where I want my books from the first project to be displayed. I already added the service reference of my previous WCF service, and also added the reference .dll file of my first ASP.NET application. Below you can find the code about my Catalog file, that I created in my third project. Please note that the .aspx file is completely the same as my first projects .aspx file where my books were displayed. Please note that in my third project's Catalog.aspx.cs file, as you can see below I have created a new instance of my previously created WCF service, and used the method there to display the books.

Catalog.aspx

<%@ Page Title="Books" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
         CodeBehind="Catalog.aspx.cs" Inherits="BookstoreServiceImplementation.Catalog" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1><%: Page.Title %></h1>
            </hgroup>

                 <section class="featured">
                    <ul> 
                        <asp:ListView ID="bookList" runat="server"
                            DataKeyNames="BookID"
                            GroupItemCount="3" ItemType="Bookstore.Models.Book" SelectMethod="GetBookData">
                            <EmptyDataTemplate>      
                                <table id="Table1" runat="server">        
                                    <tr>          
                                        <td>No data was returned.</td>        
                                    </tr>     
                                </table>  
                            </EmptyDataTemplate>  
                            <EmptyItemTemplate>     
                                <td id="Td1" runat="server" />  
                            </EmptyItemTemplate>  
                            <GroupTemplate>    
                                <tr ID="itemPlaceholderContainer" runat="server">      
                                    <td ID="itemPlaceholder" runat="server"></td>    
                                </tr>  
                            </GroupTemplate>  
                            <ItemTemplate>    
                                <td id="Td2" runat="server">      
                                    <table>        
                                        <tr>          
                                            <td>&nbsp;</td>          
                                            <td>
                                                <a href="<%#: GetRouteUrl("BookByNameRoute", new {bookName = Item.BookName}) %>">
                                                    <image src='/Catalog/Images/Thumbs/<%#:Item.ImagePath%>'
                                                        width="75" height="100" border="1"/>
                                                </a>
                                            </td>
                                            <td>
                                                <a href="<%#: GetRouteUrl("BookByNameRoute", new {bookName = Item.BookName}) %>">
                                                    <%#:Item.BookName%>
                                                </a>       
                                                <br />
                                                <span class="BookPrice">           
                                                    <b>Price: </b><%#:String.Format("{0:c}", Item.UnitPrice)%>
                                                </span>
                                                <br />
                                            </td>        
                                        </tr>      
                                    </table>    
                                </td>  
                            </ItemTemplate>  
                            <LayoutTemplate>    
                                <table id="Table2" runat="server">      
                                    <tr id="Tr1" runat="server">        
                                        <td id="Td3" runat="server">          
                                            <table ID="groupPlaceholderContainer" runat="server">            
                                                <tr ID="groupPlaceholder" runat="server"></tr>          
                                            </table>        
                                        </td>      
                                    </tr>      
                                    <tr id="Tr2" runat="server"><td id="Td4" runat="server"></td></tr>    
                                </table>  
                            </LayoutTemplate>
                        </asp:ListView>
                    </ul>
               </section>
        </div>
    </section>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>

Catalog.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.ModelBinding;
using Bookstore.Models;

namespace BookstoreServiceImplementation
{
    public partial class Catalog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void GetBookData()
        {
            CatalogReference.CatalogClient catalog = new CatalogReference.CatalogClient();

            catalog.GetBooks();
        }
    }
}

So the problem is as follows, when I open run my third project, and enter to the Catalog page, instead of seeing the books from my first project, I get only a text saying: "No data was returned." If someone can help me solve this problem, I would be glad.

4

2 回答 2

0

I believe GetBookData should not be void, it should return an IQueryable. Try

    public IQueryable<Book> GetBookData()
    {
        CatalogReference.CatalogClient catalog = new CatalogReference.CatalogClient();

        return catalog.GetBooks();
    }
于 2013-05-03T20:53:02.070 回答
0

It looks like you are confusing wcf services and wcf dataservices.

A standard wcf service cannot return a truly queryable IQueryable. Essentially, it just ToLists the IQueryable (pulling down every row in the database) and sends it across the wire as a collection. On your client, you see the evidence of this as you are receiving an array of books.

If you wish to be able to send filtered queries over the wire to the server using IQueryable, you need to use wcf data services. Data services uses an IQueryable on the client to generate rest requests to the server which relay the intent of you query. The server receives the arbitrary query and returns the data. See http://msdn.microsoft.com/en-us/library/cc668792.aspx

于 2013-05-04T05:43:29.047 回答