-
WCF syndication example
What is it ? You can ask your WCF service to represent data in ATOM or RSS format .
Example Defines feed contract Firstly we need to define syndication contract .
ServiceKnownType: indicate that what is the return type of our desired feed will be look like . In this example we expect to get ATOM or RSS.
OperationContract: to make it accessible to the world ! We have to add this attribute to the method
-
consuming the RESTFul WCF service in client side example
The client can simply use the WCF RESTFul service and invokes its methods remotely via WebChannelFactory.
Add reference Firstly, we need to add a reference to the server side project so will have the same contacts in the client side . Also you need to add the System. ServiceModel.web name space to the client side to have the WebChannelFactory functionality .
Making a WebChannelFactory It is getting the server side interface contract and the base address of hosting as input .
-
WCF RESTFul service example
Rest stand for REpresentational State Transfer that builds on HTTP methods on different data format.
Visual Studio config our binding according to SOAP architecture . We have to configure to allow WCF work with RESTful design.
Server side In this example we have three main methods, i.e. AddMachine to submit a machine , GetMachineNameById to get the machine name with GET method and DeletemachineById to delete a machine.
[ServiceContract] public interface IMachine { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "machine")] string AddMachine(MachineDetail machineDetail); [WebGet(UriTemplate = "machine/{machineId}")] [OperationContract] string GetMachineNameById(string machineId); [WebGet(UriTemplate = "machines")] [OperationContract] string GetMachineNames(); [WebInvoke(UriTemplate = "machine/{machineId}",Method = "DELETE")] [OperationContract] string DeleteMachineById(string machineId); } As we want they will be accessible in client side we have to add [ OperationContract ] to them.
-
SOAP VS REST in WCF
What is SOAP? It’s a model to define high level applications that support various protocols such as “HTTP”,”TCP” and “MSMQ” with only POST method.
Benefits : It has high interoperability and useful for enterprise system as we have more control over the system .
High security maneuver by using WS-Security models.
There are lots of tools and support to generate the client side code .
What is REST? REST refers to Representational State Transfer is an architecture that our current web is basically working.
-
Call WCF service methods asynchronously example
It is possible to call wcf service methods asynchronously so it can improve the performance .
How to do it ? Go to the service configuration that you have already imported and then enable the asynchronous option.
Service reference configure service reference allow generation of asynchronous operations Then it will be regenerate your client side methods and you will see new Async type method.
Async type method do not have return type and basically you trigger a method when their job is completed
-
WCF Mex programming example
It is possible to walk to the server and get the contract endpoints of it at run time. It will help us to have more robust classes which let client dynamically choose the desire endpoints.
In this case firstly you should add the MEX endpoint to your server side application.
<endpoint address="http://localhost:8080/ServiceMachine/mex" binding="mexHttpBinding" bindingConfiguration="" name="mex" contract="IMetadataExchange" /> The only difference is this mex endpoint use IMetadataExchange contract .
-
WCF service client side example
What is client side ? Basically, the client gets the service endpoints and contracts through the WSDL file and then make a channel to send the message to the service.
How to consume a service ? You can use the svcUtil.exe tool to make the required .cs and ,config files and then adding them to the project or just add the web reference and add your service meta address your client project .
-
Hosting WCF service example
WCF service can be hosted in two ways.
1. Self-hosting You can use the ServiceHost Class to host any .net 3.x application inside your project. In this case you can make a console application and add the reference of the WCF service and also System.ServiceModel to your project.
var serviceHost = new ServiceHost(typeof(Machine)); In the next step you can add endpoints to your service host instance
serviceHost.AddServiceEndpoint(typeof(IMachine), new BasicHttpBinding(), "http://localhost:8733/ServiceMachine/basic"); serviceHost.
-
WCF service server side example
What is WCF ? Windows Communication Foundation is service application that help us to send messages between connected systems by various endpoints.
WCF support both SOAP and Restful design architectures and could transport various types of message format on different protocols while previous Microsoft services such as ASMX and WSE did not. For instance we can send XML messages on HTTP protocol when our concerns about interoperability or binary message through TCP when we are looking to increase the performance.
-
CSharp example of mocking and stubbing with RhinoMock
What is RhinoMock? RhinoMock is a mocking framework that helps you to inject dependencies without implementing them in your test side.
When you are mocking an interface, you are making an object in a proxy domain that can at as a real class for you.more info
How to use it ? Imagine that you have below code ,and you want to assert that if the Add method is called or not .