WebRequest 来自 SQL CLR 错误
WebRequest From SQL CLR Error
更新:
感谢 j.v. 的回答,我能够 运行 这个,但现在我看到很多快速连续的错误:
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
(plus about 20-30 more after this)
搜索此内容时,我看到许多 "first chance exceptions" 通常可以忽略的评论。在这种情况下这是正确的吗?
此外,有人可以解释为什么我会看到这些错误消息吗?是否有解决方法?
OP:
我创建了一个 WCF REST 服务,它在从 Web 浏览器调用时可以正常工作:
我现在正在创建一个 SQL CLR 来使用 REST 服务。这是 SQL CLR 的代码:
public partial class UserDefinedFunctions
{
public enum HttpMethods { POST, PUT, GET, DELETE };
/// <summary>
/// Test Method
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlString ExecFoxPro_SayHello(String name)
{
// Put your code here
return new SqlString ("*** Hello, " + name + "!");
}
/// <summary>
/// Return the allowed paths the FoxProConnectWS is configured for
/// </summary>
/// <param name="WebServiceUri"></param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlString GetAllowedPaths(SqlString serviceUrl)
{
String _url = serviceUrl.ToString() + @"/AllowedPaths";
HttpWebRequest _request = null;
HttpWebResponse _response = null;
_request = (HttpWebRequest)WebRequest.Create(serviceUrl.ToString());
_request.ContentLength = 0;
_response = (HttpWebResponse)_request.GetResponse();
return (SqlString)_response.ToString();
}
}
我现在正在尝试使用 Test.sql 文件从 Visual Studio 测试 SQL CLR,该文件的设置如下:
-- To execute test from within Visual Studio, highlight the row(s) to execute and click hollow green button in this panes header or Ctrl+Shift+E
select dbo.ExecFoxPro_SayHello('Brian')
select dbo.GetAllowedPaths('http://localhost:9876/FoxProConnectService')
当我在 Visual Studio 2013 年的 Debug 中 运行 这个时,我得到以下错误(发生在 _request = (HttpWebRequest)WebRequest.Create(serviceUrl.ToString());
行):
Msg 6522, Level 16, State 1, Line 6
A .NET Framework error occurred during execution of user-defined routine or aggregate "GetAllowedPaths":
System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.SecurityException:
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Net.HttpWebRequest.CheckConnectPermission(Uri uri, Boolean needExecutionContext)
at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)
at System.Net.HttpRequestCreator.Create(Uri Uri)
at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
at UserDefinedFunctions.GetAllowedPaths(SqlString serviceUrl)
.
这需要什么权限以及如何分配?我宁愿不必设置权限,因为这是我们分发给不同客户的程序,我不能保证所有客户都具有相同的权限设置(即用户、访问级别等)
在 SQL Server management studio 中,找到包含您的 clr 的程序集,转到属性并将权限集更改为 "External Access" 或 "Unrestricted"。
更新:
感谢 j.v. 的回答,我能够 运行 这个,但现在我看到很多快速连续的错误:
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
(plus about 20-30 more after this)
搜索此内容时,我看到许多 "first chance exceptions" 通常可以忽略的评论。在这种情况下这是正确的吗?
此外,有人可以解释为什么我会看到这些错误消息吗?是否有解决方法?
OP:
我创建了一个 WCF REST 服务,它在从 Web 浏览器调用时可以正常工作:
我现在正在创建一个 SQL CLR 来使用 REST 服务。这是 SQL CLR 的代码:
public partial class UserDefinedFunctions
{
public enum HttpMethods { POST, PUT, GET, DELETE };
/// <summary>
/// Test Method
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlString ExecFoxPro_SayHello(String name)
{
// Put your code here
return new SqlString ("*** Hello, " + name + "!");
}
/// <summary>
/// Return the allowed paths the FoxProConnectWS is configured for
/// </summary>
/// <param name="WebServiceUri"></param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlString GetAllowedPaths(SqlString serviceUrl)
{
String _url = serviceUrl.ToString() + @"/AllowedPaths";
HttpWebRequest _request = null;
HttpWebResponse _response = null;
_request = (HttpWebRequest)WebRequest.Create(serviceUrl.ToString());
_request.ContentLength = 0;
_response = (HttpWebResponse)_request.GetResponse();
return (SqlString)_response.ToString();
}
}
我现在正在尝试使用 Test.sql 文件从 Visual Studio 测试 SQL CLR,该文件的设置如下:
-- To execute test from within Visual Studio, highlight the row(s) to execute and click hollow green button in this panes header or Ctrl+Shift+E
select dbo.ExecFoxPro_SayHello('Brian')
select dbo.GetAllowedPaths('http://localhost:9876/FoxProConnectService')
当我在 Visual Studio 2013 年的 Debug 中 运行 这个时,我得到以下错误(发生在 _request = (HttpWebRequest)WebRequest.Create(serviceUrl.ToString());
行):
Msg 6522, Level 16, State 1, Line 6
A .NET Framework error occurred during execution of user-defined routine or aggregate "GetAllowedPaths":
System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.SecurityException:
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Net.HttpWebRequest.CheckConnectPermission(Uri uri, Boolean needExecutionContext)
at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)
at System.Net.HttpRequestCreator.Create(Uri Uri)
at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
at UserDefinedFunctions.GetAllowedPaths(SqlString serviceUrl)
.
这需要什么权限以及如何分配?我宁愿不必设置权限,因为这是我们分发给不同客户的程序,我不能保证所有客户都具有相同的权限设置(即用户、访问级别等)
在 SQL Server management studio 中,找到包含您的 clr 的程序集,转到属性并将权限集更改为 "External Access" 或 "Unrestricted"。