'WebClient'에 해당되는 글 1건

  1. 2009.03.19 Loading Dynamic XAPs and Assemblies
Tech/Silverlight2009. 3. 19. 00:52
참고 자료 _ Loading Dynamic XAPs and Assemblies

1. What is Assembly?
2. XAP를 다운로드 하기
3. XAP에서 AppManifest.xaml 추출해 내기
4. AppManifest에 정의된 Assembly를 로드하기
5. 로드된 어셈블리에 있는 UserControl 생성하기


1. What is Assembly?


In the Microsoft .NET framework, an assembly is a partially compiled code library for use in deployment, versioning and security

Assembly는 다음과 같은 특징을 가진다.

1. 어셈블리는 코드들의 논리적인 묶음이다.
2. 어셈블리는 물리적으로 DLL또는 EXE로 존재한다.
3. 한 개의 어셈블리는 한 개이상의 파일을 포함할 수 있다.


2. XAP를 다운로드 하기

Silverlight 어플리케이션 내에서 미리 컴파일된 Silverlight Application(.xap)을 불러들이기 위해서는 WebClient를 사용한다. 불러들인 결과물의 타입은 Stream이다.

WebClient c = new WebClient();
c.OpenReadCompleted += new OpenReadCompletedEventHandler(c_OpenReadCompleted);
c.OpenReadAsync(new Uri("DynamicXAPDataGrid.xap", UriKind.Relative));

void c_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
// e.Result를 통해 xap의 Stream을 사용할 수 있다.
}

참고 사항
Silverlight 기반 응용 프로그램을 빌드하면 XAP가 만들어 진다. XAP 파일은 응용 프로그램을 시작하는데 필요한 모든 파일을 포함하는 압축된 ZIP 파일이다.

XAP 파일에는 다음과 같은 파일이 들어 있다.

1. 패키지된 어셈블리 및 응용 프로그램 진입점을 식별하는 AppManifest.xaml 파일 한 개 
2. 응용 프로그램 클래스가 들어 있는 응용 프로그램 어셈블리 한 개 
3. 0개 이상의 라이브러리 어셈블리 
4. 이미지나 비디오 파일 같은 느슨한 리소스 파일 0개 이상 


3. XAP에서 AppManifest.xaml 추출하기 

XAP 파일의 Stream에서 AppManifest.xaml 파일의 내용을 추출하기 위해서는 Application.GetResourceStream을 사용한다. GetResourceStream을 사용하면 지정한 zip 패키지에서 특정한 리소스 파일을 얻을 수 있다. (얻을 수 있는 값은 StreamResourceInfo 타입이다.)

Application.GetResourceStream(new StreamResourceInfo(e.Result, null), new Uri("AppManifest.xaml", UriKind.Relative))

AppManifest.xaml 파일의 StreamResourceInfo를 얻었다면 그 파일의 Stream을 XML 파싱을 위해 String 형태로 변환한다.

string appManifest = new StreamReader(appManifestStreamInfo.Stream).ReadToEnd();

그리고 얻어낸 string을 System.xml.Linq를 이용해 파싱하여 AssemblyPart 부분을 List로 만들어 둔다.

XElement deploy = XDocument.Parse(appManifest).Root;
List<XElement> parts = (from assemblyParts in deploy.Elements().Elements()
select assemblyParts).ToList();

제 사용한 DynamicXAPDataGrid.xap 파일에 포함된 AppManifest.xaml 파일의 내용은 다음과 같다. 이를 살펴보면 이 xap에 포함된 어셈블리가 무엇인지 확인할 수 있다.

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" EntryPointAssembly="DynamicXAPDataGrid" EntryPointType="DynamicXAPDataGrid.App" RuntimeVersion="2.0.31005.0">
  <Deployment.Parts>
    <AssemblyPart x:Name="DynamicXAPDataGrid" Source="DynamicXAPDataGrid.dll" />
    <AssemblyPart x:Name="System.Windows.Controls.Data" Source="System.Windows.Controls.Data.dll" />
    <AssemblyPart x:Name="System.Windows.Controls" Source="System.Windows.Controls.dll" />
    <AssemblyPart Source="ko/System.Windows.Controls.resources.dll" />
    <AssemblyPart Source="ko/System.Windows.Controls.Data.resources.dll" />
  </Deployment.Parts>
</Deployment>


4. AppManifest에 정의된 Assembly를 로드하기

불러들인 Silverlight 어플리케이션을 사용하기 위해서는, XAML 파일에 정의된 Assembly 파일들을 모두 로드해줘야 한다. AppManifest.xaml에서 얻어 둔 Assembly 들의 경로를 참조하여 로드한다. XAP에 포함된 어셈블리를 로드하기 위해서는 AssemblyPart 클래스를 사용한다.

Assembly asm = null;
foreach (XElement xe in parts)
{
string source = xe.Attribute("Source").Value;
AssemblyPart asmPart = new AssemblyPart();
StreamResourceInfo streamInfo = Application.GetResourceStream(
new StreamResourceInfo(e.Result, "application/binary"), new Uri(source, UriKind.Relative));
if (source == "DynamicXAPDataGrid.dll")
{
asm = asmPart.Load(streamInfo.Stream);
}
else
{
asmPart.Load(streamInfo.Stream);
}
}

AssemblyPart들 중에서 실제 Silverlight 어플리케이션인 DynamicXAPDataGrid.dll 파일은 로드를 실행하고 그 Assembly 객체를 변수에 담아둔다.


5. 로드된 어셈블리에 있는 UserControl 생성하기

실제 Silverlight 어플리케이션 Assembly 객체에서 CreateInstance를 호출해 어플리케이션 내에 포함된 UserControl 객체를 생성할 수 있다.

UserControl myControl = asm.CreateInstance("DynamicXAPDataGrid.Page") as UserControl;


'Tech > Silverlight' 카테고리의 다른 글

LINQ (Language Integrated Query)  (0) 2009.04.06
Silverlight with WEB  (0) 2009.03.30
USING STARTUP PARAMETERS WITH SILVERLIGHT  (0) 2009.03.18
HTML BROWSER INTEGRATION  (0) 2009.03.16
Posted by 알 수 없는 사용자