title: WebRequest From SQL CLR Error
body: <p><strong>UPDATE:</strong><br>
  Thanks to j.v.'s answer I am able to run this but now I am seeing a lot of the following error in rapid succession:</p>
  
  <pre><code>    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)
  </code></pre>
  
  <p>Doing a search for this I see a number of comments that "first chance exceptions" can generally be ignored.  Is this correct in this case?</p>
  
  <p>Also, can someone explain why I am seeing these error messages and if there is a way to resolve?</p>
  
  <p><strong>OP:</strong></p>
  
  <p>I have created a WCF REST Service which works correctly when invoked from web browser:</p>
  
  <p><a href="https://i.stack.imgur.com/HazgB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HazgB.png" alt="enter image description here"></a></p>
  
  <p>I am now creating a SQL CLR which is to consume the REST Service.  Here is the code for the SQL CLR:</p>
  
  <pre><code>public partial class UserDefinedFunctions
  {
      public enum HttpMethods { POST, PUT, GET, DELETE };
  
  
      /// &lt;summary&gt;
      /// Test Method
      /// &lt;/summary&gt;
      /// &lt;param name="name"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
      public static SqlString ExecFoxPro_SayHello(String name)
      {
          // Put your code here
          return new SqlString ("*** Hello, " + name + "!");
      }
  
  
      /// &lt;summary&gt;
      /// Return the allowed paths the FoxProConnectWS is configured for
      /// &lt;/summary&gt;
      /// &lt;param name="WebServiceUri"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      [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();
      }
  }
  </code></pre>
  
  <p>I am now trying to test the SQL CLR from Visual Studio using the Test.sql file which is setup like this:</p>
  
  <pre><code>-- 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')
  </code></pre>
  
  <p>When I run this in Debug within Visual Studio 2013 I get the followiong error (happens on the <code>_request = (HttpWebRequest)WebRequest.Create(serviceUrl.ToString());</code> line):</p>
  
  <pre><code>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&amp; 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)
  .
  </code></pre>
  
  <p>What permissions is this expecting and how to I assign?  I would prefer to NOT have to set permissions as this is a program that we distribute to various customers and I cannot guarantee that all customers will have the same permissions setup (i.e., users, access level, etc.)</p>