In a .NET 4.0 project, I'm experiencing that a call to System.Web.Security.Roles.RoleExists
within a System.Transactions.TransactionScope
fails due to MSDTC not being enabled on my development machine. The role manager's data source is LocalDB and the provider is of type System.Web.Providers.DefaultRoleProvider
. I've installed NuGet package Microsoft.AspNet.Providers.LocalDB
, which I believe supplies the role manager's provider.
What is it that's causing MSDTC to be required?
Code
The failing call:
using (var ts = new TransactionScope())
{
Roles.RoleExists("Administrator");
}
The relevant configuration sections from web.config:
<connectionStrings>
<clear />
<add name="MembershipConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\mydb.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
<providers>
<remove name="AspNetSqlRoleProvider" />
<add connectionStringName="MembershipConnection" applicationName="/" name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</providers>
</roleManager>
Test project
I've created a test ASP.NET MVC 4 Internet application that demonstrates the issue. Download the TestBundles Visual Studio 2012 solution, debug the app and click the 'Log in' link. As I've made the Login
action try to validate a role, you should get an exception unless you have enabled MSDTC on your machine.
public ActionResult Login(string returnUrl)
{
using (var ts = new TransactionScope())
{
Roles.RoleExists("Administrator");
}
ViewBag.ReturnUrl = returnUrl;
return View();
}