Now continuing from the last section. Let’s look a quick demo of writing a simple WCF service. Below, in the screen shot.
Now, i am going to delete the default implementation presented by visual studio and new class EmployeeService. So, below implementation has got one concrete class which acts DataContract, means composed of properties supposed to sent across wire, then one interface which is having two operations. 1st one to Submit the employee and 2nd one to Print the employee.
Now, if you see in the above implementation you will come to know it has got all the required attributes which is needed for WCF service implementation say
A simple Data Contract class
An Interface which is basically a set of operations
And then one concrete class which is basically implementing these
Now, one point to note here, is above class implementation is the default interface implementation. Now, I am going to modify the same to look like as shown below. Also, at this point now i can go ahead and add the service behavior on our service class by specifying InstanceContextMode to single which means within the service host, we will be having single instance of service running in memory and it will be used to serve all incoming messages. Now, the one advantage associated with this is now, i can have a list of employees in memory and then whenever Submit employee is called, it will simply add the employee into that list and in case of Print employee, it will simply return the result from the memory.
So, now at this point i can go ahead and build the project. And, once the build is succeeded i am ready to host this. But, before that i need to modify my App.config file so that it has the correct contract and service name. Below is the modified app.config file.
Now, with this change in place i can go ahead and host the service in Visual Studio Test tool. So, in order to do that i just need to click F5 and then little window 1st appear in the tray icon saying that your service is hosted.
Now, from WCF test client i can go ahead and test my operations.
so, in the above screen shot i have specified the required parameters and then clicked on invoke button to add the same in memory. Now, in order to retrieve the same i can just go ahead invoke print employee as shown below in the screen shot.
Now, with this i would like to wrap this session here, in the next segment will delve further into WCF configurations. Till then stay tuned and Happy Coding.
Today, in this section we’ll continue from where we have stopped in the last section. Till now, i assume that you guys got some basic familiarity with WCF Programming. Now, in this section, we are going to cover further on the same lines.
So, let’s get started. Basically Data contracts are composed of elements which is supposed to get transferred across the wire via messaging or precisely via WCF Messaging. Basically messages are usually in XML format. But, it can also be in JSON, MTOM, Binary… Now, for writing any Data contract we need to rely on Data Contract Serializer. Data Contract Serializer is the default Serializer which is being used by WCF. Now, this is bit different from general XML serializer what we used to have in web services. So, basically with XML Serializer, it would take any public class and serialize any public properties without any explicit annotation. On the other hand, Data Contract Serializer requires one to explicitly specify types telling the serializer what you want to include in the message. Now, we do the same by explicit mapping which sits in the namespace “System.Runtime.Serialization“. Now, the attributes here which we are going to use is
Data Contract and
Below, we have a simple class which we need to convert as a Data Contract Type.
Now. here we added Data Contact before the class to make it serializable via Data Contract Serializer. Also, we need to annotate each property with DataMember in order to include the same in the message. One point to note here, if you skip Data Member annotation on any of the properties, that particular field won’t be available in the message sent across. Also, DataMember annotation can be done on both public/private properties.
Now, let’s look at the concept of defining Service Contracts. Service Contracts can be defined via .Net Interfaces. So, here Interface includes all set of operations that you want to expose. Now, each operation in the interface has got signature or annotation get regarded as WCF Operation. Then, you can also annotate Service contract with attributes to participate in the message exchange. Now, let’s look at the below sample code.
Now, one point to note here, that let’s suppose you have a set of operations and one of the operation you didn’t annotate with [OpeartionContract], then in that case it won’t participate in Service Operation. Now, once we have service contract setup done, then we need to implement the service. Now, for implementing the service we need to write a class which derives interface. Now, one class can implement multiple service implementation which means class can derive multiple interface. Basic example of service implementation is shown below.
Now, there are certain attributes which you can apply on service implementation to check the service behavior like shown in the below sample
so, here i have annotated my service implementation with ServiceBehaviour and OperationBehaviour. Here, ServiceBehaviour basically controls the instancing mode and concurrency mode of my service which generally affects the threading model of my service. And operation behavior basically checks the security related checks. So, these behaviors impact how the service executes. So, we can control instancing and threading property from service behavior.Now, let’s talk a bit about InstanceContextMode and ConcurrencyMode property as shown below in the diagram.
Now, InstanceContextMode property gives you three option say PerCall, Single and PerSession. Now,
Percall means every incoming message will get a new instance of your service.
Single means there will be a Singleton instance which will handle all the incoming messages.
And PerSession means each unique client will get it’s dedicated service instance.
Now, on the other hand Concurrency Mode allows you to control the threading model of your service and it also offers three options as shown above.
Single means only a single thread will be allowed at a time.
Multiple means multiple threads will be allowed in the system
And Reentrant is a special type of single where in only a single thread will be allowed in a time, but when there is a an outbound call pending, it will allow another incoming thread.
Now, in the later section we’ll see in detail implementation of each of one. Like when to select which option. Till then stay tuned and Happy Coding.
Now with previous theoretical concepts, let’s jump into some practical implementations for the same. So, what i do i simply go ahead and launch my service which i have written earlier for WCF Demo. So, below in the screen shot, test client gets popped from VS 2013. Notice that our Demo service is up and running now.
Now, from this Host, i can go ahead and copy metadata and paste the same in notepad for later reference, which i’ll be using for creating my client.
Now, at this instant, i will return to my Visual Studio and add new project basically to build my client which an talk to the service. So, what i’ll do, i will simply go ahead and add simple console app to talk to this service as shown below.
Then, i can simply go ahead and add reference to my client project with the metadata which i copied initially. For doing the same, i’ll simply click on references and say add service reference as shown below
Then, it will open dialog box, wherein i will simply paste the metadata and click on go.
so, here it downloaded the metadata and showed me the service contract available for the service and also the operation. So, below in the dialog either i can change my service reference name or keep the same, but i am going to make it EmployeeServiceReference and press ok. So, as soon as i clicked ok, it downloaded the metadata and generated all required components for me. In the below screen shot, 1st thing which you will notice it generated all the assemblies required to run the WCF, then it created one App.config file which is nothing but the client side configuration file having the endpoints in it.
It also created service reference in there. So, now if you right click the service reference, you can see all the object level details in the object browser like what properties we created while creating the class, methods etc.
Now, the very 1st thing which we need to do here in our code is to include the client side reference as shown below in the screen shot.
Then, i can go there and instantiate my employee client as shown below in the screen shot. Now, when i instantiate this, it has to be done against one end point
Now, in order to get the endpoint details at the client side, you can open the app.config file at the client side and check what Visual Studio has created for you like shown below
Now, from these options i will select any endpoint of my interest and paste in there. Now, it will construct the communication channel based on the endpoint. Then i need to generate the required information like properties which i used while writing the class then fill the same with data and then in the end i’ll just invoke the method and print the same on the console.
Now, i need to do some setting in the project to make sure that it will run in my console client app. So, for that i’ll come to my service project and click on properties and then
Now, here i need to change this name with our client app which is “ClientDemo.exe”. And then i also need to assign the working directory of my client till debug as “C:\Rahul\Books\WCF\WCFServiceDemo\ClientDemo\bin\Debug\” Now, with that change in place i am good to go.
So, with this output i would like to wrap this session here. Will see in another session more about WCF and it’s terminologies. Till then stay tuned and happy coding.
In today’s post i will talk more about Endpoints and Services. So, basically in the last session we saw how to write a simple WCF program and host the same. So, basically with WCF we write services which expose end points to the outside world. Here, our service implementation will define the actual business logic and endpoints define the communication logic. Basically, endpoints serve various communication logic. We’ll see some of these in some time. Now, below is the diagram which explains the relationship between services and endpoints. So, basically services expose multiple endpoints to it’s consumers and consumers then utilize this service via endpoint.
Now, the question arises here is that what is an Endpoint? well endpoint is basically an information that tells WCF how to setup communication channel. Now, each endpoint address consists of three Major things.
Now, below is the diagram which shows the actual meaning of endpoint in a nutshell.
EndPoints consist three major things. These are
Address:- Basically tells where to send the messages.
Binding:- How to send them.
Contract:- What messages will contain.
Now, these are basically A,B,C of WCF. Address defines network address for sending and receiving messages. Here, WCF runtime will produce a communication channel which will be served as listener for these messages. Then comes the binding which defines the protocol for how to send messages like it specifies what kind of Transport Protocol needs to be used, what message format and what WS-* protocol you want to use. Then, we have Contract, which tells what the messages must contain. So, each endpoint contains three basic information. Hence, services exposes endpoints and client consume them.
Now, let’s discuss binding in detail. In the below table, you can see some of the built in bindings and it’s usage scenarios.
So, as you see in the table 1st three bindings all use HTTP. So, here Transport mechanism is also associated with the binding name. The 1st one provides Restful style communication whereas other two provides SOAP style communication. We’ll understand all these styles of communication in detail in coming sessions. Now, one important point to note here that these 1st three bindings are meant for interoperability which means they will use XML message format for communication.Now, the remaining bindings shown here are basically not meant for interoperability. Basically, they assume that WCF is used on both sides as these bindings are .Net Specific. That is why their naming also begins with net which means .Net to .Net communication. Now, irrespective of these built in bindings, you can always write your custom binding which could be a mix of these built in bindings. By the way, before selecting any binding you should always know which one would be best suited in your case.
Now, let’s see the process of consuming WCF Service. Now, in order to consume the WCF service, client need to know several things like
Where to send the message
How to send the message
Then what the messages are going to contain
So, basically these are the endpoints of any WCF service. So, in order to consume any WCF service client need to know the endpoint of that particular service. Now, below is the most generalized description from the higher level which shows how communication happens.
Now, with WCF we consume services via channels. Below, is the labeled diagram explaining how clients consume WCF service.
Now, channels are basically built based on endpoints. Now, here, the client has to retrieve the endpoint definition from service metadata. Now, as you can see in the diagram we have a Service which is exposing multiple endpoints. I have shown in diagram two endpoints but it can be any number. Then, let’s suppose client wants to talk to this service via this endpoint. Then, in order to do so, client has to request metadata or WSDL definition from the service. Now, once metadata is available to the client, client will run one metadata import tool which will produce client side version of the endpoint. Now, once client has endpoints available; client can then go ahead and construct the channel based on one of the endpoints. And, once the channel has been constructed, client can send message through that channel to the service. So, basically WCF provides nice symmetrical architecture on both sides of the wire.
So, in this session we have seen closely about endpoints, services and then how to consume services theoretically. We will see all these practically in coming sessions. Till then stay tuned and Happy Coding.
So, in today’s discussion, will move next step by writing a simple WCF program. So, let’s get started. Below in the screenshot, you can see I am in Visual Studio instance 2013.
Then,i’ll click on new project and select WCF tab and from there i will select WCF Service Library.
Now, this will create one simple class library solution as shown below in the screen shot.
Now, let’s understand what visual studio has given us by default. so, basically it gave me basic implementation of WCF service to get started. But, i don’t want these stuffs; rather i am quite interested in getting started right from the scratch. so, i’ll go ahead and delete these two files. Now, let’s look at the references which visual studio gave me with this template.
Now, these two namespaces are the basic things which required for building any WCF Service. Now, it also gave me app.config file which is pretty much required for all types of WCF configurations. Now, i’m going to write new class to it
Now, i am going to modify this, i’ll add one more class here itself and then annotate the same with [DataContract] as shown below so that this class will get exposed for WCF. Now, for each field within the class also needs to be annotated with [DataMember] as shown below in the screen shot.
Now, i am going to create one interface which will have one operation which will basically return one string with values which we have in the class. Also, if you have noticed that this service contract has taken Employee as an object. Now, in order to expose this as a service contract, i also need to annotate the same with [ServiceContract] attribute and the other attribute which i need to apply on the method is the [OperationContract] as shown below in the screen shot.
Also, one point to note here that [ServiceContract] is available under the namespace “System.ServiceModel”.
Now, to implement this WCF service contract, I will come to my base class and simply derive it from here as shown below in the screen shot.
So, above is my WCF Implementation. Notice that for WCF Implementation, I don’t need any kind of annotation. Now, we are good to host our service either in IIS, or we can write our own custom hosting mechanism also known as self hosting or we can test the same with WCF Test client. For now, i’ll be using WCF Test client. But, in either case, we need to update our App.Config with correct endpoints so that TestClient will come to know which service you are interested in hosting.
Now, i have completely modified the App.Config with my requirements. So, basically i have set configured three endpoints in it with service contract which i have written during implementation. i have also configured the base address of my service. Now, below is the modified app.config file.
Now, with the above changes in place, we are ready to host the service on WCF Test Client. Now, all i need is to save this file and press F5. Now, when i press F5, it will launch the below shown window which is basically a confirmation that your service has started.
Now, as you see in the screen shot, there are three endpoints hosting the same contract but different ways. So, now if click on any of these methods of any endpoint it will present me the same result. For example, I click on PrintEmployee() of BasicHttpBinding and it will present me the below UI.
Now here i can go ahead and mention the values in those text box and then click on invoke button, then it will return me expected result in string format.
With this i would like to wrap this session here. In the next section, will do more deep dive and see other important things related with WCF. Till then stay tuned and Happy Coding.
In Today’s discussion, we’ll be discussing more about WCF and its features. So, before delving in WCF directly. Let’s look at the basics like how WCF came into picture. WCF is all about connected systems. Basically, a connected system is the one which is distributed across multiple machine nodes. you can see the glimpse of connected system in below diagram.
Now, if you see closely, you will notice that these machines communicate with each other via some medium. Currently, i just labeled it with communication. It means, different machines will pass information to and fro via some protocol. So, this is the connected system in a nutshell. Now, let’s see how can we build connected systems on Windows. Years before, we used to have “DCOM/COM+/ES“, a component oriented architecture. So, here we used to take our COM components and deploy them on the network and then invoke them remotely. One of the key features of this was Distributed Transactions, where basically logical transactions are handled by multiple COM components spanned throughout the network. Now, the disadvantage of this feature was, it did require to have DCOM Infrastructure installed on each and every machine.
Now, at the next level with the release of .NET, they also released a distributed object technology known as .Net Remoting. It was also very similar to COM based architecture but only difference is that it runs under the Managed Execution environment of CLR. As a result, .Net Remoting became very simple, extensible but still in this case as well it depends on .Net CLR.
And then MSMQ came into picture. It’s very different from other two frameworks. MSMQ is completely based on messaging. It allows you take a message and transmit it between two machines in a connected system in a asynchronous durable and reliable fashion. It has nothing to do with components. But, it still requires MSMQ infrastructure installed on each and every participating machine.
so, here in the above case, each infrastructure comes with it’s own programming style which means developers need to learn different styles of coding before they jump start execution. Also, there is one more disadvantage of this system as each of these restricts its execution to Windows environment only.
So, now in today’s era interoperability is the key demand across all the companies. For example, consider the below example, companies need to have environment where in they can seamlessly do transactions.
so, here in the above diagram, this connection is via services. A service is the simple unit of functionality which you can expose to any ecosystem via messaging. So, we can implement services on each of these nodes and then expose the functionality via messaging. Now, the way we achieve interoperability via standard protocols like HTTP and message formats like XML. So, this way we can easily achieve more loosely coupled architecture.
Now, Let’s discuss a design strategy a bit.
So, as you can see in the above diagram, the 1st one is the SOAP which also incorporates various WS-* specifications. Now, this style has been used for several years and still working nicely. This design is very popular across enterprise level where in projects are basically Intranet based. Another design strategy which is gaining traction these days is REST(Representational State Transfer). This design approach is very popular at internet space.
Now, let’s discuss the same in detail. Below is the basic diagram of SOAP based architecture.
As you can see in the diagram that SOAP and WS-* based specifications build on a full fledged stack of different technology suites. So, the 1st thing is Transport Neutrality which is the foundation of this approach. Now, in-order to accomplish the same we need to make use of XML based messaging layer. Now, this is the place where in SOAP comes into picture. SOAP is basically XML for bundling the messages which we need to transmit across our services. Then comes various WS-* based specifications like Security, Reliable Messaging(RM), Transactions etc. Now, if you are using any of these advanced features, we also need to provide Metadata to our clients
Now, on the other hand Restful service is completely different from SOAP based. In fact, they don’t try to achieve Transport Neutrality as they support only HTTP protocol. So, basically with REST services are modeled as resources with Unique identifiers also known as URIs. Now, in order to interact with these resources we need to have a uniform interface which is commonly known as “GET,POST,PUT,DELETE….”. Now, in order to represent these resources, we need to have concrete format like XML, JSON, RSS, ….
Now, let’s look at the below picture. It basically shows Microsoft Services in a nutshell.
So, as you can see that the 1st one is ASP.NET Webservices also known as ASMX. So, basically it provided support for SOAP 1.1 and 1.2. Just after this, they came with Web Service Enhancements also known as WSE. This is basically meant for supporting WS-* protocols. It means it actually added, Security, Secure Conversation etc. Now, one can use WSE standalone or in conjunction with ASMX to add some of these behaviors to ASMX services. But, again this approach was also quite limited in terms of Transport neutrality.
Later on, Microsoft realized, they need to work on one framework which will complement Uniform programming model for building any kind of service oriented architecture. Hence, they tried to incorporate below listed dependencies under the same hood.
So, in a nutshell above diagram shows that it has got every required things under one programming model and hence, It turned out to be the birth of WCF.
So, in today’s discussion, we have just discussed birth of WCF by focusing on the evolution of connected systems and services. In the next section, we’ll start with WCF programming from the scratch. Till then stay tuned and Happy Coding.
2,319 total views, no views today
Subscribe For Latest Updates!
Signup for My View newsletter and get notified when I publish new articles for free!