godaddy上使用MySQL和实体框架的安全异常

godaddy上使用MySQL和实体框架的安全异常,mysql,entity,shared-hosting,securityexception,Mysql,Entity,Shared Hosting,Securityexception,在godaddy上使用实体框架时,我们得到了一个SecurityException。该实体已针对MySQL存储配置。(6.1.2节)有点奇怪,但有个例外。。。从异常堆栈来看,这似乎意味着如果我们在站点的任何地方打开到MySQL的连接,那么我们应该得到相同的异常;然而,直接打开MySQL连接似乎在站点的另一部分起作用 以下是验证: using (MySqlConnection connection = new MySqlConnection(ConnectionString)) { conne

在godaddy上使用实体框架时,我们得到了一个
SecurityException
。该实体已针对MySQL存储配置。(6.1.2节)有点奇怪,但有个例外。。。从异常堆栈来看,这似乎意味着如果我们在站点的任何地方打开到MySQL的连接,那么我们应该得到相同的异常;然而,直接打开MySQL连接似乎在站点的另一部分起作用

以下是验证:

using (MySqlConnection connection = new MySqlConnection(ConnectionString))
{
  connection.Open();
  ...
}
有人遇到过类似的问题吗

完整的错误堆栈跟踪如下所示:

[SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Reflection.MethodBase.PerformSecurityCheck(Object obj, RuntimeMethodHandle method, IntPtr parent, UInt32 invocationFlags) +0
   System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +470
   System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +1051
   System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +111
   System.Resources.ResourceManager.CreateResourceSet(Stream store, Assembly assembly) +357
   System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) +471
   System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) +583
   System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) +583
   System.Resources.ResourceManager.GetString(String name, CultureInfo culture) +74
   MySql.Data.MySqlClient.Resources.get_PerfMonCategoryName() +40
   MySql.Data.MySqlClient.PerformanceMonitor..ctor(MySqlConnection connection) +43
   MySql.Data.MySqlClient.MySqlConnection.Open() +434
   System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) +173
   System.Data.EntityClient.EntityConnection.Open() +96
   System.Data.Objects.ObjectContext.EnsureConnection() +81
   System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +46
   System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +44
   jet.Controllers.WorkOrder.WorkOrderView..ctor() +219
   jet.Controllers.WorkOrder.WorkOrderView.get_Instance() +29
   jet.Controllers.WorkItemController.Index() +11
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +39
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +53
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +258
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +193
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +382
   System.Web.Mvc.Controller.ExecuteCore() +123
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +144
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +54
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
[安全例外:请求'System.Security.Permissions.SecurityPermission,mscorlib,版本=2.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089'类型的权限失败。]
System.Reflection.MethodBase.PerformSecurityCheck(对象obj、RuntimeMethodHandle方法、IntPtr父级、UInt32调用标志)+0
System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr,Binder Binder,Object[]参数,CultureInfo区域性)+470
System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr、Binder Binder、Object[]args、CultureInfo区域性、Object[]activationAttributes)+1051
System.Activator.CreateInstance(类型类型、BindingFlags bindingAttr、Binder Binder、对象[]参数、CultureInfo区域性、对象[]activationAttributes)+111
System.Resources.ResourceManager.CreateResourceSet(流存储、程序集)+357
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo区域性,布尔createIfNotExists,布尔tryParents)+471
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo区域性,布尔createIfNotExists,布尔tryParents)+583
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo区域性,布尔createIfNotExists,布尔tryParents)+583
System.Resources.ResourceManager.GetString(字符串名称,CultureInfo区域性)+74
MySql.Data.MySqlClient.Resources.get_PerfMonCategoryName()+40
MySql.Data.MySqlClient.PerformanceMonitor..ctor(MySqlConnection)+43
MySql.Data.MySqlClient.MySqlConnection.Open()+434
System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(布尔openCondition、DbConnection StoreConnectionOpen、DbConnection originalConnection、字符串例外代码、字符串尝试操作、布尔和closeStoreConnectionOnFailure)+173
System.Data.EntityClient.EntityConnection.Open()+96
System.Data.Objects.ObjectContext.EnsureReconnection()+81
System.Data.Objects.ObjectQuery`1.GetResults(可为null`1 forMergeOption)+46
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()+44
jet.Controller.WorkOrder.WorkOrderView..ctor()+219
jet.Controllers.WorkOrder.WorkOrderView.get_Instance()+29
jet.Controllers.WorkItemController.Index()+11
lambda_方法(ExecutionScope,ControllerBase,Object[])+39
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)+17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)+178
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)+24
System.Web.Mvc.c__显示ClassA.b__7()+53
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func`1 continuation)+258
System.Web.Mvc.c__显示类c.b__9()+20
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext ControllerContext,IList`1筛选器,ActionDescriptor ActionDescriptor,IDictionary`2参数)+193
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)+382
System.Web.Mvc.Controller.ExecuteCore()+123
System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)+23
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext RequestContext)+7
ProcessRequest(HttpContextBase httpContext)+144
ProcessRequest(HttpContext HttpContext)+54
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext HttpContext)+7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+181
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+75

奇怪的一个。GoDaddy共享托管ASP.NET应用程序在中等信任下执行,并且性能计数器创建可能需要完全信任,但性能计数器创建并不是这里失败的地方。(如果失败,它将不会传播出去,因为perf coutner创建异常会被mySQL客户端代码吞没)

相反,它在创建性能计数器之前尝试访问字符串资源失败。MySQL客户端的Resources.Designer.cs中的这行代码失败:

return ResourceManager.GetString("PerfMonCategoryName", resourceCulture)
按照难度递增的顺序,尝试以下几件事:

  • 确保你的应用程序的BIN目录中有MySQL客户端DLL,并且你没有试图从GoDaddy的GAC中加载客户端DLL。如果可以避免的话,永远不要依赖GoDaddy提供的二进制文件

  • 切换到EN-US区域性,这样客户端就不必去寻找另一个资源DLL,看看问题是否消失了

  • 从源代码构建.NET客户端,而不是将DLL从二进制发行版复制到应用程序的BIN目录中。这将使类似这样的问题更容易调试,因为mySQL代码不会是一个黑盒。而且,在紧急情况下,将允许您更改有问题的资源获取调用(或硬编码区域设置)!:-)

  • 顺便说一句,如果您试图在连接字符串中设置“Use Performance Monitor=false”以试图回避问题,请不要费心。问题
        public PerformanceMonitor(MySqlConnection connection)
        {
            this.connection = connection;
    
            //// this line is where it bombs
            string categoryName = Resources.PerfMonCategoryName;
    
            //// this line is affected by connection string setting
            if (connection.Settings.UsePerformanceMonitor && procedureHardQueries == null)
            {
                try
                {
                    procedureHardQueries = new PerformanceCounter(categoryName,
                                                                  "HardProcedureQueries", false);
                    procedureSoftQueries = new PerformanceCounter(categoryName,
                                                                  "SoftProcedureQueries", false);
                }
                catch (Exception ex)
                {
                    Logger.LogException(ex);
                }
            }
        }
    
    <configuration>
      ...
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d"/>
            <bindingRedirect oldVersion="5.0.7.0" newVersion="6.1.2.0"/>
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      ...
    </configuration>