SQL Server CLR:如何在Visual Studio 2013数据库项目中的CLR SQL存储过程中调用WCF服务
我有Visual Studio 2013。
我创建了一个数据库项目。
我为该项目添加了一个CLR存储过程,我想从中调用一个WCF服务。
谁能帮我?
我希望能够在不更改程序集的情况下更改服务地址,类似于我在web.config
使用我的WCF客户端配置的endpoint
部分可以执行的操作。
经过大量的搜索后我发现了这个,并在VS 2014中花了很多时间
- 创建名为“CLR_Test”的
Database Project
- 为WCF客户端创建
Library
“CLR_Service_Client” -
将wcf服务的
Serivce Refrence
添加到“CLR_Test”然后将“CLR_Service_Client”的引用添加到“CLR_Test”中
4.您必须使用以下代码更改DB选项以运行不安全的程序集ALTER DATABASE SaleAutomation SET TRUSTWORTHY ON RECONFIGURE
-
在
SQLCLR
选项卡中的“CLR_Test”Project Properties
中,将Permission level
设置为“Unsafe
(另一种方式是,在发布项目之后,您从sql server管理更改其级别,另一种方式是向发布脚本添加权限级别,您可以使用每个他们,
但你必须注意到,如果你只使用project properties
“CLR_Test”项目自动创建Unsafe
,你必须使用其他方法来设置“CLR_Service_Client”Unsafe
)
6.运行此脚本添加Sqlserver能够运行wcf服务
CREATE ASSEMBLY SMDiagnostics from 'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\SMDiagnostics.dll' with permission_set = UNSAFE GO CREATE ASSEMBLY [System.Web] from 'C:\Windows\Microsoft.NET\Framework64\v2.0.50727\System.Web.dll' with permission_set = UNSAFE GO CREATE ASSEMBLY [System.Messaging] from 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Messaging.dll' with permission_set = UNSAFE GO CREATE ASSEMBLY [System.IdentityModel] from 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.IdentityModel.dll' with permission_set = UNSAFE GO CREATE ASSEMBLY [System.IdentityModel.Selectors] from 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.IdentityModel.Selectors.dll' with permission_set = UNSAFE GO CREATE ASSEMBLY -- this will add service modal [Microsoft.Transactions.Bridge] from 'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\Microsoft.Transactions.Bridge.dll' with permission_set = UNSAFE GO CREATE ASSEMBLY -- this will add service modal [System.Runtime.Serialization] from 'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\System.Runtime.Serialization.dll' with permission_set = UNSAFE GO CREATE ASSEMBLY -- this will add service modal [System.ServiceModel] from 'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\System.ServiceModel.dll' with permission_set = UNSAFE GO
- 现在您发布您的项目并运行存储过程并享受。
从.Net v4.0开始,这一切都没有实际意义,System.ServiceModel是一个混合的MSIL,这意味着它使用将生成的本机代码:
程序集“System.ServiceModel”的CREATE ASSEMBLY失败,因为程序集“microsoft.visualbasic.activities.compiler”格式不正确或者不是纯.NET程序集。 无法validation的PE标头/本机存根。 对于我们想要做的事情来说,这本来是一个非常优雅的解决方案,但微软再次让我们悬在悬崖上
我找到了解决问题的新方法如果你将服务参考中的wcf service
称为Web Refrence
你就不必做任何事情
- 首先添加服务引用
- 其次,将Service添加为Web引用
- 全部完成并继续使用方法调用服务