The Power of WCF Behaviors 2 – Service Behavior

A very useful usage of ServiceBehavior is to remove those “http://tempuri.org” in the wsdl file, see solutions here. For those people don’t like magic string, they can use contants in Csharp code, but how about the string in web.config?

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ServiceEndpoint se in serviceDescription.Endpoints)
            {
                // Must not use the default namespace.
                var ns = se.Contract.Namespace;
                if (ns.Equals("http://tempuri.org/"))
                    throw new InvalidOperationException(
                        "Your web service must set qualified namespace, format like urn://mydomain/projectname/");
                if (se.Binding.Namespace != ns) se.Binding.Namespace = ns;
              }
          }

But the most popular usage of ServiceBehavior is to add ErrorHandler extension, all the Exceptions can be converted to Fault in ServiceBehavior so-called Exception Shielding.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s