繁体   English   中英

将WCF服务连接到ASP.NET Web窗体应用程序时出现问题

[英]Trouble while connecting my WCF service to ASP.NET Web Forms application

我有一个有关书店的ASP.NET Web窗体应用程序。 我创建了一个数据库,并在其中包含一些书籍。 我原来的ASP.NET应用程序运行正常,并且可以在创建的“图书”页面中查看我的图书。 在该页面中,我可以看到它们的名称,缩略图和价格,以及显示“添加到购物车”的链接,当按下该链接时,会将书放入我创建的购物车中。

然后,我创建了WCF服务,希望将图书目录作为服务。 我在WCF服务应用程序中添加了原始项目的.dll引用,这是服务中的文件,其中包含我要实现的单个服务。

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

然后,我创建了第三个项目,另一个ASP.NET Web窗体应用程序,在这里我要使用之前创建的WCF项目,并在当前的第三个ASP.NET项目中显示第一个项目的目录。 我创建了一个名为目录的新页面,我希望在其中显示第一个项目中的书籍。 我已经添加了我以前的WCF服务的服务参考,还添加了我的第一个ASP.NET应用程序的参考.dll文件。 在下面,您可以找到我在第三个项目中创建的有关目录文件的代码。 请注意,.aspx文件与显示我的书的第一个项目.aspx文件完全相同。 请注意,在第三个项目的Catalog.aspx.cs文件中,如下所示,我已经创建了先前创建的WCF服务的新实例,并使用其中的方法显示书籍。

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

因此,问题如下,当我打开运行我的第三个项目并进入目录页面时,我没有看到第一个项目中的书籍,而是看到一条文字:“未返回任何数据。” 如果有人可以帮助我解决这个问题,我将很高兴。

我相信GetBookData不应为空,它应该返回一个IQueryable。 尝试

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

        return catalog.GetBooks();
    }

看来您在混淆wcf服务和wcf dataservices。

标准wcf服务无法返回真正可查询的IQueryable。 本质上,它只是ToLists IQueryable(下拉数据库中的每一行)并将其作为集合发送到整个网络。 在您的客户上,当您收到大量书籍时,您会看到证据。

如果希望能够使用IQueryable通过网络将过滤的查询发送到服务器,则需要使用wcf数据服务。 数据服务在客户端上使用IQueryable生成到服务器的其余请求,这些请求中继了查询的意图。 服务器接收任意查询并返回数据。 请参阅http://msdn.microsoft.com/en-us/library/cc668792.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM