First look into Visual Studio Community 2013

Yesterday Connect(); Event, Scott Guthrie and Soma Somasegar made a number of important announcements for the cloud-first, mobile-first developer. Visual Studio Community 2013 is a new free, fully-featured edition of Visual Studio that lets developers target any platform, from desktop and mobile to web and cloud.

Visual Studio Community 2013

Visual Studio Community 2013

Visual Studio Community 2013 also supports full Visual Studio extensibility, offering access to the ecosystem of over 5000 extensions. You can download it from here. You can also try Visual Studio Community 2013 in Azure VM image.

What is new in Visual Studio Community 2013

  • Professional-grade editing, code analysis, and debugging support – Your favourite Refactor and Debugging features are included.
  • Support for open-source workflows (Git).
  • Compilers for managed languages, C++ and more
  • Cross-platform mobile development including the web, Android, iOS, and Windows Phone with the free Visual Studio Tools for Apache Cordova extension.
  • Take advantage of cloud services with simplified Azure SDK integration, and incorporate modern app analytics and telemetry with Application Insights. Application Insights collects, processes and presents a wide variety of telemetry including performance, usage, availability, exception, crash, environment, log and developer-supplied data from all components of a modern application – including clients (devices and browser), servers, databases and services.
    Application Insights

    Application Insights

  • Access to all the Visual Studio 2013 extensions on the Visual Studio Gallery

Visual Studio Community 2013 includes Update 4, which is a cumulative update of all previous Visual Studio 2013 updates.

Happy Programming :)

How to Implement Ad-Hoc Discovery in WCF

WCF 4.0 introduces a couple of service discovery techniques, which the client can use to discover the services dynamically through client probing or service announcement. WCF 4.0 comes up with the ability of dynamic discovery. This feature allows the client to probe for the services or reversely the services can also announce their presence on the network.The Discovery API supports two different modes: Managed and Ad-Hoc.

In Managed mode there is a centralized server called a discovery proxy that maintains information about available services. Clients use the discovery proxy to retrieve information about available services. When a client searches for a service it sends a Probe message to the discovery proxy and the proxy determines whether any of the services it knows about match the service the client is searching for. If there are matches the discovery proxy sends a ProbeMatch response back to the client. The client can then contact the service directly using the service information returned from the proxy.

In Ad-Hoc mode, there is no centralized server. All discovery messages such as service announcements and client requests are sent in a multicast fashion. When a client sends a Probe message for a service it is sent over the network using a multicast protocol. Each service that receives the request determines whether it matches the criteria in the Probe message and responds directly to the client with a ProbeMatch message if the service matches the criteria specified in the Probe message.
This post is about implementing Ad-Hoc Discovery in WCF, which is simple I feel compared to Managed mode.
Here is a HelloService, which has a SayHello method, which accepts a name parameter, and returns a string.

[ServiceContract]
interface IHelloService
{
    [OperationContract]
    string SayHello(string name);
}

class HelloService : IHelloService
{
    public string SayHello(string name)
    {
        return string.Format("Hello {0}", name);
    }
}

To enable Ad-Hoc discovery, we need to add ServiceDiscoveryBehavior to the service’s behavior collection. This behavior indicates to the WCF runtime that the service supports the WS-Discovery protocol.

var discoveryBehavior = new ServiceDiscoveryBehavior();
serviceHost.Description.Behaviors.Add(discoveryBehavior);

Next we need to add UdpDiscoveryEndpoint to the list of service endpoints so that our service can start listening for WS-Discovery messages.

serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

To announce the existence of the service in the network, we need to add UdpAnnouncementEndpoint to the service’s endpoints.

discoveryBehavior.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint());

Here is the whole snippet, which combines all the above steps.

