Friday, December 30, 2011

Allow Silverlight to access clipboard after having clicked “NO” before

Silverlight applications can put information into the computer’s clipboard. But only when the user grants the application access to it.

So while using Silverlight apps online you might have come across this dialog:

image

I accidentally clicked “no” once and had lots of trouble to reverse this step because I haven’t been prompted this question again.

Even google was no help in this case. Neither was re-installing Silverlight.

Finally a heroic search in the registry gave a solution. There is a place in the registry where Silverlight places the information about clipboard access.
HKEY_CURRENT_USER\Software\AppDataLow\Software\Microsoft\Silverlight\Permissions

Inside that Key is a subfolder for each Silverlight application that has prompted this question. If you delete the proper folder, you will be prompted again the next time.

image

Monday, November 14, 2011

Setting environment variables on Windows Azure: easy way

A few hours after writing the last post, I discovered that one can specify environment variables in the .csdef file:

<Runtime>
  <Environment>
    <Variable name="a" value="b" />
  </Environment>
</Runtime>

 


Don't know yet, if this does the same as my code in the last post. I might investigate on that...

Setting environment variables in Windows Azure

At the latest when you want to run java applications on Windows Azure you need to set several environment variables. Since there are startup tasks you may perform before your Role is being started, this obviously is the place where to set these variables. But there are some difficulties:

Setting environment variables Intentional-Style

Setting environment variables using the well known set command line tool works on Windows Azure like on premise:

set MYPATH=C:\Directory\

BUT:


  • This only works for one command shell session

  • This only works for the current user

The variables set should be available all time and for all users. So they need to be set as system variables. And system variables may only be set by an administrator.


Setting environment variables correctly



  1. Make sure your startup script is run with elevated permissions.
    Check your .csdef file to look similar to this:
    <Startup>
      <Task commandLine="Startup.cmd" 
            executionContext="elevated" 
            taskType="simple" />
    </Startup>

  2. Use the setx command line tool with the /M parameter to set system variables:
    setx MYPATH C:\Directory\ /M

  3. If you need the variables set to use them in your startup script, you need to set them with the set command again.
    - set works for the current session (the startup script)
    - setx
    works for all session started after setting the variables

References:

How to use the setx command: http://ss64.com/nt/setx.html

Tuesday, October 25, 2011

Sending e-mails using IIS SMTP Server on Windows Azure

If you want to send emails from a Windows Azure Role, there’s the possibility to use the built-in SMTP Server of IIS 6.0. Here is a guide how to use startup scripts and PowerShell commands to set up a SMTP server in the cloud.

Before you start make sure that you’re running a Windows 2008 Server R2 in the cloud by setting the osFamily Attribute to osFamily=2 in the ServiceConfiguration.cscfg file.

Setup the SMTP

Here is a guide that shows how to set up the SMTP server manually. Those are the same steps we’re going to do here using scripts. It helped me to understand what’s necessary to do. Just deploy any Azure Project to the Cloud, RDP in, install the SMTP-Server feature and do the steps described in that guide.

But now to the automated setup scripts:

1. Create a new startup task in the ServiceConfiguration.cscfg

<Startup>
  <Task commandLine="Startup.cmd" executionContext="elevated" taskType="simple" />
</Startup>

2. The startup file first needs to install the SMTP-Server feature automatically. To gain this it uses the PowerShell 2.0. After that it calls a VBScript that configures the SMTP-Server.


The Startup.cmd:

powershell -command "Set-ExecutionPolicy Unrestricted"

powershell .\InstallSmtp.ps1

cscript ConfigSmtp.vbs


The InstallSmtp.ps1 PowerShell script:

Import-Module Servermanager
Add-WindowsFeature SMTP-Server

3. Now we need to create the VBScript that configures the SMTP server: We need to add all IP addresses that are allowed to send mails through this SMTP server to the grant list. Assuming only applications running on the same server are allowed to send emails we’re going to add 127.0.0.1 to that list.


The ConfigSmtp.vbs

Option Explicit
Dim smtpServer, relayIpList
// Get the default instance of the SMTP server
Set smtpServer = GetObject("IIS://localhost/smtpsvc/1")
// Get the IPList
Set relayIpList = smtpServer.Get("RelayIpList")
// Add localhost to that list
relayIpList.GrantByDefault = false
relayIpList.IpGrant = "127.0.0.1"
// Save changes
smtpServer.SetInfo

