views:

129

answers:

2

I'm trying to call a WCF Service from a SQL Stored Procedure written in C#.

I saw various posts or questions on about the same topic : http://stackoverflow.com/questions/3502343/calling-a-wcf-service-from-sql-clr-stored-procedure http://stackoverflow.com/questions/751500/sql-clr-stored-procedure-and-web-service

But there's something I don't get. To be able to call my WCF Service from the Stored Procedure I'm creating the WCF client in the C# code of the procedure:

    //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();

I'm compiling the project as a .NET 3.0+ to be able to compile it (Reference to Sytem.ServiceModel). When I try to deploy it on the SQL Server, I'm getting the following message:

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.

Should I register this assembly also on the server? as the assemblies needed for WCF? Wouldn't I damage the server adding so many assemblies?

Thanks for your help.

A: 

It should work if you use a Web Reference in the CLR assembly instead of a Service Reference.

Jason Goemaat
It works with an .asmx web service but I will try to do it with a web reference and building in .NET 3.0+ to use a WCF service
davandries
A: 

SQL Server will need your assembly in order to load the CLR Sproc as it's compiled with it. You need to add it to the GAC on the Server. I don't know what you mean 'damage the server'

Preet Sangha
By "damage the server", I mean: introduce unexpected behaviors in the database, or lower the performance of SQL Server with non supported assemblies
davandries
If I may share a few thoughts here, if your main concern was really "about damaging the server", then I think calling a Web Service from a stored procedure should have been your first warning :)
Johann Blais
:) you're right Johann... architecture of the solution we're working on kind of put us in this direction
davandries
I managed to get it done using the code above, but it wasn't working on my windows 7 development computer and worked on the w2k3 server
davandries