var baseAddress = new Uri("http://localhost:8080");
using (var serviceHost = new ServiceHost(typeof(HelloService), new[] { baseAddress }))
{
    serviceHost.AddServiceEndpoint(typeof(IHelloService), new BasicHttpBinding(), "HelloService");
    var metadataBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>() ?? new ServiceMetadataBehavior();
    serviceHost.Description.Behaviors.Add(metadataBehavior);
    serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "Mex");
    // make the service discoverable by adding the discovery behavior
    var discoveryBehavior = new ServiceDiscoveryBehavior();
    serviceHost.Description.Behaviors.Add(discoveryBehavior);
    // send announcements on UDP multicast transport
    discoveryBehavior.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint());
    // add the discovery endpoint that specifies where to publish the services
    serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

    serviceHost.Open();
    serviceHost.Description.Endpoints.ToList().ForEach(endpoint => Console.WriteLine(endpoint.Address));
    Console.ReadKey();
    serviceHost.Close();
}

You will get something like this, if you build and run the console application. You can see the third address, which the address of discovery endpoint.

WCF AdHoc discovery endpoints

WCF AdHoc discovery endpoints

And you can create the proxy / client using DiscoveryClient class. Here is a sample snippet which will create a proxy for the HelloService. In order to dynamically discover services using the Ad-Hoc model, a WCF client instantiates a DiscoveryClient that uses discovery endpoint specifying where to send Probe or Resolve messages. The client then calls Find that specifies search criteria within a FindCriteria instance. If matching services are found, Find returns a collection of EndpointDiscoveryMetadata. Here is the code snippet

var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
var discoveryResponse = discoveryClient.Find(new FindCriteria(typeof(IHelloService)));
var address = discoveryResponse.Endpoints[0].Address;
Console.WriteLine(address);

This will print out the address of the service.

Happy Programming :)

How to Self hosting WCF Data Service

WCF Data Services (formerly known as “ADO.NET Data Services”) is a component of the .NET Framework that enables you to create services that use the Open Data Protocol (OData) to expose and consume data over the Web or intranet by using the semantics of representational state transfer (REST). OData exposes data as resources that are addressable by URIs. Data is accessed and changed by using standard HTTP verbs of GET, PUT, POST, and DELETE. OData uses the entity-relationship conventions of the Entity Data Model to expose resources as sets of entities that are related by associations. – From MSDN. Last few days I got chance to explore WCF DataServices, but most of the code I found was using IIS for hosting. Here is some sample code which helps to host OData using self hosting. I am using a Console Application to host the WCF Data Service.

class Program
{
    static void Main(string[] args)
    {
        var baseAddress = new Uri("http://localhost:8080/");
        var host = new DataServiceHost(typeof(DataContextService),
            new[] { baseAddress });
        host.Open();
        Console.WriteLine("Service started. Press  to exit");
        Console.WriteLine("Listening in following endpoints");
        host.Description.Endpoints.
            ToList().ForEach(endpoint =>
                Console.WriteLine(endpoint.Address));
        Console.ReadKey();
        host.Close();
    }
}

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class DataContextService : DataService
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.DataServiceBehavior.MaxProtocolVersion =
            DataServiceProtocolVersion.V2;
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.UseVerboseErrors = true;
    }
}

This code requires following references

  • System.Data.Services
  • System.Data.Services.Client
  • System.ServiceModel
  • System.ServiceModel.Web

Here is my screen shot of console application.

Console application - Self hosting WCF Data service

Console application - Self hosting WCF Data service

And here is the sample output while navigating to the url using IE.

Accessing self hosted WCF Data service using IE

Accessing self hosted WCF Data service using IE

You need to turn off the Feed read view to see this output in IE.

You need to run the Application as Administrator, otherwise you may not able to create the host instance.

Next post(s) I will try to cover accessing and updating data from client side.

Reviewing uCertify 70-513-CSharp MCTS exam prepkit

I have got a offer from them to review their PrepKit for MCTS 70-513, C#.NET 4 WCF Development(which you can find here) and I have accepted that challenge. Initially it looks fully-featured and with a nice UI, I haven’t had the time to make a few tests and that’s what counts most, so I’ll leave any further review opinions for later. As soon as I am done with the full review I will post it for you all.