Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 26-Jun-13 23:16pm
Comments
Mahesh Bailwal at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
 
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
 
    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
 
       WCFHelper client = new WCFHelper();
            lstVenue.ItemsSource = client.GetSomething();
            client.Close();
  Permalink  
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 162
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100