I am creating a windows mobile 6 application which will consume a web service (.asmx) for different clients.
As I know, I will need to manually “Add Web Reference”; then I will be able to call those functions.
Is it possible to configure web reference as a variable from code behind?
That way I can keep the url of web service in a text file. For different client, I just need to edit that text file instead of recompile that application again.
You'll have to add the Web Reference at design time.
At runtime, you can modify the URL of your target web service using the Url property. Here's an example of pulling the target URL from the app.config:
var ws = new MyWebService();
ws.Url = ConfigurationManager.AppSettings["SomeUrl"].ToString();
The only catch here is that the WSDLs of the design-time and run-time services must match.
Yes, just add something like :
<configuration>
<appSettings>
<add key="WebReference" value="URLofASMX"/>
...
then call it by :
string URL = ConfigurationManager.AppSettings["WebReference"].ToString();
You'll need to possibly add a new reference to System.Configuration to the project if you can't access ConfigurationManager just by including System.Configuration.
Related
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.
I have added the odata webservice reference using the Add Service Reference option from visual studio. But client suggesting us to do that through coding by having the url in web.config.
In future if they has to switch to different url then it will be easy for them to add it .
That is easy enough to do.
You just keep your Service Reference because it contains the contract of the service. You can store the URL in your config file.
<appSettings>
<add key="ServiceURL" value="http://somewhere.com/Service" />
</appSettings>
and before you call the service, modify its URL by
reference.Endpoint.Address = ConfigurationManager.AppSettings["ServiceURL"];
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.
I added a .NET 2.0 web reference with the Visual Studio IDE, but for this specific case it can't be configured using the app.config file.
So I tried to initialize it this way:
var service = new MyWebservice
{
Url = config.MyWebserviceUrl
};
But I get the following exception:
Value cannot be null. Parameter name: uriString
config.MyWebserviceUrl contains a valid web address.
What is the correct way to configure and initialize a webservice programatically?
Have you checked the URL Behavior setting for the web reference? If it's Dynamic, it will be attempting to get the URL from the config file.
This question covers programmatic configuration of the URL in more detail:
How can I dynamically switch web service addresses in .NET without a recompile?
I have a web service on my c# application. I want to update webservice url when i clicked button. How can i do this.
The web service endpoint address is usually defined in the application's config file, but you can also specify the url in the client's constructor:
MyClient client = new MyClient("http://95.9.5.151/SabisOdemeWS/Service1.asmx");
To make the URL in the Reference.cs map class code behind look for the web service URL in your web.config file, we need to change this setting to Dynamic. See the article here.