title: Avoiding TRUSTWORTHY ON and PERMISSION_SET = UNSAFE using System.Net.Http
body: <p>Trying to create a stored procedure from a DLL that I built to use with SQL using CLR Integration. I think I need a signed version of <strong>System.Net.Http</strong> and explain why below. Any advice or tips would be appreciated.</p>
  
  <p>The solution works 100% if I use the command</p>
  
  <pre><code>ALTER DATABASE test2 SET TRUSTWORTHY ON
  </code></pre>
  
  <p>Then I Create the assembly using the following commands</p>
  
  <pre><code>CREATE ASSEMBLY [System.Net.Http]
  AUTHORIZATION dbo
  FROM 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll'
  WITH PERMISSION_SET = UNSAFE;
  GO 
  
  CREATE ASSEMBLY [CLRTest01]
  AUTHORIZATION dbo
  FROM 'C:\Windows\CLRTest01.dll'
  WITH PERMISSION_SET = UNSAFE;
  GO 
  </code></pre>
  
  <p>As I choose to trust in default settings, I would like to be able to do this without using Permission level 'UNSAFE' and instead use 'SAFE'. However when I try to do that for CLRTest01 which is reliant on System.Net.Http, I run into the problem of my System.Net.Http being unsigned or wrong format it seems.</p>
  
  <pre><code>CREATE ASSEMBLY [System.Net.Http]
  AUTHORIZATION dbo
  FROM 'C:\Windows\System.Net.Http.dll'
  WITH PERMISSION_SET = SAFE;
  GO 
  </code></pre>
  
  <blockquote>
    <p>Error when using v4.0_2.0.0.0__b03f5f7f11d50a3a
     - Assembly 'System.Net.Http' could not be installed because existing policy would keep it from being used.</p>
    
    <p>Error when using v4.0_4.0.0.0__b03f5f7f11d50a3a
     - CREATE ASSEMBLY failed because type 'System.Net.Http.HttpContent' in safe assembly 'System.Net.Http' has a static field 'EncodingsWithBom'. Attributes of static fields in safe assemblies must be marked  readonly in Visual C#, ReadOnly in Visual Basic, or initonly in Visual C++ and intermediate language.</p>
  </blockquote>
  
  <p>Which leads me to believe I need a signed version of <strong>System.Net.Http</strong>.</p>