Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set endpoint in runtime

I have application based on this tutorial

Method I use to test connection to server (in client app):

public class PBMBService : IService
{
    private void btnPing_Click(object sender, EventArgs e)
    {
        ServiceClient service = new ServiceClient();
        tbInfo.Text = service.Ping().Replace("\n", "\r\n");
        service.Close();
    }

//other methods
}

Service main function:

class Program
{
    static void Main(string[] args)
    {
        Uri baseAddress = new Uri("http://localhost:8000/PBMB");

        ServiceHost selfHost = new ServiceHost(typeof(PBMBService), baseAddress);

        try
        {
            selfHost.AddServiceEndpoint(
                typeof(IService),
                new WSHttpBinding(),
                "PBMBService");

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);

            selfHost.Open();
            Console.WriteLine("Serwis gotowy.");
            Console.WriteLine("Naciśnij <ENTER> aby zamknąć serwis.");
            Console.WriteLine();
            Console.ReadLine();


            selfHost.Close();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("Nastąpił wyjątek: {0}", ce.Message);
            selfHost.Abort();
        }
    }
}

In app.config I have:

    <client>
        <endpoint address="http://localhost:8000/PBMB/PBMBService" binding="wsHttpBinding"
            bindingConfiguration="WSHttpBinding_IService" contract="IService"
            name="WSHttpBinding_IService">
            <identity>
                <userPrincipalName value="PPC\Pawel" />
            </identity>
        </endpoint>
    </client>

I can change IP from here. But how can I change it during runtime (i.e. read address/IP from file)?

like image 340
Ichibann Avatar asked Jun 21 '11 20:06

Ichibann


1 Answers

You can replace the service endpoint after you created your client class:

public class PBMBService : IService
{
    private void btnPing_Click(object sender, EventArgs e)
    {
        ServiceClient service = new ServiceClient();
        service.Endpoint.Address = new EndpointAddress("http://the.new.address/to/the/service");
        tbInfo.Text = service.Ping().Replace("\n", "\r\n");
        service.Close();
    }
}
like image 170
carlosfigueira Avatar answered Sep 22 '22 17:09

carlosfigueira