4. Deploy to the cloud


Use the SMTP from C# code


To use the local SMTP server from within your C# code use the following lines:

var client = new System.Net.Mail.SmtpClient("localhost");
client.Send("from@domain.tld",
            "to@domain.tld",
            "This is my subject",
            "Hello, this is a mail from the cloud!");

Don’t get blacklisted


Even Steve Marx recommended not to use the SMTP server feature on Windows Azure instances because they soon would be blacklisted. To avoid getting on a blacklist you could use a smart host to deliver your emails with.


If you want to use a smart host in your deployment, you need to extend the ConfigSmtp.vbs:

// set the outbound connector to a smart host
smtpServer.SmartHostType = 2
smtpServer.SmartHost = "smtp.mysmarthost.tld"
// use basic authentication
smtpServer.RouteAction = 264
smtpServer.RouteUserName = "myName"
smtpServer.RoutePassword = "myPassword"
// save changes
smtpServer.SetInfo

In this case I have been using the SMTP relay services offered by http://dyn.com which worked just fine. Depending on what service you’re about to use, the settings might differ.



References


Monday, October 24, 2011

Installing updates manually on Windows Azure

While trying to manually install the PowerShell 2.0 update on a Windows Azure Role I ran into the following error message:

“Installer encountered an error: 0x80070422”

image

In order to solve this problem:

  1. Open the Server Manager: Start > Right Click on “Computer” > Manage
  2. Navigate to Configuration > Services
  3. Enable the Background Intelligent Transfer Service  and start it
  4. Enable the Windows Update Service and start it
  5. Now you can manually install updates.

I Got this hint from: Came Too Far

Sunday, September 25, 2011

How To: Look inside the Service Package file

If you ever wanted to see what’s inside a published .cspkg file: here is a small guide how to achieve this…

First there is to say that a .cspkg-file is only a ZIP file with extensions changed. But the contents are encrypted. But there is a way to create unencrypted Service Packages:

  1. Close Visual Studio
  2. Shut down Windows Azure Compute Emulator
  3. Go to Control Panel > System and Maintenance > System
  4. Click on “Advanced Settings” in the pane on the left
  5. Switch to the “Advanced” tab and click on the “Environment Variables” Button.
    The “Environment Variables” dialog appears.
  6. Check for a System Variable named _CSPACK_FORCE_NOENCRYPT_
    If there is no such variable, create it.
  7. Set the value of the _CSPACK_FORCE_NOENCRYPT_ variable to “true”
    image_thumb_4
  8. Start Visual Studio again and publish a Cloud Service Project.

Note: When publishing without encryption, Visual Studio warns you with the following message in the output window
CloudServices44 : Forcing creation of unencrypted package...

To sneak inside the package do the following steps:

  1. Rename your <cloudservice>.cspkg file to <cloudservice>.zip
  2. Unpack that .zip folder
  3. Inside the uncompressed folder there is a file named after your web role <WebRole><Guid>.cssx
  4. Rename the .cssx file to .zip
  5. Since you performed the steps before, this .zip file is unencrypted now. Unzip it.
  6. Find the content of your package in the “sitesroot” folder.

Caution: Although either encrypted and unencrypted packages can be deployed to the cloud, it’s highly recommended to only use encrypted packages for security issues.

Issue: change physical path of site named “Web”

In earlier posts I showed how to use multiple sites within a single WebRole. This required to use the physicalDirectory attribute within the ServiceDefinition file. If you tried to use this attribute you might have run into this issue:

The default site in a WebRole is named “Web”. When you set the physicalDirectory attribute to point to any location – no change will happen.

It seems as if a site named “Web” ignores the physicalDirectory attribute.

But if you change the name to any other name: the physicalDirectory attribute will be used!

For further explanation have a look at the following xml snippets from the .csdef file:

<Site name="Web">
  <Bindings>
    <Binding name="Endpoint1" endpointName="Endpoint1" />
  </Bindings>
</Site>
This will use the default content.
<Site name="Web" physicalDirectory="C:\Code\WebApplication2">
  <Bindings>
    <Binding name="Endpoint1" endpointName="Endpoint1" />
  </Bindings>
</Site>
This will still use the default content.
<Site name="Web1" physicalDirectory="C:\Code\WebApplication2">
  <Bindings>
    <Binding name="Endpoint1" endpointName="Endpoint1" />
  </Bindings>
</Site>
This will use content from WebApplication2.