Reload service configuration for ServiceHost? - c#

We have a WCF SerivceHost (self-hosted), XML-configured in App.config. The host has already been opened.
We want to change a specific configuration value in the service's binding at runtime (from code).
Unfortunately, our current approach:
((SomeBinding) _serviceHost.Description.Endpoint.Single().Binding).SomeProp = value;
does not work. The configuration is not "applied". How to force a configuration reload at runtime?

A possible approach is this:
Modify the value in the config file at runtime (cannot find instructions for doing this, but the approach here may give you a start)
Reload the config from file using:
ConfigurationManager.RefreshSection("system.serviceModel/bindings");
I don't think your current approach of having the config initially set in xml and then trying to update it via code once the service host has started will work.

Related

How do you pull values from a linked App.config in Web application when Web.config exists?

I have a mvc .NET web application written in C# and I have a web.config file associated with it for web specific configuration values. I also have a windows service application that will be running on the server in the background that has a App.config associated with it. I have linked the file within the web application and can see the file with updated values. But I am unable to use those values in my controller to display them to the UI. Is there a way to make a call to the app.config values to use in the controller and views of the web application? Right now it seems like they are coming in null due to them not being in the web.config.
Any help is apprecaited.
As long as permissions are worked out, you should be able to open the shared config file thusly:
var map = new ExeConfigurationFileMap();
//TODO: resolve this path in whatever way makes sense for your situation.
map.ExeConfigFilename = #"C:\MyConfig.config";
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
//do something with config, e.g. config.AppSettings.Settings["Blah"];
Otherwise, you can do something like put shared settings into machine.config, but it's typically wise not to mess with that file.

Dynamically change azure configuration (.cscfg) values

I'm having azure worker role project. below is a code from my configuration (.cscfg).
c# (FLAG = 1;)
Through c# code i want to change this value.
Is there any way for dynamically changing value of an azure service configuration?
Please instruct me how to do it.
Simple answer is that you can't change the value of just one setting through code as RoleEnvironment class only exposes a Get method and there's no Set method.
A convoluted approach would be to invoke Service Management API, read the configuration settings file using Get Deployment operation, change the XML file and the call Change Deployment Configuration operation. Not sure how this would work out if you're changing the configuration from the same role though.

Host WCF in Windows Forms: Error when opening the host

I am trying to start and stop a WCF service library through a windows desktop application but got stuck. I cannot start it because it gives me error in the shost.Open();
Code:
private void startwcfedcHost()
{
ServiceHost shost = new ServiceHost(typeof(WcfServiceLibrary.Service));
shost.Open();
}
Error:
Service 'WcfServiceLibrary.Service' has zero application (non-infrastructure)
endpoints.
This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
But when I try to run my wcf service it works, How can I fix this issue?
Since you don't specify the endpoints via code, you need to specify them via configuration. What you probably have is a missing configuration file. Change the Main method (if a console application; something like the Page_Loaded event if you're writing a windows app) to print the following value:
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
That will show the name that the application expects its configuration file to be. Once you have that, make sure that that file exists, and it has the appropriate <system.serviceModel> section to define the service endpoints.
I suggest you take a look at the following:
Here
WCF is about A(address) B(binding) C(contract), you need to specify binding.

Program cannot find app.config

// Set LastRun to now
config.AppSettings.Settings["LastRun"].Value = DateTime.Now.ToString();
// Save all settings
config.Save(ConfigurationSaveMode.Modified);
This code was working fine in my development server but not in my production server. It seems like my program is unable to communicate with my app.config file. I have checked all the "obvious" . . Any ideas ... ?
From your code example, I cannot tell how your config variable is initialized. But, from the comments, you have a web app. Unless you are attempting to load a specific app.config file, the web app will attempt to get AppSettings from web.config.
It's not a good idea to programatically change the values of web.config. Changing web.config will cause an application restart.
If you have a different app.config for storing this type of information, that would be better than trying to change web.config. But you'll have to specifically load the file, something like this:
Configuration config = WebConfigurationManager.OpenWebConfiguration("yourPath\app.config");
ConfigurationManager.OpenExeConfiguration() is intended for use within an executable application not a web app. Try using WebConfigurationManager as shown above.
You find some more information in this SO question/answers.
More information can be found in this SO question/answer.

Soap server address in silverlight

In my silverlight light apps im normally connecting to a ASMX or WCF web service.
I was wonder what is the best way of holding the url in the app of where the soap client should be looking at.
This is fine in debug because it just sort of knows, but in release and depending where its using an ip or it swaps production servers i keep getting tripped up.
I would love there to be some sort of client config bit like web config
Your service endpoints can go in to a ServiceReferences.ClientConfig file. This is then an XML file and can be changed at any point as needed; ie..at deployment, etc...
You can have client configuration by using Isolated Storage.
You work with Key/Value pairs as in this example from the linked page:
// Create an instance of IsolatedStorageSettings.
private IsolatedStorageSettings userSettings =
IsolatedStorageSettings.ApplicationSettings;
// Add a key and its value.
userSettings.Add("userImage", "BlueHills.jpg");
// Remove the setting.
userSettings.Remove("userImage");
So you can have the value as your URL - just choose a suitable key.

Categories