Include foreign assembly into SQL Server 2012 - sql-server

I encountered the following problem. When I try to add the assembly to SQL Server 2012 I get the following error:
Assembly "iAnywhere.Data.SQLAnywhere.v4.0" refers to an assembly "system.data.entity, version = 4.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089.", Which is not in the current database. SQL Server attempted to locate and automatically load the specified assembly from the same location, where the referencing assembly, but the operation failed (reason: 2 (can not find the file specified.)). Load the specified assembly into the current database and try the request again.
After that I tried to add the assembly specified in the description and got the following:
Assembly "system.runtime.serialization, version = 4.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089." not found in the catalog SQL.
Attention! Detectable object structures assembly Microsoft. NET Framework "system.data.entity, version = 4.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089, processorarchitecture = msil." not been fully tested in an environment running SQL Server. In the future, if you upgrade or service this assembly or the. NET Framework subroutine CLR integration may stop working. For more information, see the documentation for SQL Server. (. Net SqlClient Data Provider)
So I tried google it and found this related connect article:
https://connect.microsoft.com/SQLServer/feedback/details/744584/unable-to-load-system-identitymodel-dll-assembly-into-sql-server-2012#details
Is it possible to do something about my problem?

Related

An error occurred in the Microsoft . NET Framework while trying to load assembly

I am trying to create a view which should select data from snapshot database table on SQL Server 2017.
However I become the following error:
An error occurred in the Microsoft . NET Framework while trying to load assembly id 65536. The server may be running out of resources, or the assembly may not be trusted.
The problem is with one assembly, but in case of other views everything works fine. Any suggestions what needs to be done?

How to fix 'Cannot load dynamically generated serialization assembly' error

"Cannot load dynamically generated serialization assembly." error when executing a clr function that calls a web service in SQL server 2008 R2.
I have a database project in Visual Studio 2017 which includes a clr function that calls a web service.
The project properties include the following:
Project Settings Target Platform = SQL Server 2008
SQLCLR Target framework = .Net Framework 3.5
SQLCLR Build Generate serialization assembly = On
The serialization assembly is created in the database by a post-deployment script when the database is published.
When I publish the databse to a SQL Server 2008R2 instance on my Windows 10 PC, executing the clr function results in the "Cannot load dynamically generated serialization assembly" error, however, when the database is published to a SQL2016 instance on my PC, it runs OK
Post deployment script to register serialization assembly:
CREATE ASSEMBLY [CifasEdit.XmlSerializers] FROM 'C:\tfs\CIFAS\Source\Database\Hub\CifasEdit\CifasEdit\bin\Release\CifasEdit.XmlSerializers.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS;
GO
The full error is here
A .NET Framework error occurred during execution of user-defined routine or aggregate "xxx":
System.InvalidOperationException: Cannot load dynamically generated serialization assembly. In some hosting environments assembly load functionality is restricted, consider using pre-generated serializer. Please see inner exception for more information. ---> System.IO.FileLoadException: LoadFrom(), LoadFile(), Load(byte[]) and LoadModule() have been disabled by the host.
System.IO.FileLoadException:
at System.Reflection.Assembly.nLoadImage(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence evidence, StackCrawlMark& stackMark, Boolean fIntrospection)
at System.Reflection.Assembly.Load(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence securityEvidence)
at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
at Microsoft.CSharp.CSharpCodeGenerator.FromSourceBatch(CompilerParameters options, String[] sources)
at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.
...
System.InvalidOperationException:
at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer.GetSerializersFromCache(XmlMapping[] mappings, Type type)
at System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] mappings, Type type)
at System.Web.Services.Protocols.SoapClientType..ctor(Type type)
at System.Web.Services.Protocols.SoapHttpClientProtocol..ctor()
So it appears that the serialization assembly generated automatically by VS2017 does not play nice with SQL Server 2008 R2 although it works fine with SQL2016.
To get the CLR function working in 2008R2 I moved the CLR code to a separate project and generated the serialization assembly manually using sgen.exe, then registered both dlls.
Be sure to use a version of sgen for clr2.0, on my PC this was located in
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin
The command to generate the serialization assembly is
sgen.exe c:\YourDir\YourAssembly.dll
This will create an assembly called YourAssembly.XmlSerializers.dll
Register both assemblies with SQL Server:
CREATE ASSEMBLY [YourAssembly] FROM 'c:\YourDir\YourAssembly.dll' WITH PERMISSION_SET = EXTERNAL_ACCESS
CREATE ASSEMBLY [YourAssembly.XmlSerializers] FROM 'c:\YourDir\YourAssembly.XmlSerializers.dll' WITH PERMISSION_SET = SAFE

Issue when registering into SQL server a SQL CLR assembly referencing FSharp.Data.SqlProvider

