开发者

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)?


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();
    }
}


You can use the following channel factory:

using System.ServiceModel;

namespace PgAuthentication
{
    public class ServiceClientFactory<TChannel> : ChannelFactory<TChannel> where TChannel : class
    {
        public TChannel Create(string url)
        {
            return CreateChannel(new BasicHttpBinding { Security = { Mode = BasicHttpSecurityMode.None } }, new EndpointAddress(url));
        }
    }
}

and you can use this with the following code:

Console.WriteLine(
                new ServiceClientFactory<IAuthenticationChannel>()
                    .Create("http://crm.payamgostar.com/Services/IAuthentication.svc")
                        .AuthenticateUserNameAndPassWord("o", "123", "o", "123").Success);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