This is a mirror of official site: http://jasper-net.blogspot.com/

Creating your own (customized) standard endpoints in WCF 4

| Thursday, December 8, 2011
Lately I’ve been writing and speaking a lot about WCF 4.5, but while delivering my “What’s new in WCF 4” session in Visual Studio Live yesterday I realized that there is one feature of WCF 4 that most people are not aware of, and do not really understand how useful it is – Standard Endpoints.

In WCF we always have to specify a set of address+binding+contract (ABC) for our endpoints. If our endpoints also need to be configured, for example – change the binding configuration, or the endpoint behavior, then we need to add some more configuration. We can use default configuration (another feature of WCF 4), but if we have two common settings, we cannot set two defaults and we’re back to square one.

Standard endpoints change the way we define endpoints – with standard endpoints we specify a special “kind” name in our endpoint, which automatically sets our endpoint’s address, binding, contract, binding configuration, and endpoint behavior.

For example – if we define the following endpoint:

<endpoint address="mex" kind="mexEndpoint"/>

The above endpoint will automatically be set with the mexHttpBinding and the IMetadataExchange contract.

If we define the following endpoints:

<endpoint address="web" kind="webHttpEndpoint" contract="MyNS.IMyContract"/>

We will get an endpoint which uses webHttpBinding, and automatically gets the webHttp endpoint behavior.

Although this is quite nice, this is the least we can do with standard endpoints. The real use of standard endpoints is when you create some of your own.

Image the following – you are a part of an infrastructure team in your organization and you need to explain to the dev teams which endpoint configuration they should use in their projects – “Please use NetTcp binding, with increased message size limits, with either security none or transport, and don’t forget to increase the send timeout”.

One way to do this is to send a memo to all the dev teams, hoping everyone follow your instructions to the letter. Another way you can do that is to create your own standard endpoint with all of the above configuration and just send it to the dev teams to use.

First of all you need to create your custom endpoint:


   1:  public class CompanyNameStandardEndpoint : ServiceEndpoint
   2:  {
   3:      private bool _isSecured;
   4:  
   5:      public CompanyNameStandardEndpoint(ContractDescription contract)
   6:          : base(contract)
   7:      {
   8:          this.Binding = new NetTcpBinding();
   9:          ResetBindingConfiguration(this.Binding);
  10:          this.IsSystemEndpoint = false;
  11:      }
  12:  
  13:      public bool IsSecured
  14:      {
  15:          get
  16:          {
  17:              return _isSecured;
  18:          }
  19:          set
  20:          {
  21:              _isSecured = value;
  22:              if (_isSecured)
  23:              {
  24:                  (this.Binding as NetTcpBinding).Security.Mode = SecurityMode.Transport;
  25:              }
  26:              else
  27:              {
  28:                  (this.Binding as NetTcpBinding).Security.Mode = SecurityMode.None;
  29:              }
  30:          }
  31:  
  32:      }
  33:  
  34:      // Receive a dynamic object instead of creating separate methods
  35:      // for netTcp, basicHttp, WSHttpBinding...
  36:      private void ResetBindingConfiguration(dynamic binding)
  37:      {
  38:          binding.SendTimeout = TimeSpan.FromMinutes(5);
  39:          binding.MaxReceivedMessageSize = Int32.MaxValue;
  40:          binding.MaxBufferSize = Int32.MaxValue;
  41:      }
  42:  }


Read more: Ido Flatow's Blog
QR: creating-your-own-customized-standard-endpoints-in-wcf-4.aspx

Posted via email from Jasper-net

0 comments: