Thursday, 25 October 2012

How to generate a Proxy at runtime?


How to generate a Proxy at runtime?
yes, Proxy generation is done through adding service reference at client which generates proxy at client as xml files(which are platform-operatingsystem/technology-java,php,.net);
proxy generation is not new in wcf. We already know asp.net web service comsuming through
AddWebReference  : http://localhost:72/Service---asp.net webservice/wcf also
or
Once service is running then client can consuming in 2 ways
1.  Proxy generation at client

webreference/servicereference

(which are platform-operatingsystem/technology-java,php,.net)
Prepare Service single service with 3 endpoints:

Contract /service define
[ServiceContract(Name="ourservice")]
    //[ServiceContract(Name="OUrService",ProtectionLevel=ProtectionLevel.EncryptAndSign)]
    interface IMathService
    {
        [OperationContract]
        int GetSum(int a, int b);
       
    }
Service implementation:
   class MathService: IMathService
    {       
        public int GetSum(int a, int b)
        {
            return a + b;
        }
    }
Host the service:
static void Main(string[] args)
        {                      
           using (ServiceHost sh = new ServiceHost(typeof(MathService), new Uri("http://localhost:72/Service")))
           {
                //Adding the endpoints to the ServiceHost
sh.AddServiceEndpoint(typeof(IMathService), new NetTcpBinding(), "net.tcp://localhost:71/Service");                            
sh.AddServiceEndpoint(typeof(IMathService), new WSHttpBinding(),"/wsaddress");
sh.AddServiceEndpoint(typeof(IMathService), new BasicHttpBinding(), "/basicaddress");
//Creating a new Service Behavior
           
ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
metadataBehavior.HttpGetEnabled = true;
sh.Description.Behaviors.Add(metadataBehavior);
                //Opening the ServiceHost for incoming connections
sh.Open();
                
Console.WriteLine("I am listening ........\n");
Console.WriteLine("Waiting for clinet requests..");
Console.ReadLine();

                //Close opened ServiceHost object
               
            }
        }
Note: serviceHost is a type in System.ServiceModel namespace.which can host ourservice.
In Every Hosting type :

Which can be Self-Hosting.
Which can be IIS-Hosting.

---- ServiceHost---à IS Maindatory type.
Yes service is ready.

No consume the service at client

1.       Design time Proxy:

àAdd service reference.// automatically generated by svcutil.exe tool




And generated configuration of 3 endpoints implements at service.
and
app.config contrains endpoints details
like
binding, endpoint details.

but keep in mind : which also generates a channel/ from  proxy at rumtime.
for their apporiate details.

and
we can browse service is hosted as self-host address of basic/ ws -http binding.



once---

at client :



proxy.GetSume(14,51)--->go to definication-->
step throw Proxy: Reference


another way to generate proxy at desige time is using 

svcutil.exe  tool
go to visual studio 2008/ 2010 commandpromt the 
write command as 
svcutil.exe : address of running service


path
C:\Program Files\Microsoft Visual Studio 9.0\VC\


2.       Runtime Proxy:

 yes, client doen't know about proxy even also can consume the service through as per client has to specify the binding
and contract and address details 

which prepares a proxy---> runtime. but the 

SERVICE HAS these Details exactly.


yes proxy is generated at code like
below:

yes above code generates a proxy at runtime.


No comments:

Post a Comment