Microsoft MVP성태의 닷넷 이야기
.NET Framework: 364. WCF RIA 서비스 + Silverlight 사용 예제 [링크 복사], [링크+제목 복사],
조회: 40264
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

WCF RIA 서비스 + Silverlight 사용 예제


이번 글은 아래의 내용을 실습해 본 것입니다.

WCF RIA Services Part 1: Getting Started
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-1-Getting-Started.aspx

그래도 뭐가 다르긴 달라야 하니까 ^^ 굳이 차이점을 설명해 보면 Visual Studio 2012에서 실습을 진행한다는 정도가 있습니다. (실제로 약간 달라진 점이 있습니다.)

편의상, 설명도 위의 글과 유사하게 진행해 보겠습니다. ^^


Step 1: 실버라이트 프로젝트 생성

새 프로젝트 생성에서 "Silverlight" / "Silverlight Application"을 선택해서 생성합니다. 그럼, 다음과 같은 상자가 뜨는데 "Enable WCF RIA Services" 항목을 체크해 줍니다. (물론, 체크하지 않고 생성해도 나중에 수작업으로 구성하는 것이 가능합니다.)

wcf_ria_use_1.png

그럼, Silverlight 프로젝트 한 개와 그 모듈이 포함될 HTML 페이지를 호스팅할 Web 프로젝트가 하나 생성됩니다.


2. Entity Data Model 생성

WCF RIA 서비스를 통해 노출시킬 데이터베이스의 Entity Framework 모델을 생성합니다. 서비스는 웹에서 할 것이므로, 웹 프로젝트에 "새 항목" 추가를 이용해 "ADO.NET Entity Data Model"을 생성합니다.

wcf_ria_use_2.png

위의 화면에서 '확인(OK)' 버튼을 누르면 Model을 선택하는 화면이 뜨는데 제 경우에는 간단하게 기존에 만들어 둔 테스트 DB로부터 "Entity Data Model"을 자동 생성할 것이므로 "Generate from Database" 옵션을 선택했습니다.

그럼, DB 연결 문자열을 설정해야 하는 대화창이 뜨고 거기서 데이터베이스를 선택해 지정합니다. 제 경우에는 다음과 같이 설정이 되었습니다.

wcf_ria_use_3.png

그다음 화면에서는 "Tables" 항목만 선택했습니다. 테스트 DB라서 ^^ 단순하게 "Account", "mytable" 2개의 테이블만 있습니다.

wcf_ria_use_4.png

모든 과정을 완료하면 .edmx 파일이 웹 프로젝트에 추가되고 연결 문자열은 web.config에 보관됩니다.


3. WCF RIA 서비스 정의

2번 단계에서 생성한 모델을 이제 WCF로 노출시켜야 할 텐데요. 이를 위해 "새 항목" / "Web" / "Domain Service Class"를 하나 생성합니다.

wcf_ria_use_5.png

여기서 문제가 하나 발생합니다. "WCF RIA Services Part 1: Getting Started" 글에서는 "Add New Domain Service Class" 창에서 선택할 수 있는 DataContext/ObjectContext 항목이 있다고 나오는데요. Visual Studio 2012에 포함된 "Domain Service Class"는 반드시 ObjectContext로부터 상속받은 Entity Data Model만이 가능합니다.

이에 대해서는 다음의 글을 참조합니다.

"Some Entity Framework context classes may have been excluded" message when adding a new Domain Service Class
; http://support.microsoft.com/kb/2745294

해결 방법을 정리하면 다음과 같습니다.

  1. .edmx 파일을 디자인 모드로 열고, 엔티티가 없는 영역을 마우스로 누른 후 "Properties" 창에서 "Code Generation Strategy" 옵션을 "Default"로 변경
  2. .edmx 파일의 하위에 있는 파일 중에 .tt 확장자를 가진 2개의 파일을 삭제
  3. 프로젝트 다시 빌드

이렇게 조정하고 다시 "Domain Service Class"를 추가하면, 이번엔 정상적으로 2번 단계에서 추가한 .edmx가 보입니다. 노출하기를 원하는 Entity에 대해서 선택하고 확인(OK) 버튼을 누릅니다.

wcf_ria_use_6.png

이걸로, 준비는 모두 끝났습니다. ^^

(참고로, 위의 화면에서 "Expose OData endpoint"가 있는데요. 이 부분은 나중에 수작업으로 쉽게 추가하는 것을 보여드리겠습니다.)


