2012-07-12 4 views
0

가 나는 복합 타입을 만들어 반환하고 다음과 같이 서비스 작업에 반환하고 있습니다 :WCF 데이터 서비스는 복잡한 유형

[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을 호출하여 서비스 작업을 호출하려고하면 브라우저에 오류 페이지가 표시됩니다.

도와 주시겠습니까?

+0

받고있는 오류가 무엇 ? –

+0

웹 페이지를 찾을 수 없습니다. – Andrea

답변

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; } 
    } 
} 
+0

좋은 일터를 찾았습니다. 가짜 매핑을 사용하여 위조 된 엔티티를 만들었습니다. 이제 맞춤 서비스 기능을 위해서 $ top과 같은 특별한 키워드를 사용할 수 있습니다! http : //localhost/MyService.svc/GetCityByZip? zip = '20000'& $ top = 1 – Andrea

+0

@ 앤드 레아 어떻게 그렇게했는지 좀 자세히 설명해 주시겠습니까? 나는 우리가 그것으로부터 또한 이익을 얻는다고 생각한다. – julealgon

+0

@julealgon 질문이 오래된 것은 알지만 WCF 데이터 서비스에는 기본 설명서가 있으므로 답변을 찾기가 어렵습니다. 가짜 엔터티를 만드는 방법은 여기에서 확인할 수 있습니다. http://social.msdn.microsoft.com/ 포럼/ko-ko/e473e2b9-f385-412d-9aba-44acf9719fd8/poco-class-as-a-return-value-adding-metadata로 사용자 정의 서비스 작업을 생성하는 방법? 포럼 = adodotnetdataservices –

0

GetCityByZip 메서드에 2 개의 매개 변수가있는 경우 마지막 코드가 작동합니다. 첫 번째 것은 zip이고 두 번째 것은 top입니다. 귀하의 경우 매개 변수 불일치가 있고 wcf가 메서드를 찾을 수 없습니다.

+1

$ top은 WCF Data Services의 키워드이며, 내 기능의 매개 변수가 아닙니다. http://msdn.microsoft.com/en-us/library/dd744841.aspx – Andrea

관련 문제