SignalR 使用 Azure Web 角色进行了惊人的扩展。但是,当我在 Azure Worker Role 中使用自托管 OWIN 项目时,SignalR 将在添加多个实例时开始出现问题。作为记录,我的项目使用 Redis 作为背板。
将 Azure Worker Role 实例增加到一个以上时,客户端连接将随机失败,并显示错误"The ConnectionId is in the wrong format"。我相信这是由于负载平衡导致单个客户端的协商跨越多个服务器时引起的;我不相信参与协商的多个服务器可以解密数据(DPAPI 在幕后?)。
我尝试在 app.config 中设置 < machineKey /> validationKey 和 decryptionKey 但这似乎没有什么区别;问题依然存在。同样,该项目可以作为 Web 角色 (IIS) 正常工作,但不能作为工作角色(OWIN 自托管)。
假设这是 DpapiDataProtectionProvider 的问题,我如何确保提供程序在多个服务器/实例之间呈现相同的加密/解密结果?
解决方案
SignalR (DpapiDataProtectionProvider) 使用的默认保护提供程序似乎不支持 Azure 辅助角色扩展。通过滚动我自己的示例提供程序,我能够扩展 SignalR/OWIN/Azure Worker,并且不会收到随机的 400 HTTP/“ConnectionId 格式不正确”。请记住,以下示例不会保护/保护令牌。
public class ExampleDataProvider : IDataProtector
{
public byte[] Protect(byte[] userData)
{
Trace.TraceInformation("Protect called: " + Convert.ToBase64String(userData));
return userData;
}
public byte[] Unprotect(byte[] protectedData)
{
Trace.TraceInformation("Unprotect called: " + Convert.ToBase64String(protectedData));
return protectedData;
}
}
public class ExampleProtectionProvider : IDataProtectionProvider
{
public IDataProtector Create(params string[] purposes)
{
Trace.TraceInformation("Example Protection Provider Created");
return new ExampleDataProvider();
}
}
在 Owin 启动期间注入自定义提供程序:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.SetDataProtectionProvider(new ExampleProtectionProvider());
GlobalHost.DependencyResolver.UseRedis(new RedisScaleoutConfiguration("0.0.0.0", 0, "", "Example"));
app.MapSignalR(new HubConfiguration
{
EnableDetailedErrors = true
});
app.UseWelcomePage("/");
}
}