c# - Create ServiceEndpoint in code and in config

4489 단어 C#WCF
You can create an endpoint from the config file, (app.config) ; what you can create from the config section in the app.config can also be created from the code.
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.

좋은 웹페이지 즐겨찾기