One of the cool new features in the Windows Azure Mix 09 CTP is the ability to run your Web and/or Worker Roles in full trust (non-admin). This opens up all kinds of features that weren’t available in the previous CTPs.
An interesting feature, which is the subject of this post is the ability to make PInvoke calls.
One of the things I have noticed in playing with calling native code is that you have to be more aware of the actual environment in the cloud. For example, Windows Azure in the cloud:
I also want to reiterate here that your Role Instances run in separate VMs and those VMs can come and go – it’s important not to count on any file system persistence in the VM.
1. Create a new Web Cloud Service project. File | New | Project. Select Visual C#/VB | Cloud Services | Web Cloud Services
2. Add a project to your solution for your native code. Right click on the Solution Explorer and select Add | New Project…
3. Select “Win32 Project” and call it “NativeCalc”
Setup the win32 project to be a DLL – we’ll just call a simple C function from it. Select “Export symbols”.
3. Open NativeCalc.h and change the dummy fnNativeCalc function to:
NATIVECALC_API int AddNumbers(int left, int right);
Note that the extern “C” specifies to use C linkage convention – what you see for the exported symbol if you used dumpbin – instead of the C++ decorations. This is what PInvoke will look for.
4. Now implement the method in NativeCalc.cpp
NATIVECALC_API int AddNumbers(int left, int right)
return left + right;
5. Next we have to ensure that this DLL is included as part of the Service Package. See this post for more details.
First I set the output directory of the DLL to be in the Web Role project directory.
I build all then right click on the nativeCloudService_WebRole project in Solution Explorer and select Add | Existing Item | NativeCalc.dll
I then set the CopyToOutputDirectory property to “CopyIfnewer”
6. Add a button and a label on default.aspx and in the button handler, set the label to the output of the native call:
static extern int AddNumbers(int left, int right);
protected void Button1_Click(object sender, EventArgs e)
Label1.Text = AddNumbers(2, 3).ToString();
7. Set the enableNativeExecution attribute on the WebRole in the ServiceDefinition.csdef
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="NativeCloudService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="WebRole" enableNativeCodeExecution="true">
8. Run the application and hit the button – everything will work as expected – you just called native code running on Windows Azure!
9. If you want to publish and deploy this to the Cloud – make sure that the native DLL is being built in release mode and is built for x64. See the troubleshooting section below for more details.
Small troubleshooting section:
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. Exception Details: System.Security.SecurityException: System.Security.Permissions.SecurityPermission
When trying in the Cloud:
Compiling for x64 in Visual Studio related:
If you weren't able to make it to MIX09, you can catch videos of the sessions.
My session is available for watching here: Using the Windows Azure Tools for Microsoft Visual Studio to Build Cloud Services
If you haven't seen the keynotes, I highly recommend that you see them, lots of awesome announcements and they are pretty darn entertaining too.
You can also catch the other Azure related sessions.
Here's the list of Windows Azure sessions:
Lots of great information available, hope you enjoy.
New release of the Windows Azure SDK and Tools!
From now on, you only have to download the Windows Azure Tools for Microsoft Visual Studio and the SDK will be installed as part of that package.
Windows Azure Tools for Microsoft Visual Studio (please give time for propagation or use the direct link to the download)
In a nutshell, you can now run managed code Full Trust and if you want to give fastCGI applications on Windows Azure a try, you can do that too!
Please install the following hotfixes:
What’s new in Windows Azure SDK
What’s new in Windows Azure Tools for Visual Studio
[For more recent information on using ASP.NET MVC with Windows Azure please see this post.]
ASP.Net MVC RC2 released a few days back and I've already gotten a number of requests to update the MVC on Windows Azure samples since they were getting a little stale.
I updated my posts on MVC and I also added more detail to the original "MVC on Windows Azure" post by making it a walkthrough -- hopefully it will be easy for you to duplicate the steps on your projects.
Also note that we've released a hotfix that will solve stability issues when using MVC with the Windows Azure Tools.
For those of you who are attending Mix '09, it'd be great if you could come check out my tools talk on Windows Azure: https://content.visitmix.com/2009/Speakers/ (click on "Azure")
MIX09-T81M Using the Windows Azure Tools for Microsoft Visual Studio to Build Cloud ServicesFriday March 20 |9:25 AM-9:45 AM | San Polo 3401By: Jim Nakashima Tags: ASP.NET | Azure Come hear how to use the Windows Azure Tools for Visual Studio to more easily create, build, debug, deploy, run and package scalable services on Windows Azure.
I really hope to see some of you there -- please come by to chat, I'd really like to get some first hand feedback about the Tools and Platform.
I should also mention, if you've seen Steve Marx talk, you know not to miss it, if you haven't, definitely check it out:
MIX09-T09F Building Web Applications with Windows AzureWednesday March 18 |4:00 PM-5:15 PM | San Polo 3504By: Steve Marx Tags: Azure Come learn how to use Windows Azure to build a scalable Web application and deploy it to the cloud.
Other Windows Azure sessions:
MIX09-T07F Overview of Windows AzureWednesday March 18 |2:15 PM-3:30 PM | San Polo 3504By: Manuvir Das Tags: Azure Curious about cloud computing? Come learn how to use Windows Azure to better address key challenges of running Internet-scale applications in the cloud. Also hear about the essential concepts of Windows Azure, including what's new.
MIX09-T38F See through the Clouds: Introduction to the Azure Services PlatformWednesday March 18 |11:30 AM-12:45 PM | San Polo 3504By: James Conrad Tags: Azure Come hear how Microsoft is building a new platform for applications, and learn about the key services that compose the platform as well as how to get started. Also hear Microsoft's roadmap for the Azure Services Platform and learn about new features that will be added.
MIX09-T08F Windows Azure StorageThursday March 19 |10:30 AM-11:45 AM | San Polo 3504By: Brad Calder Tags: Azure Come hear about the highly available and massively scalable cloud storage service that is provided by Windows Azure. Learn how to create and access the different types of Windows Azure storage available, including blobs, tables, and queues.
Update: We've refreshed the release and this new release resolves these issues. See here for more information.
We recently helped out a couple of folks on the Windows Azure Forum and felt it would be useful to post some information about two issues we’ve been seeing in the March 2009 CTP.
Issue: Visual Studio closes unexpectedly when launching Help | About or creating a Cloud Service in some non ENU Operating Systems.
Workaround: Modify (you might have to add it if it's not there) the value of the following key to "False":
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Cloud Computing Tools\Update Notification\AutomaticUpdatesEnabled="False"
Issue: if you specify the SQL instance to use for the Development Storage (through the DSInit command line utility in the SDK), the “Create Test Storage Tables” command will fail.
Workaround: Please replace the Microsoft.CloudService.targets file with the file attached to this blog post.
This is useful for people that wish to use a different SQL Server instance other than SQL Server Express.
You can find the Microsoft.CloudService.Targets file to replace in your C:\Program Files (x86)\MSBuild\Microsoft\Cloud Service\v1.0 directory.
Hope this helps, stay tuned for more information.