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