Accessing an endpoint element from Web.config or app.config

Hi All,

I am trying connect to a web service endpoint from my code as below:

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress remoteAddress = new EndpointAddress("<endpoint address>");
binding.Name = "ServiceSOAP";
binding.AllowCookies = false;
ServiceClient client = new ServiceClient(binding, remoteAddress);

Using the above way I can connect to the endpoint and carry out the tasks.

My query is:

" Instead of hard coding the endpoint address in the code, can it be fetched from the app.config or web.config file.

The config file is as below:

<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="NotificationServiceSOAP" />
            </basicHttpBinding>
            
        </bindings>
        <client>
            <endpoint address="http://<endpointaddress>/notificationDispatcher"
                binding="basicHttpBinding" bindingConfiguration="NotificationServiceSOAP"
                contract="Notification.NotificationService" name="NotificationServiceSOAP" />
            
        </client>
    </system.serviceModel>

I tried using the code below to access the endpoint address from within the event receiver but it fails in first line itself on debug, might be it is not able to read the config file.

ClientSection clientSection = (ClientSection)WebConfigurationManager.GetSection("system.serviceModel/client");
ChannelEndpointElement endpoint = clientSection.Endpoints[0];
string endpointStr = string.Format("Address: {0}; Binding: {1}; Contract: {2}", endpoint.Address.ToString(), endpoint.Binding, endpoint.Contract);
NotificationServiceClient client = new NotificationServiceClient(endpointStr);

I have also tried using as below but doesn't work inside an event receiver

 NotificationServiceClient client = new NotificationServiceClient("NotificationServiceSOAP");

Could you please suggest few options or am I doing anything wrong here!

Thanks in advance.

December 22nd, 2014 10:58am

gi,

you can save the name of endpoint in the web.config app settings 

like this

<appSettings> <add key="endpointaddress" value="<endpointaddressvalue>"/>

</appSettings>

and you can read the appsetting value from code like this

System.Configuration.Configuration rootWebConfig1 =
				System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
			if (rootWebConfig1.AppSettings.Settings.Count > 0)
			{
				System.Configuration.KeyValueConfigurationElement customSetting = 
					rootWebConfig1.AppSettings.Settings["customsetting1"];
				if (customSetting != null)
					Console.WriteLine("customsetting1 application string = \"{0}\"", 
						customSetting.Value);
				else
					Console.WriteLine("No customsetting1 application string");
			}

Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2014 1:02pm

Hi Nikhil,

Thanks for your reply. I just wasn't using the appropriate web.config file. Had to use the one under:

  • \\Inetpub\wwwroot\wss\VirtualDirectories\ Port_Number \wpresources

Both the codes I put above works now.

Cheers!

Kishan.

December 23rd, 2014 6:35am

Hi Nikhil,

Thanks for your reply. I just wasn't using the appropriate web.config file. Had to use the one under:

  • \\Inetpub\wwwroot\wss\VirtualDirectories\ Port_Number \wpresources

Both the codes I put above works now.

Cheers!

Kishan.

Free Windows Admin Tool Kit Click here and download it now
December 23rd, 2014 6:41am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics