0

我创建了一个 ComplexType 并在服务操作中返回它,如下所示:

   [WebGet]
   public IQueryable<ComplexAddressType> GetCityByZip(string zip) 
   {
      List<AddressType> normalizeAddress = NormalizeAddressProcess(new AddressType
                                                                             {
                                                                                 ZIP = zip,
                                                                             }).AddressList;
      return normalizeAddress.Select(x =>new ComplexAddressType
                        {
                            ZIP = x.zip,
                            City = x.City,
                            State = x.State
                        }).AsQueryable();
        }

当我尝试通过调用来调用服务操作时http://localhost/MyService.svc/GetCityByZip?zip='20000',服务操作调用起作用并且浏览器显示城市列表。

当我尝试通过调用来调用服务操作时http://localhost/MyService.svc/GetCityByZip?zip='20000'&$top=1,浏览器会显示一个错误页面。

你能帮帮我吗?

4

2 回答 2

2

假设ComplexAddressType实际上是一个复杂类型,您不能将$top系统查询选项与该服务操作一起使用。如果您根据上面的评论启用详细错误,您可能会收到此错误:

Query options $orderby, $inlinecount, $skip and $top cannot be applied to the requested resource.

为了能够$top与服务操作一起使用,您需要返回实体类型的集合而不是复杂类型。

你也可以在你的函数调用中引入另一个参数,这样你就可以使用如下 URL:

http://localhost:59803/ScratchService.svc/GetProfiles?startsWith='ABC'&top=2

示例代码:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace Scratch.Web
{
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class ScratchService : DataService<ScratchContext>
    {
        static ScratchService()
        {
            Database.SetInitializer(new ScratchContextInitializer());
        }

        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.AllRead);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
            config.UseVerboseErrors = true;
        }

        [WebGet]
        public IQueryable<User> GetUsers(int numUsers)
        {
            var users = new List<User>();

            for (int i = 0; i < numUsers; i++)
            {
                users.Add(new User
                              {
                                  Id = i,
                                  Password = i.ToString(),
                                  Username = i.ToString()
                              });
            }
            return users.AsQueryable();
        }

        [WebGet]
        public IQueryable<Profile> GetProfiles(string startsWith, int top)
        {
            var profiles = new List<Profile>
                            {
                                new Profile{ DisplayName = "A", Preferences = "1" },
                                new Profile{ DisplayName = "AB", Preferences = "2" },
                                new Profile{ DisplayName = "ABC", Preferences = "3" },
                                new Profile{ DisplayName = "ABCD", Preferences = "4" },
                                new Profile{ DisplayName = "ABCDE", Preferences = "5" },
                                new Profile{ DisplayName = "ABCDEF", Preferences = "6" },
                                new Profile{ DisplayName = "ABCDEFG", Preferences = "7" }
                            };

            return profiles.Where(p => p.DisplayName.StartsWith(startsWith)).Take(top).AsQueryable();
        }
    }

    public class ScratchContextInitializer : DropCreateDatabaseAlways<ScratchContext>
    {
    }

    public class ScratchContext : DbContext
    {
        public DbSet<User> Users { get; set; }
    }

    public class Profile
    {
        public string DisplayName { get; set; }
        public string Preferences { get; set; }
    }

    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public Profile Profile { get; set; }
    }
}
于 2012-07-12T17:04:10.473 回答
0

GetCityByZip当方法有 2 个参数时,最后一个代码将起作用。第一个为zip,第二个为top。在您的情况下,您的参数不一致并且 wcf 找不到方法。

于 2012-07-12T08:02:06.710 回答