I have a F# DLL (.NET Framework 4.5.1, F# 4.1, FSharp.Core 4.4.3.0). The only NuGet added reference is to FSharp.Data.SqlProvider 1.1.41.
The code is in one file only
module DB
open FSharp.Data.Sql
[<Literal>]
let private dbVendor = Common.DatabaseProviderTypes.MSSQLSERVER
Build goes without errors
Import the built dll in SQL server as assembly
CREATE ASSEMBLY [Library2] FROM '<path>\Library2.dll' WITH PERMISSION_SET = UNSAFE
Operation fails with error
Assembly 'Library2' references assembly 'fsharp.core, version=4.3.1.0,
culture=neutral, publickeytoken=b03f5f7f11d50a3a.', which is not
present in the current database. SQL Server attempted to locate and
automatically load the referenced assembly from the same location
where referring assembly came from, but that operation has failed
(reason: version, culture or public key mismatch). Please load the
referenced assembly into the current database and retry your request
Now comment the last two rows in the code
//[<Literal>]
//let private dbVendor = Common.DatabaseProviderTypes.MSSQLSERVER
With this the SQL assembly creation succeeds, the assembly is correctly created in SQL server
Finally, if I downgrade the project to F# 3.1 (FSharp.Core 4.3.1.0), then no error in both cases
SQL Server 14.0.1000
Windows 10 Pro
Please note that an issue has already been raised in the project GitHub repo: Issue #541
Questions
Does anyone see something wrong in what I am doing?
Has anyone incurred in such an issue and solved it? If yes, how?
F# is not a natively supported SQLCLR language. I believe only C#, VB.NET, and Visual C++ are (and of course, IL if you are into writing it directly and not using a silly compiler ;).
So, as the error message states, you need to manually load that F# DLL, marking it as UNSAFE.
Please see the following answer of mine, also on S.O., for additional details on using F# in SQLCLR:
F# with sqlclr in a reasonably safe way and scripted assembly

Entity Framework Profiler: unable to determine the provider name for provider factory of type 'system.data.sqlclient.sqlclientfactory'

When I use the Entity Framework Profiler 3.0 to log the real SQL statement in background, sometimes I give this exception:
Message: unable to determine the provider name for provider factory of type 'system.data.sqlclient.sqlclientfactory'.
Source: EntityFrameWork.
I give this error when I run the project in visual studio 2015 debugging mode, it displays a file browser to locate r5wldbbb.5.cs. I don't know what's this file and also it doesn't exist in Entity Framework Profiler folder. So when I cancel it I give the above exception and raise this error:
r5wldbbb.5.cs not found.
You need to find r5wldbbb.5.cs to view the source for the current call stack frame.
However, when I cancel the Entity Framework Profiler I don't give this error.
I use these programe:
Visual Studio 2015.
MSSQL Server 2014.
EntityFramework 6.
Any helps appreciated.
I finally fixed it.
The trick is to update the way you get and open the connection to the database:
var connectionString = ConfigurationManager.ConnectionStrings["yourContextName"].ConnectionString;
var connectionFactoryService = (IDbConnectionFactory)DbConfiguration.DependencyResolver.GetService(typeof(IDbConnectionFactory);
var DbConn = connectionFactoryService.CreateConnection(connectionString);
Let me know if it works for you too :) Also, don't forget to update the EF profiler to the latest version using NuGet.

Why can I not register System.Web in SQL Server 2008?

Running this command:
CREATE ASSEMBLY
[System.Web] from
'C:\Windows\Microsoft.NET\Framework\v2.0.50727\system.web.dll'
with permission_set = UNSAFE
Gives me this error:
Msg 10300, Level 16, State 2, Line 1
Assembly 'System.Web' references assembly 'system.web, version=2.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.', which is not present in the current database. SQL Server attempted to locate and automatically load the referenced assembly from the same location where referring assembly came from, but that operation has failed (reason: version, culture or public key mismatch). Please load the referenced assembly into the current database and retry your request.
... this sounds a little silly. It seems like SQL Server thinks that the System.Web assembly is referencing it's self. How can I fix this?
Try with Framework64 assemblies (64 bit sql server 2008)
CREATE ASSEMBLY
[System.Web] from
'C:\Windows\Microsoft.NET\Framework64\v2.0.50727\System.Web.dll'
with permission_set = UNSAFE
GO
It sounds like you need to install the .Net 2.0 framework on your database server.
Also, I wouldn't directly add a reference to System.Web.dll. Your other custom CLR code should reference that. (Or, if you don't have custom .Net code, you should create a custom .Net project to interface into the System.Web assembly.)
Turns out System.web.dll isn't supported for this. In fact, it turned out that loading DLLs into SQL Server like this (for CLR) was a bad idea on many levels (one of which was 64/32-bit support between deployments).

Resources