David Kennedy’s Tech Ramblings

Just another WordPress.com weblog

Silverlight Adventures Part 2 August 26, 2008

Filed under: Silverlight Tutorials — dotnetdave @ 12:29 am

The Grid

Ok, for this exercise we start with a new Silverlight project (you can create this in either Expression Blend or Visual Studio). Next, we want to add a reference to system.windows.controls.data (to make the datagrid control accessible) before dragging the new Silverlight Datagrid control onto the page, producing xaml as follows:

<UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverGridClient.Page" Width="640" Height="480"> <Grid x:Name="LayoutRoot" Background="White"> <my:DataGrid x:Name="dg" AutoGenerateColumns="True"></my:DataGrid> </Grid> </UserControl>

Next, we want to add a WCF Service project to the solution within VS. As a data source for our grid I’ve opted for a simple LinQ to SQL dbml, so add a new ‘LINQ to SQL Classes’ item to the WCF project. From server explorer, define a connection to any DB that has some data in it, and drag a populated table onto the designer. Since we are passing this type via our WCF service, we need to decorate the generated LinQ classes (in the designer.cs file) with the DataContract attribute. Open the Web.config for the WCF service & ensure that the service endpoint is defined with basicHttpBinding as the binding attribute. Finally, we need to define a method to return data for our grid. In my case I was using a table specifiying documents from a recent project I was involved with, so I added a method as follows:

public List<FreestyleDocument> GetAllDocuments() { SilverLinqDataContext linq = new SilverLinqDataContext("Server=.;Database=Data_DavesJuly;Integrated Security=true"); return linq.FreestyleDocuments.Take(500).ToList(); }

Now build the WCF service.

Next we need to generate a proxy for our service, so we want to add a service reference to our WCF service from the Silverlight project. Kick up the WCF service (or host it in IIS if you prefer) and navigate to the service endpoint from the ‘Add Service Reference’ context option. This will create our proxy as well as a ServiceReferences.ClientConfig file defining the endpoint. Finally, we want to include the call via the proxy to our WCF service method which we have defined in order to populate the grid view. Do this in the Page.xaml.cs file; in my case the code looked like this:

public Page() { //InitializeComponent(); SilverProxy.Service1Client webService = new SilverProxy.Service1Client(); webService.GetAllDocumentsCompleted += new EventHandler<SilverProxy.GetAllDocumentsCompletedEventArgs>(webService_GetUsersCompleted); webService.GetAllDocumentsAsync(); } void webService_GetUsersCompleted(object sender, SilverProxy.GetAllDocumentsCompletedEventArgs e) { dg.ItemsSource = e.Result; }

At this stage it would be possible for the Silverlight application to directly access our WCF service if it weren’t for the browser-side sand-boxing which prevent cross domain calls. Attempting to run the application now will result in something similar to the following error:

An error occurred while trying to make a request to URI ‘http://localhost/SilverService/Service1.svc’. This could be due to a cross domain configuration error. Please see the inner exception for more details.

According to online documentation, this problem should be averted by including one or both of the following configuration override files in the root of the hosting web server (in my case I hosted the service in IIS to make this process simpler):

crossdomain.xml:

<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy> <allow-http-request-headers-from domain="*" headers="*"/> </cross-domain-policy>

clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8" ?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="*"> <domain uri="*"/> </allow-from> <grant-to> <resource path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access> </access-policy>

In my case neither of these files made any difference; I continued to receive the cross domain call error. Failing this approach, the next step is to institute an intermediate proxy on the server side of our Silverlight project, thus negating the cross domain call from the browser. After moving the Linq2Sql ORM & WCF service into the main ASP.NET site which included the Silverlight application the datagrid was bound to the data as expected.

 

Getting Reporting Services running under Vista August 13, 2008

Filed under: Tech Tips — dotnetdave @ 1:33 am

My environment is Vista Ultimate 32 bit, with IIS & ASP.NET already configured for VS development.

You need to install Sql Server 2005 SP2 AFTER including the Reporting Services component in the original Sql Server installer (this can be added separately, but you still need to run SP2 again as it updates the Reporting Services)

Next you must remove an extra slash from the ‘AboMapperCustom’ Handler entry under the ReportServer site from within IIS Manager.

ie. C:\Windows\Microsoft.NET\Framework\v2.0.50727\\aspnet_isapi.dll -> C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll

Run the Reporting Services Configuration Manager and ensure that the virtual directories & database is setup correctly, do so as required

Verify permissions to the rsreportserver.config file in the ReportingServices\ReportServer directory under your Sql Server installation (I took the sledgehammer approach and added ‘Everyone’ for Read/Execute permissions)

That was enough to get me up and running! 🙂

 

Silverlight Pacman!

Filed under: Silverlight Tutorials — dotnetdave @ 12:29 am

I’ve finally got my pacman game hosted on live.com 🙂

http://silverlight.services.live.com/invoke/75950/Pacman/iframe.html

 

MVC Discoveries (from my desktop notepad files :P) August 12, 2008

Filed under: Uncategorized — dotnetdave @ 5:30 am

After having lived in WinForms & back-office web services land for so long I’m a little bit rusty, and with the changes to the MVC framework having been significant between the 4 pre-releases its made for an up-hill battle. Mostly for my own personal benefit, I’ll dump a few pointers here that I’ve found helpful or could have saved me some time a couple of weeks ago.

To start with, I gained a great deal from ScottGu’s blog posts regarding the framework:
http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx
also the screencast from Scott Hanselman was useful, available from:
http://www.hanselman.com/blog/ASPNET35ExtensionsPlusMVCHowToScreencast.aspx

Recent changes in the Pre-release 4 preview:

ControllerActionAttribute gone – any public method on Controller classes will be exposed
RenderView method replaced by ActionResult return for Controller methods (RenderView has been changed from public to protected for reasons known to M$)

General reminders:

Load up ViewPages from the Page_Load() event (with AutoEventWireup=true, the default), NOT the constructor! This seems blindingly obvious to me now, but it still took me a little while before I twigged to this one.

When you kick up a debug session out of VS2008 while in either the code or designer file for a ViewPage, it defaults to adding the ‘.aspx’ extension. With IIS7 & the MVC routing, this doesn’t work. You need to hit the path without the ‘.aspx’ extension, and let the MVC routing & controllers take care of the rest.

This at least got me up and running & able to add my own ViewPages to an MVC site. Happy MVCing!