c# - 在 Web 或辅助角色之外引用 Microsoft.WindowsAzure.ServiceRuntime

标签 c# azure dll azure-role-environment

我有一个场景,我需要能够在 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 事件内运行时成功执行,但当我尝试在通过 Azure 启动任务触发的单独 exe 中执行此代码时像这样

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

我收到以下错误

“Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment”的类型初始值设定项引发异常。

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

最佳答案

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

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

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

关于c# - 在 Web 或辅助角色之外引用 Microsoft.WindowsAzure.ServiceRuntime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18340907/

相关文章:

使用应用程序设置 json 的 Azure Function 2.0

windows - 如果加载它的 DLL 被卸载,DLL 是否会被删除?

python - 导入 Dash : "ImportError: DLL load failed while importing _brotli: The specified module could not be found." 时出错

c - 纯 C.Dll 导出

CSS 文件中的 C#? (ASP.NET MVC 5)

c# - 匹配以随机内容开头的可选模式

c# - 1 综合计时器 vs. 多个计时器,每个对象一个计时器

c# - 如何添加到 Visual Studio 错误列表窗口中的消息列表?

c# - 在 Azure 中扩展应用程序服务时,是否会在所有实例上处理单个消息(来自队列)?

azure - 在装有 VS2012 但没有 VS2010 的干净计算机上,具有多个角色实例的 Windows Azure 计算模拟器 (SDK 1.8) 的奇怪行为