Which specifications does wcf follow
WCF is evolved from Web Service. What is mean by Service, Message and client? Service — Self-contained business functionality which are independent of other services. Message — Services communicate with each other with the help of messages.
Client — party which consume the service. What are different things we need to do in order to create a WCF service? Create a service contract an interface attached with ServiceContract attribute and methods with OperationContract attribute Create data contract if required any custom classes with DataContract Attribute and properties with DataMember attribute Implement the service and define operation contracts.
Define end points Host the service. Do watch the below WCF interview questions video created by www. Post a new comment Error Anonymous comments are disabled in this journal. Your reply will be screened Your IP address will be recorded.
The next thing to note is the Operation Contract attribute. Operation Contract dictates which methods should be exposed to the external client using this service.
It defines individual exchange or request and replies. In the current sample, we have defined GetTotalCost method, which will be used by the end client to get the total cost results. The next thing to note in the code snippet is the Data Contract attribute. In the previous two steps, we have exposed class as a service by using Service Contract and methods by using Operation Contract. Every operation will definitely do some kind of data transfer. Data Contract attributes defines which type of complex data will be exchanged between the client and the service.
They determine which parameters to be serialized. When you are using simple data types like int, bolo etc it is not necessary that you need to mark the data contract attribute. Because you will always find matching types on the client. However, complex structure like one shown in the below code snippet you will need to define a data contract. Remember data contract define how this data will be passed during transmission.
In short data contract attribute define how data will be serialized will transmission. In the below sample we have marked the structure product data to be serialized. Figure The Service class As data contract are all about serialization you need to import System. Serialization namespace. In the next step, we implement the GetTotalCost function.
It just returns a simple string with product name and the total cost of all products. Once our service class is done its time to host this service.
There are various ways of hosting a WCF service we will look in to the same in the next question. For the current example, we will host in their own process.
Figure 5: - Hosting the service Hosting the WCF service needs two things one is the config file and second is the hosting code on startup. Because we are hosting this service in its own application process this needs to be a windows application. So first let us have a look what entries do, we need to make in the App.
In the above figure, everything is clear but let us understands all the section defined in the App. As said in the previous answer End gives three important answers Where, What and How. In short where is the service, what the contract of the service is and how do we communicate with the service. In the above code snippet, we have only defined the contract i. The where is defined in the application entry point static void main. Therefore, the contract attribute defines the interface and binding says that the end clients can communicate using "HTTP" protocol.
In Static void Main method, we create an object of Service Host class and use the open method to host the service. We have used the URI object to define the address where the service will be hosted. Figure 6: - Service Started If you compile the project, you will see something as shown in the above figure. This says that the service is up and running and ready to serve any WCF client. Now its time to develop consumer, which will consume this WCF service. Microsoft has provided a decent automation to generate the client.
Therefore, below figure depicts the various steps. One you run the command against the URI it will generate two files one is the config file and the other is the proxy. You can see in the above figure two files are generated serviceGetCost.
With the help of these two files, we will make our client. In that, we have added output. We have renamed output. Once we have done with everything, its time to write the client code, which calls the proxy who in turn will call the service hosted. In the above figure, you can see we have the client code also. It is a simple code we first created the object of the data structure set the values. Then we create the object of the service and call the GetTotalCost function. If everything is compiled and you run the server and client, you should get your output as shown below.
There are three major ways to host a WCF service:- Self-hosting the service in his own application domain. This we have already covered in the first section. The service comes into existence when you create the object of Service Host class and the service closes when you call the Close of the Service Host class.
Host in application domain or process provided by IIS Server. So what we will do is host the same GetCost sample which was self hosted in the previous question. First thing you will need is to create the SVC file, which exposes the service class. SVC file contains the pointer to the class. You can see from the figure below the class attribute points to the class whose interface is exposed by the service.
Also, note the actual interface is in service. Below figure, have both the files service. We have taken the same sample, which was self-hosted in the previous question. Figure - The SVC file and the behind code We also need to provide implementation for the interface. So we have made a class ServiceGetCost which has the actual implementation. Below figure shows the same in detail. In the below figure you can also see the solution files.
Figure - Implementation of Service. Figure - Web. We need to create the virtual directory in IIS. One important thing to note while creating virtual directory set the access permission to execute. Figure IIS Configuration In the third step, we will publish the website to our virtual directory. Note the fourth step in which we have copied the svc file so that the service can be requested. Note: - ASP. So if you want to have full compiled DLL you need to publish the project to a virtual directory.
Once you have hosted the SVC file you can test the same by request the service. The proxy and config will be same, as we had done for self-hosting. The one important change is the address. The config file URL now points to the service. You can run the same client, which we had created for self-hosting.
The only change you will need to do is change the endpoint address. We have renamed output. Once we have done everything, it is time to write the client code which calls the proxy which in turn will call the service hosted. In the above figure, you can see we have the client code also.
It is a simple code: we first create the object of the data structure and set the values. Then we create the object of the service and call the GetTotalCost function. If everything is compiled and you run the server and client, you should get your output as shown below. So what we will do is host the same GetCost sample which was self hosted in the previous section. The first thing you will need is to create the SVC file which exposes the service class. The SVC file contains the pointer to the class.
You can see from the figure below that the class attribute points to the class whose interface is exposed by the service.
Also, note the actual interface is in the service. The below figure has both the files service. We have taken the same sample which was self-hosted in the previous question. We also need to provide an implementation for the interface. So we have made a class ServiceGetCost which has the actual implementation.
The below figure shows the same in detail. In the below figure you can also see the solution files. We also need to specify the service type and the endpoint in the web. Now that we are done with the coding part, we need to create the virtual directory in IIS. One important thing to note while creating a virtual directory is to set the access permission to execute.
In the third step, we will publish the website to our virtual directory. Note the fourth step in which we have copied the svc file so that the service can be requested. Note: ASP. So if you want to have a fully compiled DLL you need to publish the project to a virtual directory.
Once you have hosted the SVC file you can test the same by requesting the service. If everything works fine you will get something as shown in the below figure:. Using Svcutil. The proxy and config will be the same, as we had done for self-hosting. The one important change is the address. The config file URL now points to service.
You can run the same client which we had created for self-hosting. The only change you will need to do is change the endpoint address.
IIS provides automatic activation which means the service is not necessary to be running in advance. When any message is received by the service it then launches and fulfills the request. But in the case of self hosting the service should always be running. If IIS finds that a service is not healthy that means if it has memory leaks etc.
Let us try to understand what recycling is in the IIS process. For every browser instance, a worker process is spawned and the request is serviced. When the browser disconnects the worker, the process stops and you lose all information.
IIS also restarts the worker process. By default, the worker process is recycled in around minutes. So why does IIS recycle? By restarting the worker process it ensures any bad code or memory leak does not cause issues to the whole system. In the case of self-hosting both the above features you will need to code yourself.
Lots of work, right? That is why IIS is the best option for hosting services until you are really doing something custom. The below figure shows where the recycle option is located in IIS. You need to click on the DefaultAppool and then Properties. Web services can only be invoked by HTTP.
A Service or a WCF component can be invoked by any protocol and any transport type. Second, web services are not flexible. However, Services are flexible.
If you make a new version of the service you just need to expose a new end. Therefore, services are agile and which is a very practical approach looking at the current business trends. WCF includes predefined bindings. They cover most of the bindings widely needed in day-to-day applications. However, just in case you find that you need to define something custom WCF does not stop you.
So let us try to understand what each binding provides:. Note : An interprocess control IPC protocol is used for exchanging information between two applications, possibly running on different computers in a network. The difference between Named pipes and TCP is that named pipes have good performance in terms of communication within processes. But when it comes to communicating across a network TCP holds the best choice.
IsOneWay equal to true ensures that the client does not have to wait for the response. So methods marked by IsOneWay to true should always return void. In this, the caller does not get anything in return so it is called as a one-way communication. In order to understand one-way implementation in WCF let's make a code walkthrough of a sample.
0コメント