问题描述
我的Web应用程序(WCF服务)使用 SqlRoleProvider
,它工作正常 Visual Studio开发服务器上。它切换到 IIS8防爆preSS 会导致它抛出一个的NullReferenceException
虽然
Roles.IsUserInRole(用户名,角色)//他们既不实际空
我无法找到一个暗示了此异常的。切换回Visual Studio开发服务器也可以正常工作。这是什么异常的原因,我怎么可以修复它是否正确?该项目的目标框架的是的.NET Framework 4 的
下面是配置的连接字符串:
<添加名称=的ConnectionString
的connectionString =数据源= \\ sqlex preSS;初始目录= DevWeb;集成安全性=真
的providerName =System.Data.SqlClient的/>
和这是 roleManager /提供商
节点:
<清/>
<添加的connectionStringName =的ConnectionString的applicationName =MyApp的NAME =AspNetSqlRoleProviderTYPE =System.Web.Security.SqlRoleProvider/>
什么弗拉基米尔说的是真的,但实际上并没有解释发生了什么。是什么引发的NullReferenceException是EtwTrace code这是在和的isUserInRole GetRolesForUser。一切都在角色类别的账户的事实,HttpContext.Current可以为null。我发现这个由Microsoft参考源寻找NET,4.5版在
在所有其他的测试环境中我试过,跟踪级别不足以引发NullReferenceException异常。只有当我用IIS防爆preSS 8安装Visual Studio 2013,我看到的问题,只有在IIS防爆preSS后。
你能做些什么呢?
一种选择是让ASP.Net兼容模式为WCF。
首先,在web.config中,添加属性aspNetCompatibilityEnabled =true添加到节点<结构>< system.serviceModel>< serviceHostingEnvironment取代。然后通过添加属性选择服务类到行为[System.ServiceModel.Activation.AspNetCompatibilityRequirements(RequirementsMode = System.ServiceModel.Activation.AspNetCompatibilityRequirementsMode.Allowed)对类definition.With启用此功能,HttpContext.Current将填充你的时间去从角色类角色的要求,假设你不工作在后台线程像弗拉基米尔提到的(在这种情况下,你需要修补的HttpContext.Current第一)。您可以在。
另一种选择是绕过角色类这两种方法。而不是调用Roles.IsUserInRole的,叫Roles.Provider.IsUserInRole。而不是调用Roles.GetRolesForUser的,叫Roles.Provider.GetRolesForUser。每种方法都有相同的重载可用。你失去了跟踪站和本地缓存的角色,但你绕过空引用异常。
My web application (a WCF service) uses SqlRoleProvider
, which works fine on Visual Studio Development Server. Switching it to IIS8 Express causes it to throw a NullReferenceException
though:
Roles.IsUserInRole(username, role) // neither of them actually null
I could not find a hint for this exception in the IsUserInRole
method documentation. Switching back to Visual Studio Development server makes it work. What is the cause of this exception, and how could I fix it properly? The project's target framework is .NET Framework 4.
Here is the configured connection string:
<add name="ConnectionString"
connectionString="Data Source=.\sqlexpress;Initial Catalog=DevWeb;Integrated Security=True"
providerName="System.Data.SqlClient" />
And this is the roleManager/providers
node:
<clear />
<add connectionStringName="ConnectionString" applicationName="MyApp" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"/>
What Vladimir said is true but doesn't actually explain what is happening. What throws the NullReferenceException is the EtwTrace code that's in IsUserInRole and GetRolesForUser. Everything else in the Roles class accounts for the fact that HttpContext.Current can be null. I found this by looking in the Microsoft Reference Source for "NET, Version 4.5" at http://referencesource.microsoft.com/netframework.aspx
In every other test environment I tried, the tracing level was not sufficient to trigger the NullReferenceException. It was only when I used IIS Express 8 after installing Visual Studio 2013 that I saw the issue and only in IIS Express.
What can you do about it?
One option is to enable "ASP.Net Compatibility Mode" for WCF.First, in the web.config, add the attribute aspNetCompatibilityEnabled="true" to the node <configuration><system.serviceModel><serviceHostingEnvironment>. Then opt your service class into the behavior by adding the attribute [System.ServiceModel.Activation.AspNetCompatibilityRequirements(RequirementsMode = System.ServiceModel.Activation.AspNetCompatibilityRequirementsMode.Allowed)] to the class definition.With this functionality enabled, HttpContext.Current will be populated by the time you go to request roles from the Roles class, assuming you're not working on a background thread like Vladimir mentioned (in which case you'll need to patch up the HttpContext.Current first). You can read more about ASP.NET Compatibility Mode for WCF at http://blogs.msdn.com/b/wenlong/archive/2006/01/23/516041.aspx.
The other option is to bypass the Roles class for these two methods. Instead of calling Roles.IsUserInRole, call Roles.Provider.IsUserInRole. Instead of calling Roles.GetRolesForUser, call Roles.Provider.GetRolesForUser. Each method has the same overloads available. You lose the trace stops and the local role cache but you bypass the null reference exception.
这篇关于SqlRoleProvider上IIS8防爆preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!