- First thing to consume a service is to create proxy.
- Proxy is a class which client should prepare in client language based on service information.
- W3 standards for describing service information in WSDL[Webservice Description Language]
We can create two
types of Proxy’s:
1. On-fly Proxy.
2. Strong (or) design time proxy.
Normally code in client is written based on proxy.
- On-fly Proxy: On-fly proxy means Proxy created @ runtime only. It is not user-friendly but provides dynamic processing advantages.
- Design time proxy: Proxy is created first and code is written based on proxy. These are more friendly & more preferred also for regular service consumption.
As a wcf developer and consumer we have to handle these two
tasks.i.e, Creating bindings according to requirement and creating proxy based on app properly.One proxy is created for single endpoint.
- Previous we Developed one Service Called WCFDEMO. Now we consume that WCFDEMO service at client Side.
- Ensure that WCFDEMO service is Running.
Consuming WCF Service example:
- ChannelFactory<t> class is used to create On-Fly proxy.It is present in same System.ServiceModel namespace.
- Start another Microsoft VisualStudio instance sothat we can write client program here and comminicate with running wcf service.
- Copy (or) Get the interface created in service to our client project [WCFDEMO] sothat we can use ChannelFactory class for creating proxy.
- Write code in Main,which is starting point.Code to use ChannelFactory and create On-Fly proxy class then start requesting .
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;//add reference
namespace YMsgr
{
[ServiceContract]
interface IDemo
{
[OperationContract]
string
Msg();
string
Add();
}
public class Program
{
static void Main(string[] args)
{
ChannelFactory<IDemo> obj = new
ChannelFactory<IDemo>(new BasicHttpBinding(),new EndpointAddress("http://localhost:5990/sudhakarservice")));
IDemo
Proxyobj = obj.CreateChannel();//CreatesProxy
string
msg = Proxyobj.Msg();
Console.WriteLine("Result:"+msg);
Console.ReadKey();
}
}
No comments:
Post a Comment