4. Silverlight UI에서 데이터 목록 보여주기

이전 단계에서 한 작업들이 이상이 없는지 이쯤에서 솔루션 전체 빌드를 한 번 해줍니다. 그런 후, 실버라이트 프로젝트의 MainPage.xaml을 디자인 모드로 열고 "VIEW" / "Other Windows" / "Data Sources" 메뉴를 선택합니다. 그럼, 잠시 후 "Data Sources" 창이 열리면서 3번 단계에서 만들어 두었던 웹 프로젝트의 Domain Service 항목이 보입니다. (혹시, 안 보이면 솔루션 전체 빌드를 해줍니다.) 그 하위의 테이블을 XAML 화면에 끌어다 놓기 하면 자동으로 목록을 보여주는 xaml이 생성됩니다.

wcf_ria_use_7.png

F5 키를 눌러 디버깅으로 진입하면 다음과 같이 동작하는 Silverlight 응용 프로그램을 볼 수 있습니다.

wcf_ria_use_8.png

여기까지 아주 기초적인 개념을 잡는 것이고, 보다 자세한 정보는 다음의 글을 참조하시면 도움이 될 것입니다.

WCF RIA Services Part 2: Querying Data
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-2-Querying-Data.aspx

WCF RIA Services Part 3: Updating Data
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-3-Updating-Data.aspx

WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx

WCF RIA Services Part 5 - Metadata and Shared Classes
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-5-Metadata-and-Shared-Classes.aspx

WCF RIA Services Part 6 - Validating Data
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx

WCF RIA Services Part 7 - Authentication and Authorization
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-7-Authentication-and-Authorization.aspx

WCF RIA Services Part 8 - Testing and Debugging
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-8-Testing-and-Debugging.aspx

WCF RIA Services Part 9 - Structuring Your Application
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-9-Structuring-Your-Application.aspx

WCF RIA Services Part 10 - Exposing Domain Services To Other Clients
; http://www.silverlightshow.net/items/WCF-RIA-Services-Part-10-Exposing-Domain-Services-To-Other-Clients.aspx




개인적으로 WCF RIA 서비스가 마음에 드는 점이 있다면, 데이터를 무거운 XML 직렬화를 사용하지 않고, 바이너리와 유사한 포맷으로 했다는 것입니다. "JMeter로 XML 웹 서비스 호출에 대한 부하 테스트 방법" 글에서 설명했던 것처럼, IE에서 개발자 도구를 이용해 WCF RIA 서비스 호출을 확인할 수 있는데요.

요청부터 재미있습니다. ^^

