15 pts.
 Error while referencing external assembly in SQL Server 2005.
Sir, I created a dll (SqlWcfServiceLibrary1.dll) using .net frammework 2008. My code has a reference of another assemble (WcfServiceLibrary1.dll) also. I want to create assemble in sql and use that assemble in a procedure. I am getting error that "Error while referencing external assembly". I am pasting all code that I used.... .cs page code - namespace SqlWcfServiceLibrary1 { public class SqlService1 { void SendToSql(string message) { SqlMetaData[] table = new SqlMetaData[1]; table[0] = new SqlMetaData("Message", SqlDbType.VarChar, 1000); SqlDataRecord row = new SqlDataRecord(table); row.SetValue(0, message); SqlPipe pipe = SqlContext.Pipe; pipe.SendResultsStart(row); pipe.SendResultsRow(row); pipe.SendResultsEnd(); } public static void GetMessage(SqlString name) { Assembly assembly = LoadAssembly("D:\OutCast\WcfServiceLibrary1\WcfServiceLibrary1\bin\Debug\WcfServiceLibrary1.dll", true); Type type = LoadType(assembly, "WcfServiceLibrary1.Service1", true); String message = ""; using (ServiceHost host = new ServiceHost(type)) { host.Open(); IService1 service1 = InitChannel(); message = service1.GetMessage(name.ToString()); host.Close(); } } static IService1 InitChannel() { Uri address = new Uri("net.tcp://localhost:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/"); ChannelFactory<IService1> factory = new ChannelFactory<IService1>(new NetTcpBinding(), new EndpointAddress(address)); return factory.CreateChannel(); } static Assembly LoadAssembly(String assemblyName, bool throwOnError) { Assembly assembly = null; try { assembly = Assembly.LoadFrom(assemblyName); } catch { } if (assembly == null && throwOnError) throw new Exception("Hello Sujeet, Error while referencing external assembly.rn rnAssembly '" + assemblyName + "' doesn't exist."); return assembly; } static Type LoadType(Assembly assembly, String typeName, bool throwOnError) { Type type = null; try { type = assembly.GetType(typeName, false, true); } catch { } if (type == null && throwOnError) throw new Exception("Error while loading type.rn rnType '" + typeName + "' doesn't exist."); return type; } } [ServiceContract] public interface IService1 { [OperationContract] string GetMessage(string name); } } using above code I created a dll and using it in sql. SQL Code - -- create assembly CREATE ASSEMBLY SqlWcfServiceLibrary1 FROM 'D:OutCastSqlWcfServiceLibrary1SqlWcfServiceLibrary1binDebugSqlWcfServiceLibrary1.dll' WITH PERMISSION_SET = EXTERNAL_ACCESS GO --create procedure -- external name is composed by
Software/Hardware used:
ASKED: March 16, 2009  9:47 AM
UPDATED: March 22, 2009  7:35 PM

Answer Wiki:
Does the assembly exist at the path specified on the SQL Server? You'd be better off placing the assembly in the GAC then calling it from there. What is the end goal of this assembly?
Last Wiki Answer Submitted:  March 16, 2009  7:22 pm  by  Denny Cherry   64,520 pts.
All Answer Wiki Contributors:  Denny Cherry   64,520 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Thanks for your reply.

My assemble exist in the path that I specified on the Sql server and code. Error is giving about that assemble that I used in code.

Assembly assembly = LoadAssembly(“D:\OutCast\WcfServiceLibrary1\WcfServiceLibrary1\bin\Debug\WcfServiceLibrary1.dll”, true);

After executing proc the above assemble is not found. If you give me your email id, I will mail you my project after that you can check.

This assemble is returning a simple message. I am just wanting to use external assemble in class library.

I tried this after adding my assemble in GAC but it is giving same error. If you have any other solution please tell me.

Sujeet

 15 pts.

 

My looking at the CLR code isn’t going to help. I don’t know enought about CLR to help on that path. My recommendation would be to load the other assembly into the SQL Server and create a function or procedure pointing to it directly as it doesn’t appear that your .NET code is doing anything but calling the other assembly.

Perhaps if you remove the explicit path to the DLL the SQL Server could then find it in the GAC?

 64,520 pts.