CSharpFeeds - All your C# feeds in one place.

Sponsors

Friday, July 06, 2007

Accessing to Service Description in WCF Programmatically

by Keyvan Nayyeri via Keyvan Nayyeri on 7/6/2007 5:47:34 PM

Getting access to a service description information can be a common scenario for some situations.  Doing this is easy in Windows Communication Foundation.  There is a built-in mechanism to do this via the OperationContext class.  By creating an OperatingContext object for current context and using the Description property of its Host property you have access to some properties and methods to deal with service description.

As an example, suppose that I have a service operation like what you see below.  In this service I return some properties of my service description to client like the name of service description, number of endpoints and number of defined behaviors.

using System;

using System.Runtime.Serialization;

using System.ServiceModel;

 

namespace RuntimeInfo

{

    [ServiceContract]

    public interface IMyService

    {

        [OperationContract]

        string GetServiceDescriptionName();

 

        [OperationContract]

        int GetServiceEndpointsCount();

 

        [OperationContract]

        int GetServiceBehaviorsCount();

    }

}

My service description uses the OperationContext.Current object to get access to service description information.

using System;

using System.ServiceModel;

 

namespace RuntimeInfo

{

    public class MyService : IMyService

    {

        #region IMyService Members

 

        public string GetServiceDescriptionName()

        {

            OperationContext context = OperationContext.Current;

 

            return context.Host.Description.Name;

        }

 

        public int GetServiceEndpointsCount()

        {

            OperationContext context = OperationContext.Current;

 

            return context.Host.Description.Endpoints.Count;

        }

 

        public int GetServiceBehaviorsCount()

        {

            OperationContext context = OperationContext.Current;

 

            return context.Host.Description.Behaviors.Count;

        }

 

        #endregion

    }

}

Now I host my service on IIS using a configuration file like this:

<?xml version="1.0"?>

<configuration>

  <system.serviceModel>

    <services>

      <service name="RuntimeInfo.MyService"

              behaviorConfiguration="metadataSupport">

        <endpoint contract="RuntimeInfo.IMyService"

                  binding="basicHttpBinding"/>

        <endpoint address="mex"

          binding="mexHttpBinding"

          contract="IMetadataExchange"/>

      </service>

    </services>

 

    <behaviors>

      <serviceBehaviors>

        <behavior name="metadataSupport">

          <serviceMetadata />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>

With a simple code for client to get the returned values of service operations and write them in console, I have what I was looking for:

static void Main(string[] args)

{

    Console.Title = "Accessing to Service Description in WCF Programmatically";

 

    using (MyServiceClient proxy = new MyServiceClient())

    {

        Console.WriteLine("Service Description Name: {0}",

            proxy.GetServiceDescriptionName());

        Console.WriteLine("Service Endpoints Count: {0}",

            proxy.GetServiceEndpointsCount().ToString());

        Console.WriteLine("Service Behaviors Count: {0}",

            proxy.GetServiceBehaviorsCount().ToString());

    }

 

    Console.ReadLine();

}

Output

There are some other properties available for service description that you can use whenever you need them.

email it!bookmark it!digg it!

Original Post: Accessing to Service Description in WCF Programmatically

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us