Question

I have a c# CLR project that is called by MS SQL Server. The CLR project tries to connect to, and read from, a WCF service. Normally the webservice bindings and endpoints are configured in the corresponding app.config file. This is also the case in my project. However, when I, for example, try to read the AppSettings from the app.config file I get a "object reference not set to an instance of this object". The return value from the AppSettings call is null. The CLR can't find the config file. I want to call the config file because it contains the endpoint URL I want to connect to.

This is what I'm doing:
1. Building the CLR DLL and putting it in a directory together with the configuration file
2. Loading the assembly in MS SQL Server (2008 R2)
3. Creating functions and launching a call to the CLR

What am I doing wrong? Is there any way to read from the app.config file in this particular scenario?

Hope the question is clear. Thanks in advance.

Was it helpful?

Solution 2

Maybe the simplest solution would be to configure WCF client programatically within CLR DLL library?

var binding = new System.ServiceModel.BasicHttpBinding();
binding.SendTimeout = TimeSpan.FromMinutes( 1 );
binding.OpenTimeout = TimeSpan.FromMinutes( 1 );
binding.CloseTimeout = TimeSpan.FromMinutes( 1 );
binding.ReceiveTimeout = TimeSpan.FromMinutes( 10 );
using (var client = new SomeServiceSoapClient(binding, new System.ServiceModel.EndpointAddress("http://site.example/SomeService.asmx")))
{
    // client.DoWork(...)
}

and pass configuration data (URL) as argument to the SQL CLR function? In this way you are independent from the app.config location problems.

OTHER TIPS

How are you reading your settings from the app.config file?

Remember to use the ConfigurationManager class, available in the System.Configuration namespace, to retrieve the value of your setting in the AppSettings section of configuration file. You can do that in this way:

var value = ConfigurationManager.AppSettings["mySetting"];

if you have your app.config file like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
     <add key="mySetting" value="setting"/>
  </appSettings>
</configuration>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top