Click here to Skip to main content
15,886,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every one
I am trying to call a service written using c# and WCF project of .net .
The location of the host is not stable and the IP will change time by time. I need to call the service without adding the service reference to the client.
I found thisHow to make your Web Reference proxy URL dynamic[^] article from code project but when I publish my client I do not have any ,config file to change the url in it.
Can anyone help me to find how to publish my client which will also contain a .config file or generally what is the appropriate way to publish a project in VS.
best regards
Posted
Comments
Mahesh Bailwal 27-Jun-13 5:11am    
You should use web.config in case of web application and App.config in case of windows application for WCF configuration. If you don not see any config file then you can add it to your project manullay.

http://msdn.microsoft.com/en-us/library/ms731745.aspx

You will definitely have app.config or web.config as it is required to specify the end point connections

First Create a ConfigHelper to Edit the address

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;



    public class ConfigHelper
    {
        private static string NodePath = "//system.serviceModel//client//endpoint";
        private ConfigHelper() { }

        public static string GetEndpointAddress()
        {
            return ConfigHelper.loadConfigDocument().SelectSingleNode(NodePath).Attributes["address"].Value;
        }

        public static void SaveEndpointAddress(string endpointAddress)
        {
            // load config document for current assembly
            XmlDocument doc = loadConfigDocument();

            // retrieve appSettings node
            XmlNode node = doc.SelectSingleNode(NodePath);

            if (node == null)
                throw new InvalidOperationException("Error. Could not find endpoint node in config file.");

            try
            {
                // select the 'add' element that contains the key
                //XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
                node.Attributes["address"].Value = endpointAddress;

                doc.Save(getConfigFilePath());
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static XmlDocument loadConfigDocument()
        {
            XmlDocument doc = null;
            try
            {
                doc = new XmlDocument();
                doc.Load(getConfigFilePath());
                return doc;
            }
            catch (System.IO.FileNotFoundException e)
            {
                throw new Exception("No configuration file found.", e);
            }
        }

        private static string getConfigFilePath()
        {
            return Assembly.GetExecutingAssembly().Location + ".config";
        }
    }


Then create a "Helper Class" that inherits the client class

C#
class WCFHelper : WCFClient
{
    public WCFClientHelper()
    {
        var endpointAddress = Endpoint.Address;

        EndpointAddressBuilder newEndpointAddress = new EndpointAddressBuilder(endpointAddress);
        newEndpointAddress.Uri = new Uri( ConfigHelper.GetEndpointAddress());
        this.Endpoint.Address = newEndpointAddress.ToEndpointAddress();
    }
}

Use this helper class whenever you want to use the Service instead of using the client class

C#
WCFHelper client = new WCFHelper();
     lstVenue.ItemsSource = client.GetSomething();
     client.Close();
 
Share this answer
 
v5
I found a simple way to do what I wanted to do.
I found many pages in the internet addressing the .config file but I was not able to find the file.
This is the way that I solved my problem:

there is a .config file in the project solution in the visual studio. If the address in this file is correct then the published program will not look for any other file such as .config file to get the address of the service. so I changed the address specified in the app.config file in the project solution to a nonsense address.
Then without publishing the project, I only pressed f6 to refresh the related file to the project.
I moved to the path where my project is saved. For me it is: C:\Users\user\Documents\Visual Studio 2012\Projects\my project
Then I moved to bin directory and then debug directory
Here I found the .exe file of my solution and a .config file which is the right file that I was looking for.
I copied the .exe and .config file in another directory. Now when I run the .exe file it will look to the .config file that I copied just beside it. If the address of the web service in the .config file is correctly defined then I can access the methods in the web service from this client.

I wish it will also help others who are looking for addressing web service from an external file without recompiling the solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900