سؤال

I'm writing a worker role for sending emails. It has some email template html files with build action = content and copy to output = Copy Always

How can i access those files from the code of the WorkerRole?

I don't want to store those files in a blob because i need to upload this service as soon as possible and i have to be able to edit those email template easily without wiring up extra code for that.

EDIT: Guys i'm talking about Azure. This doesn't work with the regular method of loading the Folder of the current running assembly because that would give you the Azure host process which is located in a different place.

هل كانت مفيدة؟

المحلول

I figured it out - here's how to do it:

Path.Combine(Environment.GetEnvironmentVariable("RoleRoot") + @"\", @"approot\FileTemplates\");

نصائح أخرى

Having a build action - content does not embed the file into the .dll, it is deployed into your assembly directory (usually \bin), then in the same folder structure that you have the template in. That was confusing to write, so here is an example:

Project Directory    

    -Templates

       -EmailTemplateA

When complied and deployed, EmailTemplateA would be in the following location: \bin\Templates\EmailTemplateA

Now that we know where it is at, we need to use it. Below is a code snippet that would load a template, replace some values and then send your email

public void SendRegistrationConfirmation(string toAddress, string confirmUrl)
{
   const string subject = "Your Registration";

   //load the template
   var template = File.OpenText(AssemblyDirectory + " \\Templates\\NewProgramRegistration.Template").ReadToEnd();

   //replace content in the template
   //We have this #URL# string in the places we want to actually put the URL
   var emailContent = template.Replace("#URL#", confirmUrl);

   //Just a helper that actually sends the email, configures the server, etc
   this.SendEmail(toAddress, subject, emailContent);
}

You could store your templates in Local Storage:

http://convective.wordpress.com/2009/05/09/local-storage-on-windows-azure/

You should reconsider you design. - as mentioned above, you can use Local Storage (you would need to copy the files there first) and then you can use the System.IO .NET API to manipulate the files

The problem with this is (as you said you are making changes) - what if you horizontally scale, now you have individual worker roles that have their own local copy of the "template". You mention not wanting to use blob storage, but that should be an option since it can act as a central/persisted repository for your templates. And you can copy them locally of course as needed.

Another option is to use SQL Azure DB. Something like templates, should be super fast on it that multiple roles can share.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top