0

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.

1

2 Answers 2

1

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>
0
0

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.

2
  • Hi kkokosa, I'm already doing it like this. The problem is we need the endpoint to be in a different file. We don't want to recompile the dll every time we are going to deploy it to one of our clients. Nov 11, 2013 at 13:34
  • It gave me an idea though. We are going to add an extra parameter that contains the endpoint. It is passed through via SQL Server. The data is stored in a table. This way we don't need to work with any config files and just deliver the dll's in a setup project. Nov 11, 2013 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.