title: Loading a CRL assembly in SQL Server 2005: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
body: <p>I've written a C# DLL which I need to call from a stored procedure.</p>
  
  <p>I've got errors through ALL the steps I did, and managed to solve them. But now I'm so tired of errors that I had to resort here and ask. I can't solve this one. Every little step I made in installing this .DLL into SQL Server, I got tons of errors to solve.</p>
  
  <p>Here's the C# code of the program (compiled as Class Library to get the DLL):</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net;
  using System.Xml;
  using System.IO;
  using System.Data;
  using System.Data.SqlClient;
  using System.Data.SqlTypes;
  using Microsoft.SqlServer.Server;
  
  namespace WebServiceVatEuropa
  {
      public class WebServiceVatEuropaClass
      {
          #region "Default Constructor"
          public WebServiceVatEuropaClass()
          {
  
          }
          #endregion
  
          [SqlProcedure]
          public static void check(string country, string vatNum)
          {
              bool valid;
              string name;
              string address;
              checkVatService vatchecker = new checkVatService();
              vatchecker.checkVat(ref country, ref vatNum, out valid, out name, out address);
          }
      }
  }
  </code></pre>
  
  <p>Which is using another free library provided at the european vat checking webservice (<a href="http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl" rel="nofollow">Link to webservice</a>) and uses their library (<a href="http://pastebin.com/KwBxPZPa" rel="nofollow">Library code</a>) (this code is long and complex, you can just read mine I guess.</p>
  
  <p>Well. I create the stored procedure (I had many problems here, many errors and so on, but now it works) with this code in SQL Server Management Studio:</p>
  
  <pre><code>CREATE PROCEDURE VAT_CHECKER
  @Naz_codi nvarchar(2),
  @vatNum nvarchar(max)
  AS EXTERNAL NAME WebServiceVatEuropaClass.[WebServiceVatEuropa.WebServiceVatEuropaClass].[check]
  GO
  </code></pre>
  
  <p>And then in the db I have:</p>
  
  <ol>
  <li><p>enabled the clr with the code from msn</p>
  
  <pre><code>sp_configure 'show advanced options', 1;
  GO
  RECONFIGURE;
  GO
  sp_configure 'clr enabled', 1;
  GO
  RECONFIGURE;
  GO
  </code></pre></li>
  <li><p>changed the db owner to sa</p></li>
  <li><p>registered the assembly with this code</p>
  
  <pre><code>CREATE ASSEMBLY [WebServiceVatEuropaClass] --AUTHORIZATION [mydb\administrator]
  FROM 'C:\Upload\WebServiceVatEuropa.dll'
  --WITH PERMISSION_SET = SAFE
  WITH PERMISSION_SET = UNSAFE
  --WITH PERMISSION_SET = EXTERNAL_ACCESS
  </code></pre></li>
  </ol>
  
  <p>Then when I launch</p>
  
  <pre><code>EXEC VAT_CHECKER 'IT','10050721009'
  </code></pre>
  
  <p>I get  this error:</p>
  
  <blockquote>
    <p>An error occurred in the Microsoft .NET Framework while trying to load assembly id 65545. The server may be running out of resources, or
    the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS
    or UNSAFE. Run the query again, or check documentation to see how to
    solve the assembly trust issues. For more information about this
    error:  System.IO.FileLoadException: Could not load file or assembly
    'webservicevateuropa, Version=0.0.0.0, Culture=neutral,
    PublicKeyToken=null' or one of its dependencies. The given assembly
    name or codebase was invalid. (Exception from HRESULT: 0x80131047)</p>
    
    <p>System.IO.FileLoadException:<br>
    at System.Reflection.Assembly._nLoad(AssemblyName fileName, String
    codeBase, Evidence assemblySecurity, Assembly locationHint,
    StackCrawlMark&amp; stackMark, Boolean throwOnFileNotFound, Boolean
    forIntrospection)<br>
    at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef,
    Evidence assemblySecurity, StackCrawlMark&amp; stackMark, Boolean
    forIntrospection)<br>
    at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark&amp; stackMark, Boolean forIntrospection)<br>
    at System.Reflection.Assembly.Load(String assemblyString)</p>
  </blockquote>
  
  <p>What it could be? I can't find anything to fix it...</p>