Scott Seely
This user hasn't shared any biographical information
Homepage: https://scottseely.wordpress.com
Moved hosts, so my blog is back
Posted in Uncategorized on November 30, 2009
I was unusually quiet over the last couple of weeks. Why? My former host apparently packed too many sites on one machine and caused this page to experience OutOfMemoryExceptions galore. I run a number of sites for various purposes and decided it was time to move up to a VM host. I’m currently on MaximumASP and am using a MaxV server. Setup was pretty simple, support was helpful for a few of my questions, and I’m happy to have my blog engine up and running again.
Slides and Code from Chicago Alt.NET meeting
Posted in Uncategorized on November 12, 2009
I want to say thanks to everyone who came out to watch me speak about OpenSocial at the Chicago Alt.NET meeting. Thanks for inviting me to present and for making the time so enjoyable. I’ve posted the slides and the code for the Canvas page here.
Speaking at Chicago Alt.Net Meeting
Posted in Uncategorized on November 9, 2009
This month, I’ll be speaking at the Chicago Alt.Net user group meeting. Please check out the details here and register here. And, here is the blurb on the talk:
November 2009 Meeting
Building OpenSocial Applications
6:00 pm
Pizza and networking time
6:30 pm
From its official web site:
Friends are fun, but they’re only on some websites. OpenSocial helps these sites share their social data with the web. Applications that use the OpenSocial APIs can be embedded within a social network itself, or access a site’s social data from anywhere on the web.
OpenSocial is the platform that MySpace, Orkut, Ning, LinkedIn, Hi5, and pretty much every social network but Facebook supports for creating games and other applications that run on these social network sites.
In this talk, we focus on the MySpace platform and how one builds a MySpace application. This involves interacting with the OpenSocial JavaScript as well as receiving and sending OpenSocial requests on your own servers.
Iowa Code Camp November 2009 Slides Up
Posted in Uncategorized on November 7, 2009
I want to send out a big thank you to the team who put together the Iowa Code Camp. You people did an awesome job!!! I had a great time giving my talks and really enjoyed hanging out with the crowd in Iowa.
For those of you who attended my talks, or just want to see the materials, I’ve posted things.
WCF Diagnostics Talk and Materials
See you next year.
Speaking at nPlus1 ArcSummit- Chicago
Posted in Uncategorized on November 2, 2009
On December 7, I’ll be speaking at the nPlus1 ArcSummit for the optional morning session. I’d love to see the place packed! Here are the details:
https://www.clicktoattend.com/invitation.aspx?code=142763
About nPlus1.org
nPlus1.org is a site dedicated to helping Architects, aspiring Architects and Lead Developers learn, connect and contribute. On this site you’ll have access to great first party content written by some of the most skilled and experienced Architects working today. You’ll also have access to, and be able to contribute to a nexus of content from around the Internet aimed at keeping Architects up to date on all the new developments in their fields of interest.
When
Monday December 7, 2009 – 10:00PM to 5:00PM
Where
Microsoft MTC – Aon Center
200 E. Randolph
Suite 200
Chicago, IL 60601
Free Lunch Provided
Agenda
Morning Session (Optional): An Introduction to Object Oriented Programming
10:00 AM – 12:00 PM
Are you new to OOP? Do you want a refresher on the benefits of Interfaces and the differences between implements and extends? The morning session is a two hour introductory course of Object Oriented Programming. If you are new to OOP the lessons in this session will prepare you for the more advanced topics in the afternoon.
If you are already well versed in OOP then feel free to come have a refresher, or simply join us for lunch and the advanced sessions in the afternoon. The morning session is completely optional.
Afternoon sessions:
Session One: Software Patterns
Patterns are an important tool to use as architects and developers. They provide a common vocabulary for us to design with, as well as a common approach to a common problem. Come learn about useful patterns, and how to use them in your everyday code.
Session Two: How I Learned To Love Dependency Injection
Dependency Injection is one of those scary topics that most developers avoid. It sounds all ‘high-falootin’ and complex. It’s not. Really. We wouldn’t lie. It’s a great way to manage complexity in your system, and a great way to make your system so much more testable. And isn’t that what we all want?
Each session will be followed by open discussions periods.
A catered lunch will be provided starting at noon. This will divide the morning introductory sessions from the advanced sessions. Register once for all session and choose to attend the morning, the afternoon or both! Lunch is provided for attendees for any of the sessions.
Lambdas aren’t just for managed code
Posted in Uncategorized on October 21, 2009
A long time ago, I was a C++ developer. I actually thought of myself as a pretty darn good C++ developer and got way too excited when I finally got to meet folks like Scott Meyers and actually landed a job working with Bobby Schmidt-same team on MSDN. (If you know Bobby’s name, well, you were pretty deep into C++ circa 1999.) Then, like many C++ devs, I moved over to a garbage collected language. I’ve done more than my fair share of professional .NET and Java development. C++ has been left by the wayside. Today, I finally downloaded and installed Visual Studio 2010 Beta 2. I dug into the “What’s New” and, out of curiosity, decided to look at C++ first (I already know about many of the C# and VB changes). I saw two cool things:
1. C++ now has an auto keyword. For you C# devs, it is pretty much the same as the C# var keyword.
2. C++ has lambdas.
Now, I’m only a little ways into understanding C++ lambdas, so some of my information here may be suspect. The C++ lambda is just an anonymous function. You can pass variables into the function so that the variable is visible within the function. The syntax is:
[ list of variables from current scope to pass into lambda scope ] return-type( function signature ) { code }
The canonical example appears to be std::for_each from <algorithm>.
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> myints;
for (auto i = 0; i < 10; ++i){
myints.push_back(i);
}
auto number = 0;
auto numSquared = 0;
for_each(myints.begin(), myints.end(), [&number, &numSquared](int n) {
cout << n << endl;
number += n;
numSquared += n * n;
});
cout << "The sum is " << number << endl;
cout << "The sum of the squares is " << numSquared << endl;
return 0;
}
In this example, I have two values: number and numSquared. I make references of the value visible to the lambda, which prints out each value as it comes through and then sums the numbers and their squares. Once the function completes, the code emits the values to the console window. If you don’t want to pass anything to the lambda, you still need to state this. If I didn’t want number or numSquared visible, the lambda signature would be:
[](int n){}
At some point, I’ll dig in and see what else this is good for, including how to build my own Functor signatures. Anyhow, just thought I’d share this little nugget with you.
New F#/WCF Article up at InformIT
Posted in Uncategorized on September 30, 2009
My article on programming REST services with F# and WCF went up at InformIT. Please go read it! http://www.informit.com/articles/article.aspx?p=1394625
Quiet lately
Posted in Uncategorized on September 21, 2009
Things have been quiet around here lately because of a large volume of stuff I’ve been handling elsewhere. I recently was forwarded a nice review of the REST book. Feel free to take a gander and see if this pushes you to buy the book!
Solution for Daily WTF Praxis on 8-5-2009
Posted in Uncategorized on August 5, 2009
Over at The Daily WTF, they’ve started posting programming puzzles. The latest one is a puzzle where a set of locker doors are toggled, starting from the closed state, to the open state. Starting at a step size of 1 and stopping at a step size equal to the number of lockers, one toggles each door. At a step size of 1, all doors are opened, step size of 2, all the even doors are closed. At a step size of 3, door 3 is closed, door 6 opened, and so on.
Many of the solutions observe that only perfect squares remain opened when the algorithm is complete. That option is easy, but I wanted to do one that mimics the jocks effort of brute force solving the problem.
// Init the lockers to the initial state
// and mark all doors as closed (false)
let sw = System.Diagnostics.Stopwatch.StartNew()
let totalLockers = 100
let lockers =
[1..totalLockers]
|> Seq.map( fun e -> (e, false))
// Recursive function to set each door.
let rec openLockers l skip =
let listLength = Seq.length(l)
if (totalLockers < skip) then
// We’ve done our last toggle on the previous iteration.
l
else
// Do the toggling and try again.
let newList =
l |> Seq.map(
fun (e, f : System.Boolean) ->
let modValue = (e % skip)
match modValue with
| 0 -> (e, not(f))
| _ -> (e, f))
openLockers newList (skip + 1)
let remainingOpen =
(openLockers lockers 1) |>
Seq.filter(fun (e, f:System.Boolean) -> f) |>
Seq.map(fun (e, f:System.Boolean) -> e)
sw.Stop()
Seq.iter (printf "%dn") remainingOpen
printf ("%sn") (sw.Elapsed.ToString())
Calling an STA COM Object from a WCF Operation
Posted in Uncategorized on July 17, 2009
One of the things that many people are still doing is making use of old COM objects that run in STA (single threaded apartment) threads. Back in October, 2006, Jeff Prosise wrote how to do this from ASMX. Not too long after that, I had a chance to teach for Wintellect and Jeff asked me to show him how to do the same in WCF. That information went up on a post for a consulting company that didn’t make it through the latest recession. For better or worse, that post was referenced a fair number of times and now, folks are writing to me, asking for the code again.
In general, any time you receive a message via WCF, the message itself will be processed on a MTA (multi-threaded apartment) thread. Normally, this is just fine. Sometimes, you might be calling out to a COM object. COM objects will not run if the threading model the COM object needs differs from the threading model of the calling thread. MTA COM objects work just fine. But, if you have a bunch of STA COM objects (typically produced by Visual Basic but sometimes coming from C++ applications or other utilities) that you use in your WCF service, you have a problem. To allow things to work, the method needs to be invoked on an STA thread.
In .NET, one creates an STA thread by setting the apartment state of the thread prior to Start()-ing the thread.
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Now, how do we make this happen in WCF? We need to wrap the invocation of the actual method! For this, we resort to an IOperationBehavior. One typically applies a behavior (IServiceBehavior, IContractBehavior, IOperationBehavior) via an attribute. The only exception to this is an IEndpointBehavior, which is applied via manipulation of the EndpointDescription or in the app|web.config file. The IOperationBehavior exposes four methods:
- AddBindingParameters(OperationDescription, BindingParameterCollection): In our case, we will just do nothing with this info.
- ApplyClientBehavior(OperationDescription, ClientOperation): Only called when the contract is used on a client. We will do nothing here since we only care about the service implementation.
- ApplyDispatchBehavior(OperationDescription, DispatchOperation): Only called when the contract is used on the server. We will do our work here and override the IOperationInvoker on the DispatchOperation so that our operation is invoked on an STA thread.
- Validate(OperationDescription): Used to make sure that everything is OK before applying the behavior. Normally, one throws an exception from this method if the environment isn’t right in some way. Our implementation will throw if the method we are calling is being executed asynchronously. If you need an async version of the attribute, that work is left as an exercise for you, dear reader.
Our IOperationBehavior then looks like this:
public class STAOperationBehaviorAttribute: Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription,
System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription,
System.ServiceModel.Dispatcher.ClientOperation clientOperation)
{
// If this is applied on the client, well, it just doesn’t make sense.
// Don’t throw in case this attribute was applied on the contract
// instead of the implementation.
}
public void ApplyDispatchBehavior(OperationDescription operationDescription,
System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
{
// Change the IOperationInvoker for this operation.
dispatchOperation.Invoker = new STAOperationInvoker(dispatchOperation.Invoker);
}
public void Validate(OperationDescription operationDescription)
{
if (operationDescription.SyncMethod == null)
{
throw new InvalidOperationException("The STAOperationBehaviorAttribute " +
"only works for synchronous method invocations.");
}
}
}
So far, so good. Now, we need to write that IOperationInvoker. You’ll note from the ApplyDispatchBehavior above that our STAOperationInvoker takes an IOperationInvoker in the constructor. This is done because, in general, the IOperationInvoker WCF gives us does everything right. It just needs to do its thing on an STA thread. Our implementation will delegate as much work as possible to the provided IOperationInvoker. This is a pattern you will follow in most WCF extensions as most extensions require a slight tweak to existing behavior. We do just that, except for our implementation of Invoke. In that, we will setup an STA thread and then call the contained IOperationInvoker’s Invoke method from the STA thread.
public class STAOperationInvoker : IOperationInvoker
{
IOperationInvoker _innerInvoker;
public STAOperationInvoker(IOperationInvoker invoker){
_innerInvoker = invoker;
}
public object[] AllocateInputs()
{
return _innerInvoker.AllocateInputs();
}
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
// Create a new, STA thread
object[] staOutputs = null;
object retval = null;
Thread thread = new Thread(
delegate(){
retval = _innerInvoker.Invoke(instance, inputs, out staOutputs);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
outputs = staOutputs;
return retval;
}
public IAsyncResult InvokeBegin(object instance, object[] inputs,
AsyncCallback callback, object state)
{
// We don’t handle async…
throw new NotImplementedException();
}
public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
{
// We don’t handle async…
throw new NotImplementedException();
}
public bool IsSynchronous
{
get { return true; }
}
}
To test, I wrote the following Service Contract:
[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetApartmentTypeMTA();
[OperationContract]
string GetApartmentTypeSTA();
}
and applied the attribute to this implementation:
class TestService : ITestService
{
public string GetApartmentTypeMTA()
{
return Thread.CurrentThread.GetApartmentState().ToString();
}
[STAOperationBehavior]
public string GetApartmentTypeSTA()
{
return Thread.CurrentThread.GetApartmentState().ToString();
}
}
When calling the service, GetApartmentTypeMTA always returns MTA and GetApartmentTypeSTA always returns STA.
If you want to save some typing time or see the sample application, go here.
You must be logged in to post a comment.