我有一个场景,我需要能够在 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/