c# - Create ServiceEndpoint in code and in config




This is actually quite this is expecially useful when you are working with some hosted environment, where you have no control of deploying the app.config, but constructing the binding element in code gives you some advantage.

supppose that you have something in the app.config in one of the client application, and it is like this: 

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <client>
            <endpoint name="TabularPushService"
                address="net.tcp://127.0.0.1:9999/TabularPushService"
                binding="netTcpBinding"
                contract="WcfPub.ITabularPushService">
      </endpoint>
    </client>
  </system.serviceModel>  
</configuration>

With this in in app.config, you can code your program with something like this:

static void Main(string[] args)
        {
            InstanceContext instanceContext = new InstanceContext(new TabularPushCallback());
            ServiceEndpoint endpoint = new ServiceEndpoint(
                ContractDescription.GetContract(typeof(ITabularPushService)),
                new NetTcpBinding(),
                new EndpointAddress("net.tcp://127.0.0.1:9999/TabularPushService"));


            using (DuplexChannelFactory<ITabularPushService> channelFactory = new DuplexChannelFactory<ITabularPushService>(instanceContext, "TabularPushService"))
            {
                ITabularPushService proxy = channelFactory.CreateChannel();
                using (proxy as IDisposable)
                {
                    proxy.Subscribe(0);
                    proxy.Subscribe(1);
                    
                    Console.WriteLine("the table {0} {1} registered", 0, proxy.IsSubscribed(0) ? "is" : "is not");
                    Console.WriteLine("the table {0} {1} registered", 9, proxy.IsSubscribed(9) ? "is" : "is not");
                    
                    proxy.UnSubscribe(0);
                    Console.Read();
                }
            }
        }
However, you can change the code to something like this:

static void Main(string[] args)
        {
            InstanceContext instanceContext = new InstanceContext(new TabularPushCallback());
            ServiceEndpoint endpoint = new ServiceEndpoint(
                ContractDescription.GetContract(typeof(ITabularPushService)),
                new NetTcpBinding(),
                new EndpointAddress("net.tcp://127.0.0.1:9999/TabularPushService"));


            using (DuplexChannelFactory<ITabularPushService> channelFactory = new DuplexChannelFactory<ITabularPushService>(instanceContext, endpoint))
            {
                ITabularPushService proxy = channelFactory.CreateChannel();
                using (proxy as IDisposable)
                {
                    // same as before
                    Console.Read();
                }
            }
        }

Remember the rule that anything in bindingElemenmt within the config can be rewritten in code. 


Another note to add, sometimes, you don't need to explicitly create a ServiceEndpoint instance and pass that to caller which needs it. like in the case of a ServiceHost.AddServiceEndpoint methods, an example is shown as  below.

using (ServiceHost host = new ServiceHost(typeof(TabularPushService)))
            {
                host.AddServiceEndpoint(
                    typeof(ITabularPushService), 
                    new NetTcpBinding(),
                    string.Format("net.tcp://127.0.0.1:{0}/TabularPushService", arguments.Port));
                host.Open();
                Application app = new Application();
                app.Run(window);
            }
as you can see, you can pass a raw string as the endpoint value. 

转载于:https://my.oschina.net/u/854138/blog/133972

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值