Binding can be configured either through configuration file or
Programming. Let us see the binding representation in each method.
Note:
It is always good if you configure the binding properties using
configuration file, because while moving to the production you no need
to change in the code and recompile it. It is always good practice to
represent in the configuration file.
Administrative (Configuration file):
In the configuration file of the hosting application, you can add the <bindings> element inside the <system.serviceModel> element and add the properties to particular binding type. Properties corresponding to the particular binding type can be mentioned below. Name of the binding properties that you are going to use has to be mention in the end point.<system.serviceModel> <services> <service name="MyService"> <endpoint address="http://localhost/IISHostedService/MyService.svc" binding="wsHttpBinding" bindingName="wshttpbind" contract="IMyService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <bindings> <wsHttpBinding> <binding name="wshttpbind" allowCookies="true" closeTimeout="00:01:00" receiveTimeout="00:01:00" /> </wsHttpBinding> </bindings> </system.serviceModel>
Programming Model:
In the following code, I have created the WSHttpBinding object and assign the properties which to be configured. This binding object is added to the Service endpoint for client communication. Similarly you can also create any type of binding and add to endpoint.//Create a URI to serve as the base address Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator"); //Create ServiceHost ServiceHost host = new ServiceHost(typeof(MyCalculatorService.SimpleCalculator), httpUrl); //Create Binding to add to end point WSHttpBinding wshttpbind = new WSHttpBinding(); wshttpbind.AllowCookies = true; wshttpbind.CloseTimeout = new TimeSpan(0, 1, 0); wshttpbind.ReceiveTimeout = new TimeSpan(0, 1, 0); //Add a service endpoint host.AddServiceEndpoint (typeof(MyCalculatorService.ISimpleCalculator), wshttpbind, ""); //Enable metadata exchange ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; host.Description.Behaviors.Add(smb); //Start the Service host.Open(); Console.WriteLine("Service is host at " + DateTime.Now.ToString()); Console.WriteLine("Host is running... Press key to stop"); Console.ReadLine();
No comments:
Post a Comment