.NET Services November CTP released today

In case you missed it, there was a new release of .NET Services today. LOTS of changes, I’m just starting to digest them now (including some breaking changes, to be expected given that we are at CTP stage).

I really like where this is all heading, and see some interesting new types of applications that we’ll be able to create that would not have been possible without a cloud-based service bus. And better yet, I’m having some of these discussions with customers around real scenarios, leveraging .NET services to solve a lot of distributed application challenges. These are exciting times indeed!

You can read the official announcement and release notes here.

You can download the SDK here.

Retrieving Nested Properties from Lambda Expressions

Normal
0

false
false
false

EN-GB
X-NONE
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:”Table Normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin-top:0cm;
mso-para-margin-right:0cm;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0cm;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:”Calibri”,”sans-serif”;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}

[Source: http://geekswithblogs.net/EltonStoneman]

Supposing you’re using a lambda expression as a neat way to access a named property:

s => s.FirstName

– when what you really want is to access it without using a string for the property name (“FirstName”), for example to specify the source or target property for a mapping exercise.

It’s easy if the expression is a direct property of the parameter type – a member of the “s” in the expression; casting the whole expression as a MemberExpression gives you access to the member as a PropertyInfo. You can encapsulate it in an extension method of LambdaExpression:

public static PropertyInfo AsPropertyInfo(this LambdaExpression expression)

{

PropertyInfo info = null;

if (expression.Body.NodeType == ExpressionType.MemberAccess)

{

info = ((MemberExpression)expression.Body).Member as PropertyInfo;

}

return info;

}

– which lets you access the property like this:

Expression<Func<FullUser, object>> exp = s => s.FirstName;

PropertyInfo sourceProperty = exp.AsPropertyInfo();

But if the expression represents a property of a nested type, or a property of a nested type of a nested type:

s => s.Address.Line1

s => s.Address.PostCode.Code

– then it’s different.

What you want in this case is an executable expression which will access the containing object of the property (e.g. get you the Address property of the source object, or the PostCode property of the Address property of the source object). You also want the PropertyInfo of the target property (e.g. Line1 or Code). And you want both the property accessor and the PropertyInfo from the same expression.

This is not too difficult. It doesn’t require a stack of target objects and PropertyInfos as originally feared. Firstly store the PropertyInfo from the nested target in the normal way:

targetPropertyInfo = targetExpression.AsPropertyInfo();

– this gives us access to Line1 or Code. Then store the expression which retrieves the path to the containing object for the property from the original source – .Address or .Address.Postcode. In the expression tree, this is the expression which contains the root to the original parameter:

if (targetExpression.Body.NodeType == ExpressionType.MemberAccess)

{

MemberExpression memberExpression = targetExpression.Body as MemberExpression;

if (memberExpression.Expression.NodeType != ExpressionType.Parameter)

{

ParameterExpression parameter = GetParameterExpression(memberExpression.Expression);

if (parameter != null)

{

_targetExpression = Expression.Lambda(memberExpression.Expression, parameter);

}

}

}

The key line is the one which contains Expression.Lambda(). The parent expression is a MemberExpression (e.g. s.Address, or s.Address.PostCode) which itself is not executable. You can’t compile a MemberExpression and execute it on an object to get the member, as the expression needs to know the parameter it executes against.

Expression.Lambda creates a compilable, executable expression against a provided target parameter – which we already have from the “s” of the original expression, and we can find for any level of nesting:

private ParameterExpression GetParameterExpression(Expression expression)

{

while (expression.NodeType == ExpressionType.MemberAccess)

{

expression = ((MemberExpression)expression).Expression;

}

if (expression.NodeType == ExpressionType.Parameter)

{

return (ParameterExpression)expression;

}

return null;

}

So now we have the PropertyInfo for the nested target, and an expression which will give us the nested target, we can set the target property value like this:

object target; //this is the top-level object

//…

realTarget = expression.Compile().DynamicInvoke(target);

targetPropertyInfo.SetValue(realTarget, value, null);

It’s quite a complex requirement, but the solution is straightforward and the complexity is isolated from the calling code. Note that the example code above only deals with nested properties, not expressions with mixed properties and method calls (e.g. s.Address.GetPostCode().InwardCode), but it’s straightforward to cover those cases too.

Avoiding TNSNAMES in Oracle .NET data connections

Normal
0

false
false
false

EN-GB
X-NONE
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:”Table Normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin-top:0cm;
mso-para-margin-right:0cm;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0cm;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:”Calibri”,”sans-serif”;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}

