Question

I have a scenario where I need to be able to use the Microsoft.WindowsAzure.ServiceRuntime outside of a Web/Worker role.

Specifically I have the following code

public static RoleInstanceEndpoint ResolveIP()
{
    if (RoleEnvironment.IsAvailable)
    {
        RoleInstance instance = RoleEnvironment.CurrentRoleInstance;

        RoleInstance RelatedWCFInstance = RoleEnvironment.Roles["MyServiceRoleName"]
                                                            .Instances
                                                            .Where(o => o.UpdateDomain == instance.UpdateDomain)
                                                            .FirstOrDefault();

        if (RelatedWCFInstance != null)
            return RelatedWCFInstance.InstanceEndpoints.Where(o => o.Value.Protocol == "tcp").FirstOrDefault().Value;
    }

    return null;
}

This code successfully executes when running inside the RoleEntryPoint.OnStart event but when I try to execute this code in a separate exe that is triggered via Azure startup tasks like this

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

I receive the following error

The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception.

Could someone please confirm if it is infact possible to reference this library outside of a Web or Worker role? and if so provide any advice on what I might be doing wrong?

Was it helpful?

Solution

Checking the innerException resulted in showing the following message:

"Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information."

and the solution described in Mixed mode assembly is built against version ‘v2.0.50727′ of the runtime worked to resolve this problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top