我正在尝试从用C#编写的SQL存储过程调用WCF服务。
我看到有关同一主题的各种帖子或问题: Calling a WCF Service from SQL CLR Stored Procedure SQL CLR Stored Procedure and Web Service
但是有些东西我没有得到。 为了能够从存储过程调用我的WCF服务,我在过程的C#代码中创建了WCF客户端:
//Create an endpoint addresss for our service
public static EndpointAddress endpoint =
new EndpointAddress(new Uri("http://localhost:32226/ServiceName.svc"));
//Create a binding method for our service
public static WSHttpBinding HttpBinding = new WSHttpBinding();
//Create an instance of the service proxy
public static ChannelFactory<IServiceName> MyChannelFactory = new ChannelFactory<IRecursosHumanos>(HttpBinding, endpoint);
// Create a channel.
public static IRecursosHumanos ServicioRrhh = MyChannelFactory.CreateChannel();
我正在将项目编译为.NET 3.0+以便能够编译它(参考Sytem.ServiceModel)。当我尝试在SQL Server上部署它时,我收到以下消息:
Msg 10301, Level 16, State 1, Line 2
Assembly 'MyAssembly' references assembly 'system.runtime.serialization, version=3.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089.', 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: 2(failed to retrieve text for this error. Reason: 15105)). Please load the referenced assembly into the current database and retry your request.
我是否应该在服务器上注册此程序集?作为WCF所需的组件? 我不会损坏添加这么多程序集的服务器吗?
感谢您的帮助。
答案 0 :(得分:3)
如果在CLR程序集中使用Web引用而不是服务引用,它应该可以工作。
答案 1 :(得分:1)
经过2天的反复试验,我终于得到了它的工作,如下:
(在 Windows Server 2008 x64,SQL Server 2008 64位,使用Framework 3.0构建的程序集DLL,任何CPU )。我建议你坚持使用.Net 3 for SQL 2008。
alter database [TTBackup]
set trustworthy on;
go
USE TTBackup
--Register the assemblies referenced by our assembly.
CREATE ASSEMBLY SMdiagnostics AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\SMdiagnostics.dll' WITH permission_set = unsafe
CREATE ASSEMBLY [System.Web] AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\Framework64\v2.0.50727\System.Web.dll' WITH permission_set = unsafe
CREATE ASSEMBLY [System.Runtime.Serialization] AUTHORIZATION dbo FROM 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.Runtime.Serialization.dll' WITH permission_set = unsafe
CREATE ASSEMBLY [System.IdentityModel.Selectors] FROM 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.IdentityModel.Selectors.dll' with permission_set = unsafe
CREATE ASSEMBLY [System.Messaging] AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Messaging.dll' WITH permission_set = unsafe
CREATE ASSEMBLY [Microsoft.Transactions.Bridge] FROM 'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\Microsoft.Transactions.Bridge.dll' with permission_set = unsafe
--register our assembly
CREATE ASSEMBLY [TTDatabaseIntegration] AUTHORIZATION dbo FROM 'D:\TTDatabaseIntegration.dll' WITH permission_set = unsafe
GO
--Register the UDF from CLR
CREATE FUNCTION [ListImportTemplates]( ) RETURNS TABLE(TemplateID int, TemplateName NVARCHAR(4000))
AS EXTERNAL NAME [TTDatabaseIntegration].[UserDefinedFunctions].[ListImportTemplates]
GO
--Test my function
select * FROM dbo.[ListImportTemplates]()
请注意,大多数引用的DLL都是32位,但System.Web是64位。 (这是它对我有用的唯一方式)。
首先我注册了64位程序集,但它抛出了运行时异常:System.IO.FileLoadException: Could not load file or assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Assembly in host store has a different signature than assembly in GAC. (Exception from HRESULT: 0x80131050) See Microsoft Knowledge Base article 949080 for more information.
我最终将GAC中的文件(按大小)与我在SQL中注册的文件进行比较。 (GAC位于C:\ Windows \ assembly,但您无法在资源管理器中查看该文件夹,我一直在使用Total Commander。)这就是我发现GAC中的版本是32位版本的方式
您可以使用此查询查看已注册的程序集:SELECT * FROM sys.assemblies
答案 2 :(得分:0)
SQL Server将需要你的程序集才能加载CLR Sproc,因为它是用它编译的。您需要将其添加到服务器上的GAC。我不知道你的意思是“损坏服务器”