如何设置在运行时的端点 [英] How to set endpoint in runtime

查看:174
本文介绍了如何设置在运行时的端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据本教程

方法我用它来测试连接到服务器(在客户端应用程序):

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
}

服务的主要功能:

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

的app.config 我有:

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



我可以从这里更改IP。但是,我怎么能在运行时改变它(即从文件中读取地址/ IP)?

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

这篇关于如何设置在运行时的端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