GET /ClientBin/SilverlightApplication1-Web-DomainService1.svc/binary/GetAccounts HTTP/1.1
Accept: */*
Referer:    http://localhost:6813/ClientBin/SilverlightApplication1.xap
Accept-Language:    ko,en-US;q=0.7,en;q=0.3
Accept-Encoding:    gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
Host:   localhost:6813
DNT:    1
Connection: Keep-Alive

사실, 웹 프로젝트의 물리적인 파일 상으로는 종래의 WCF와는 달리 확장자가 .svc인 파일을 찾아볼 수 없습니다. 그런데, 위의 요청을 보면 .svc가 명시되어 있고 경로는 DomainService1 클래스가 구현된 네임스페이스 정보까지 '-' 문자로 이어진 형태입니다. ("/ClientBin/" 경로는 silverlight을 포함하는 웹 페이지가 놓인 폴더 경로입니다.)

실제로 웹 브라우저를 이용해 "http://localhost:6813/ClientBin/SilverlightApplication1-Web-DomainService1.svc/binary/GetAccounts"로 곧바로 조회하는 것이 가능합니다. (Web Application만 같다면 /ClientBin 경로는 생략해도 됩니다.) 대신 응답 유형은 HTML이나 XML이 아닙니다.

HTTP/1.1 200 OK
Cache-Control:  no-cache
Pragma: no-cache
Content-Length: 1260
Content-Type:   application/msbin1
Expires:    -1
Server: Microsoft-IIS/8.0
X-AspNet-Version:   4.0.30319
X-SourceFiles:  =?UTF-8?B?ZDpcc2V0dGluZ3NcRGVz...[생략]...uc3ZjXGJpbmFyeVxHZXRBY2NvdW50cw==?=
X-Powered-By:   ASP.NET
Date:   Fri, 08 Mar 2013 07:52:37 GMT

Response Body의 내용은 파일로 저장해서 확인할 수 있는데, 다음과 같습니다.

wcf_ria_use_9.png

보시는 것처럼, 거의 바이너리 유형에 가깝습니다. Content-Type이 "application/msbin1"인 이유가 있었던 것입니다. Silverlight과의 연동이라면 둘다 닷넷 플랫폼일 테니 일면 타당하기도 합니다.




물론, WCF RIA 서비스를 다른 식으로 직렬화하는 것도 가능합니다. 본문에서 "Expose OData endpoint"에 대해 넘어갔었는데, 이제 추가를 해보겠습니다.

우선, 웹 프로젝트에 "System.ServiceModel.DomainServices.Hosting.OData" 어셈블리에 대한 참조를 합니다. 그다음, web.config 파일을 열어 configSections에 domainServices 노드를 추가하고,

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

    <sectionGroup name="system.serviceModel">
      <section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowDefinition="MachineToApplication" requirePermission="false" />
    </sectionGroup>    
  </configSections>
  
  ...[생략]...

<configuration>

그다음, system.serviceModel 하위에 domainServices 노드를 추가하고 OData 항목을 포함시킵니다.

<system.serviceModel>

    <domainServices>
        <endpoints>
            <add name="OData" type="System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory, System.ServiceModel.DomainServices.Hosting.OData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </endpoints>
    </domainServices>      
      
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />
</system.serviceModel>

마지막으로, OData로 노출시켜줄 Domain Service의 메소드에 Query 특성을 부여합니다.

namespace SilverlightApplication1.Web
{
...[생략]...

    [EnableClientAccess()]
    public class DomainService1 : LinqToEntitiesDomainService<UnitTestDB2Entities1>
    {
        [Query(IsDefault = true)]
        public IQueryable<Account> GetAccounts()
        {
            return this.ObjectContext.Accounts;
        }
    }
}

이제 빌드하고 다음의 URL로 OData가 노출되는 API 목록을 확인할 수 있습니다.

http://localhost:6813/ClientBin/SilverlightApplication1-Web-DomainService1.svc/odata/

API 호출은 웹 브라우저로도 확인이 되는데요. 위의 화면에서 확인된 API 이름을 그대로 이어서 붙여주면 됩니다.

http://localhost:6813/ClientBin/SilverlightApplication1-Web-DomainService1.svc/odata/AccountSet




마지막으로, 관련된 오류 상황 정리해 봅니다. ^^

1) 실습하는 중에 Silverlight를 실행시킨 후 서비스 호출 시 다음과 같은 오류 메시지를 만날 수 있습니다.

System.ServiceModel.DomainServices.Client.DomainOperationException: Load operation failed for query '...'. The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.


그럼, 브라우저를 이용해 직접 .svc 호출을 한 후 좀 더 상세한 오류 정보를 response body로 받을 수 있는데요.

@Fault5http://schemas.microsoft.com/ws/2005/05/envelope/none@Code@Value?Sender@Reason@Textxmllang?ko-KR삇The specified named connection is either 

not found in the configuration, not intended to be used with the EntityClient provider, or not valid.@Detail@DomainServiceFaultDomainServices 

i)http://www.w3.org/2001/XMLSchema-instance@   ErrorCode뗵@ErrorMessage삇The specified named connection is either not found in the configuration, not 

intended to be used with the EntityClient provider, or not valid.@IsDomainException?
StackTrace?   at 

System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
   at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
   at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
   at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
   at SilverlightApplication1.Web.UnitTestDBEntities..ctor() in d:\...\RIAServicesLibrary1\SilverlightApplication1.Web\Model1.Designer.cs:line 34


보시는 것처럼, 오류 메시지도 살짝 바이너리로 인코딩되어 있어서 다소 이상하지만, UnitTestDBEntities 생성자에서 오류가 발생한 것은 알 수 있습니다.

기본 생성자가 실행되는 경우, .edmx 생성 당시의 연결 문자열 키가 들어가게 되는데요. 여기서 제가 실행한 예제의 경우 "UnitTestDBEntities"가 기본으로 지정되어 있습니다. 문제는 이것이 web.config에 없을 때 발생합니다. 따라서, 명시적으로 web.config에 다음의 설정으로 자신의 상황에 맞게 .edmx를 위한 연결 문자열을 추가해 주시면 됩니다.

  <connectionStrings>
    <add name="UnitTestDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=[dbsrv];initial catalog=[dbname];user 
id=[mydbaccnt];Password=[mydbpw];MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
  </connectionStrings>

2) 그리고, 어떤 경우에는 이런 오류도 발생할 수 있습니다.

Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Runtime.AsyncResult.End(IAsyncResult result) +324
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +501
   System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +107

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272  

이유는, web.config에서 system.serviceModel의 하위에 serviceHostingEnvironment 옵션이 누락되었기 때문입니다. 따라서, 다음과 같이 추가해 주시면 위의 오류가 발생하지 않습니다.

    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
          multipleSiteBindingsEnabled="true" />

        ...[생략]...




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/8/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 31  32  33  [34]  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
13089정성태6/28/202215081개발 환경 구성: 646. HOSTS 파일 변경 시 Edge 브라우저에 반영하는 방법
13088정성태6/27/202213504개발 환경 구성: 645. "Developer Command Prompt for VS 2022" 명령행 환경의 폰트를 바꾸는 방법
13087정성태6/23/202217489스크립트: 41. 파이썬 - FastAPI / uvicorn 호스팅 환경에서 asyncio 사용하는 방법 [1]
13086정성태6/22/202216893.NET Framework: 2026. C# 11 - 문자열 보간 개선 2가지파일 다운로드1
13085정성태6/22/202216661.NET Framework: 2025. C# 11 - 원시 문자열 리터럴(raw string literals)파일 다운로드1
13084정성태6/21/202215441개발 환경 구성: 644. Windows - 파이썬 2.7을 msi 설치 없이 구성하는 방법
13083정성태6/20/202216044.NET Framework: 2024. .NET 7에 도입된 GC의 메모리 해제에 대한 segment와 region의 차이점 [2]
13082정성태6/19/202215106.NET Framework: 2023. C# - Process의 I/O 사용량을 보여주는 GetProcessIoCounters Win32 API파일 다운로드1
13081정성태6/17/202214254.NET Framework: 2022. C# - .NET 7 Preview 5 신규 기능 - System.IO.Stream ReadExactly / ReadAtLeast파일 다운로드1
13080정성태6/17/202215240개발 환경 구성: 643. Visual Studio 2022 17.2 버전에서 C# 11 또는 .NET 7.0 preview 적용
13079정성태6/17/202212683오류 유형: 814. 파이썬 - Error: The file/path provided (...) does not appear to exist
13078정성태6/16/202215837.NET Framework: 2021. WPF - UI Thread와 Render Thread파일 다운로드1
13077정성태6/15/202216852스크립트: 40. 파이썬 - PostgreSQL 환경 구성
13075정성태6/15/202213694Linux: 50. Linux - apt와 apt-get의 차이 [2]
13074정성태6/13/202214722.NET Framework: 2020. C# - NTFS 파일에 사용자 정의 속성값 추가하는 방법파일 다운로드1
13073정성태6/12/202214820Windows: 207. Windows Server 2022에 도입된 WSL 2
13072정성태6/10/202215049Linux: 49. Linux - ls 명령어로 출력되는 디렉터리 색상 변경 방법
13071정성태6/9/202215705스크립트: 39. Python에서 cx_Oracle 환경 구성
13070정성태6/8/202216392오류 유형: 813. Windows 11에서 입력 포커스가 바뀌는 문제 [1]
13069정성태5/26/202218376.NET Framework: 2019. C# - .NET에서 제공하는 3가지 Timer 비교 [2]
13068정성태5/24/202217047.NET Framework: 2018. C# - 일정 크기를 할당하는 동안 GC를 (가능한) 멈추는 방법 [1]파일 다운로드1
13067정성태5/23/202215159Windows: 206. Outlook - 1년 이상 지난 메일이 기본적으로 안 보이는 문제
13066정성태5/23/202214628Windows: 205. Windows 11 - Windows + S(또는 Q)로 뜨는 작업 표시줄의 검색 바가 동작하지 않는 경우
13065정성태5/20/202215859.NET Framework: 2017. C# - Windows I/O Ring 소개 [2]파일 다운로드1
13064정성태5/18/202215230.NET Framework: 2016. C# - JIT 컴파일러의 인라인 메서드 처리 유무
13063정성태5/18/202215620.NET Framework: 2015. C# - 인라인 메서드(inline methods)
... 31  32  33  [34]  35  36  37  38  39  40  41  42  43  44  45  ...