Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Friday, November 19, 2010

Using ASP.NET authentication in Silverlight with RIA Services

The first step is to set up the database you wish to authenticate against (ignore this if you are using Windows authentication, which wouldn't apply for most websites).

If you are not using the default DB, add this to the configuration section in your web.config:

<connectionStrings>
    <clear/>
    <add name="LocalSqlServer" connectionString="data source=(local); Integrated Security=SSPI;Initial Catalog=ASPNET;" providerName="System.Data.SqlClient" />
</connectionStrings>

(Thanks to centricle.com for the html encoding utility.)

To add the default ASP.NET authentication tables to the database, use aspnet_regsql.

asp.net has a good introduction to setting up ASP.NET authentication.

This blog entry shows how to modify the silverlight business templates to use ASP.NET forms authentication with "stay logged in" feature.

RIA tid-bits

Server side:
DomainService (with [EnableClientAccess])

[ServiceOperation] for generic operations

[Query] or Get...(args); returning T, IEnumerable or IQueryable
Use HasSideEffects = true to send query as post.
[Insert] or Insert...(args)
[Update] or Update...(args)
[Delete] or Delete...(args)
Resolve is used for optimistic concurrency resolution.
[Custom] methods return void and take an entity as the first parameter but is not one of the above. They are change tracked etc.
[ServiceOperation] are more like web methods.

Entities may inherit from entity, but must have a key if used in an IEnumerable collection.

[Include] is used to link classes together, for example Job and JobNotes. This will ensure the code generator will copy JobNotes to the client project. This can also be used for data denormalisation on the Job class's metadata class:
[Include("NoteDateTime", "DateTime")]
public JobNotes JobNotes;
This will result in a new property on the Job class on the client called NoteDateTime and only serialise the DateTime of the job note, not the detail.

[MetadataType] can be used to specify another class whose attributes are merged with the main class. Attributes on properties in the metadata class are merged with those on the main class. This is useful for modifying the attributes on compiler generated code through partial classes.

Client side:
Perform linq operations to filter (if required) the query, using the query as IEnumerable, then use the DomainContext's Load methods to load the data asynchronously.

DataGrid / DataForm / FieldLabel:
[Bindable] specifies whether a property is bindable or not.
[Display(Name, Description)]

Validation:
[StringLength]
[Required]
[Range]
[RegularExpression]
[CustomValidation]
[DataType]
Custom validation attributes can be made by deriving from ValidationAttribute.

Authorisation:
[RequiresAuthentication]

Shared code:
Code file should be called xxxxx.shared.cs, then use [Shared].
Use [CustomValidation] for validation code (see this).

A useful application of shared code is to add a partial class that extends the functionality of a generated class, i.e. to add a new calculated property. This class will then be copied to the client and the calculated property will be in both client and server code.

More detail:
See this page.

Monday, January 18, 2010

Broken Silverlight breakpoints

When debugging a Silverlight application in Visual Studio, sometimes breakpoints will not be set due to a symbol load error. This appears to be caused by the xap file being cached. In my case, the following PowerShell command fixed the problem:

dir -fo -r "~\AppData\Local\Microsoft\Windows\Temporary Internet Files" -inc *.xap | % {del $_}

Thursday, November 5, 2009

Frameworks and tools for Silverlight

To keep track of the ballooning list of frameworks and helpers that are required or helpful when working with Silverlight and/or WPF, I've started a list (in no particular order) here:

1 Silverlight Tools for VS 2008 SP1
2 Composite WPF and Silverlight (Prism)
3 Silverlight Toolkit
4 RIA Services
5 Silverlight Unit Test Framework
6 This Microsoft hotfix for a problem with unit tests in projects contained in the same solution as a Silverlight project.

Useful control toolkits:
1. Dev-express Silverlight DataGrid
2. Visifire open source data visualisation tools (charting)
3. Blacklight

Hopefully VS 2010 will roll some of these up and they won't have to be downloaded separately.