【问题标题】:Reference Microsoft.WindowsAzure.ServiceRuntime outside a Web or Worker role在 Web 或 Worker 角色之外引用 Microsoft.WindowsAzure.ServiceRuntime
【发布时间】:2013-08-22 20:23:54
【问题描述】:

我有一个场景,我需要能够在 Web/Worker 角色之外使用 Microsoft.WindowsAzure.ServiceRuntime

具体我有以下代码

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;
}

此代码在 RoleEntryPoint.OnStart 事件中运行时成功执行,但是当我尝试在一个单独的 exe 中执行此代码时,通过 Azure 启动任务触发 像这样

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

我收到以下错误

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

有人可以确认是否可以在 Web 或 Worker 角色之外引用此库?如果是这样,请就我可能做错的地方提供任何建议?

【问题讨论】:

  • 什么是 InnerException?
  • 好问题,给我一分钟重新部署和检查...
  • 干得好! “混合模式程序集是针对运行时版本‘v2.0.50727’构建的,如果没有额外的配置信息,则无法在 4.0 运行时中加载。”
  • 所以我接受它,它只是这样做的问题吗? stackoverflow.com/questions/6425707/… 并将其部署到我的项目中
  • 是的,引用的问题一定能解决你的问题!

标签: c# azure dll azure-role-environment


【解决方案1】:

检查 innerException 导致显示以下消息:

“混合模式程序集是针对运行时版本‘v2.0.50727’构建的,如果没有额外的配置信息,则无法在 4.0 运行时中加载。”

Mixed mode assembly is built against version ‘v2.0.50727′ of the runtime 中描述的解决方案可以解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    相关资源
    最近更新 更多