[Source: http://geekswithblogs.net/EltonStoneman]

The typical approach for connecting to Oracle data sources using ODP.NET is to use a named source in the connection string and rely on the name being resolved through SQLNAMES.ORA and TNSNAMES.ORA:

app.config:

<connectionStrings>

<add name=XYZ connectionString=Data Source=XYZ.WORLD;User ID=xyz;Password=xyz;/>

</connectionStrings>

TNSNAMES.ORA:

XYZ.WORLD =

(DESCRIPTION =

(ADDRESS_LIST =

(LOAD_BALANCE = off)

(FAILOVER = on)

(ADDRESS_LIST =

(LOAD_BALANCE = on)

(FAILOVER = on)

(ADDRESS = (PROTOCOL = TCP)(HOST = XYZ1-1.internal.xyz.org.uk)(PORT = 1522))

(ADDRESS = (PROTOCOL = TCP)(HOST = XYZ1-2.internal.xyz.org.uk)(PORT = 1522))

)

(ADDRESS_LIST =

(LOAD_BALANCE = on)

(FAILOVER = on)

(ADDRESS = (PROTOCOL = TCP)(HOST = XYZ2-1.internal.xyz.org.uk)(PORT = 1522))

(ADDRESS = (PROTOCOL = TCP)(HOST = XYZ2-2.internal.xyz.org.uk)(PORT = 1522))

)

)

(CONNECT_DATA =

(SERVICE_NAME = XYZ)

(FAILOVER_MODE =

(TYPE = select)

(METHOD = basic)

(RETRIES = 16)

(DELAY = 1)

)

)

)

This is fine until you upgrade your Oracle client, or have multiple instances – if the wrong TNSNAMES gets picked up you’ll get error ORA-12514: TNS:could not resolve the connect identifier specified, and it can be fiddly to track down.

A neater way is to put the whole set of service details in the connection string, avoid the named identifier and circumvent TNSNAMES.ORA altogether:

<connectionStrings>

<add name=XYZ connectionString=Data Source=(DESCRIPTION =

(ADDRESS_LIST =

(LOAD_BALANCE = off)

(FAILOVER = on)

(ADDRESS_LIST =

(LOAD_BALANCE = on)

(FAILOVER = on)

(ADDRESS = (PROTOCOL = TCP)(HOST = XYZ1-1.internal.xyz.org.uk)(PORT = 1522))

(ADDRESS = (PROTOCOL = TCP)(HOST = XYZ1-2.internal.xyz.org.uk)(PORT = 1522))

)

(ADDRESS_LIST =

(LOAD_BALANCE = on)

(FAILOVER = on)

(ADDRESS = (PROTOCOL = TCP)(HOST = XYZ2-1.internal.xyz.org.uk)(PORT = 1522))

(ADDRESS = (PROTOCOL = TCP)(HOST = XYZ2-1.internal.xyz.org.uk)(PORT = 1522))

)

)

(CONNECT_DATA =

(SERVICE_NAME = UVASA)

(FAILOVER_MODE =

(TYPE = select)

(METHOD = basic)

(RETRIES = 16)

(DELAY = 1)

)

)

);User ID=xyz;Password=xyz;/>

</connectionStrings>

Service details contain the whole of the TNSNAMES entry following the identifier (i.e. from DESCRIPTION= onwards), and can span multiple lines in the config file.

Incidentally, if you’re not using ODP.NET in favour of the Microsoft’s framework Oracle provider, System.Data.OracleClient, consider migrating as it will be deprecated from .NET 4.0.

Generate sample request/response message from WSDL

For those of you well versed in the ways of wsdl, you might be able to do this by hand. But if you want a little help you can generate sample xml from the wsdl/xsd you get from a service’s metadata. Using Add Service Reference in Visual Studio creates this metadata for you. When you add your service reference, if you expand the files that you got you will see a couple of schemas.

Now if you have a xsd in BizTalk Generating an instance of a schema is a very basic functionality for all developers. I’m not sure how it is for the pure .NET developers though, but there are ways (of which this is one) that does not include BizTalk tools that allows you to generate xml for pieces of a schema.

In your service reference locate the schema you want (hint: in the above image that would be item.xsd) and open it with the XML Editor (you could just as well open any other schema, it need not come from a service reference in a project). Then go for View – XML Schema Explorer.

Right clicking on the node you want (your response) you can then choose to Generate Sample XML.

Now I make no claim as to having used this extensively – I’m a BizTalk developer, and as such I’ve got other tools at hand. I’m sure it’s got it’s flaws, but hey – you can always do it by hand if you aren’t happy with the help you get.

WCF Stateful Security Context Token and Service Accounts

I have spent some time investigating an issue relating to the configuration of WCF web services for SCT as described in this article and specific service accounts.

It turns out that when your web service is setup like this and you assign a specific service account rather than use the network service account or the new Application Pool Identities, you may get errors such as “The token provider cannot get tokens for target” or “Secure channel cannot be opened because security negotiation with the remote endpoint has failed. This may be due to absent or incorrectly specified EndpointIdentity in the EndpointAddress used to create the channel. Please verify the EndpointIdentity specified or implied by the EndpointAddress correctly identifies the remote endpoint” or “The request for security token has invalid or malformed elements”.

These errors are somewhat misleading as to the underlying cause. Instead of having anything to do with security negotiation and tokens per se, I deduced that it appears to be related to the service account not having a user profile on the server. In fact the article above does mention this in the following paragraph, although at first it isn’t an obvious link to the errors highlighted above.

For applications that use stateful SCTs in a secure session, the thread identity for the service must be a user account that has an associated user profile. When the service is run under an account that does not have a user profile, such as Local Service, an exception may be thrown.”

Anyway, in order to get around this you can either logon as the service account on the server as part of the deployment (probably not the best option) or change the “Load User Profile” option in the associated Application Pool to True. This will create the user profile for the service account the first time the service is instantiated. You can then change this option back to false if you wish.

I would be interested to know exactly what it is from the user profile for the service account that is required when the requireSecurityContextCancellation attribute is set to false, as firstly I would just like to know, and secondly I don’t believe either of the options above are elegant. If anyone can shed any light feel free to let me know.

Comparing Event Stream Processing and Rete

Alexandre Alves wrote an interesting article on CEP extensions for Logic Programming (see http://adcalves.wordpress.com/2009/10/30/logic-programming-lp-extensions-for-cep/) in which he mentions the use of Rete-based rules engines. I gave a presentation at a conference last week (October Rules Fest 2009) on the differences and similarities between various event stream processing (ESP) approaches and the Rete algorithm. I had a slide that is not very different in intent to Alexandre’s post. I also touched on performance issues, which is something he talks about briefly. I don’t know of any solid, well thought out, comparative benchmarking between any of the major ESP and CEP-enabled Rete engines, so I can only hazard informed guesses with regard to likely performance issues. I thought it would be useful to summarise some of my thinking in this area.

Read more at http://geekswithblogs.net/cyoung/archive/2009/11/04/136003.aspx

Speaking: CMAP Code Camp Fall 2009

I will presenting “A Lap Around Microsoft StreamInsight” at the Central Maryland Association of .NET Professional’s Fall 2009 Code Camp on November 7, 2009 at 8:45 a.m. I’m looking forward to seeing a lot of familiar faces in the Baltimore/DC Metro area.

The presentation is based upon the public CTP2 of StreamInsight, and the agenda is as follows:

  • Background and Context
  • Architecture and Technologies
  • Terminology
  • Demo: Traffic Sensors and the Object Model
  • Building StreamInsight Applications
  • Integration
  • Deployment
  • Monitoring
  • Demo: A Twitter event stream
  • Demo: Replaying historic data
  • Q&A

Currently the deck stands at around 50 slides and includes 3 demos, which should just fit in at an hour and fifteen minutes.

Bring your questions… there may just be a giveaway for the best question.

BizTalk: Restarting your BTS Hosts – Powershell Style

A while back I created a script that restarts your BizTalk Hosts – pretty simple,
here http://blogs.breezetraining.com.au/mickb/2006/10/04/SimpleScriptToRestartAllBizTalkServices.aspx
(also this script didn’t pick up your service if it was previously stopped – limitation
of the ‘sc query’ command)


Now with PowerShell it’s a one line job:
It goes something like this:
get-service BTS* | foreach-object -process {restart-service $_.Name}

You can also set all your BTS Services to start ‘automatic’ as follows:
get-service BTS* | foreach-object -process {set-service $_.Name -startuptype automatic}

(I’m actually trying to set the BTS Services to ‘Automatic (Delayed)’ but haven’t
been able to do that yet)

Enjoy,

Mick.