This is a mirror of official site: http://jasper-net.blogspot.com/

Calling a WCF service from a client without having the contract interface

| Tuesday, February 14, 2012
I was asked yesterday in the Hebrew C#/.NET Framework MSDN forums a tough question – is it possible to dynamically call a WCF service using only the contract name, operation name, and metadata address?

At first, I agreed with the answer given in the forum – move from SOAP bindings to WebHttpBinding (“REST”). This of course makes things a lot easier, only requiring you to create a WebHttpRequest and parse the response. However the question remains - is it possible to do this in the case of a SOAP-based service endpoint?

The short answer is – YES!

The full answer is – YES, but you’ll need to do a lot of coding to make it work properly, and even more coding for complex scenarios (who said passing a data contract?)

How is it done you ask?

First let’s start with the contract – you have a simple contract that looks like so:


[ServiceContract]
public interface ICalculator
{
   [OperationContract]
   double Add(double n1, double n2);
  
   [OperationContract]
   double Subtract(double n1, double n2);

   [OperationContract]
   double Multiply(double n1, double n2);

   [OperationContract]
   double Divide(double n1, double n2);
}


At this point, the implementation doesn’t matter, but you can assume the service compiles and loads successfully.

Second, make sure your service has either a MEX endpoint or metadata exposed over HTTP GET. Read here for more info about the difference between the two.

Third – do the client coding!!! To create the client code, I took some ideas from the following links:

    http://msdn.microsoft.com/en-us/library/ms733780.aspx – Generating client-side type information for WCF contracts
    http://www.codeproject.com/Articles/42278/Call-a-Web-Service-Without-Adding-a-Web-Reference – The same concept of dynamic calls, but for ASP.NET web services (ASMX)


 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.ServiceModel;
 using System.ServiceModel.Description;
 using System.Globalization;
 using System.Collections.ObjectModel;
 using System.CodeDom.Compiler;
 
 namespace Client
 {
     class Program
     {
         static void Main(string[] args)
         {
             // Define the metadata address, contract name, operation name,
             // and parameters.
             // You can choose between MEX endpoint and HTTP GET by
             // changing the address and enum value.
             Uri mexAddress = new Uri("http://localhost:8732/CalculatorService/?wsdl");
             // For MEX endpoints use a MEX address and a
             // mexMode of .MetadataExchange
             MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGet;
             string contractName = "ICalculator";
             string operationName = "Add";
             object[] operationParameters = new object[] { 1, 2 };
 
             // Get the metadata file from the service.
             MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress, mexMode);
             mexClient.ResolveMetadataReferences = true;
             MetadataSet metaSet = mexClient.GetMetadata();
 
             // Import all contracts and endpoints
             WsdlImporter importer = new WsdlImporter(metaSet);
             Collection<ContractDescription> contracts =  importer.ImportAllContracts();
             ServiceEndpointCollection allEndpoints = importer.ImportAllEndpoints();
 
             // Generate type information for each contract
             ServiceContractGenerator generator = new ServiceContractGenerator();
             var endpointsForContracts = new Dictionary<string, IEnumerable<ServiceEndpoint>>();
 
             foreach (ContractDescription contract in contracts)
             {
                 generator.GenerateServiceContractType(contract);
                 // Keep a list of each contract's endpoints
                 endpointsForContracts[contract.Name] = allEndpoints.Where(
                     se => se.Contract.Name == contract.Name).ToList();
             }
 

Read more: Codeproject
QR: Calling-a-WCF-service-from-a-client-without-having

Posted via email from Jasper-net

4 comments:

Anonymous said...

Tyya's dad won't reconcile oneself to anything befitting at the depend on - no ice cream, no sweets, no cookies. But when the saleslady puts a valuation sticker on Tyya's nose, Daddy is decisively stiff to pay off something allowable

http://www.webhunter.pl/?p=1


http://cara.pl/nieruchomosci/wynajem-czy-zakup
http://cafewenecja.pl/smietanka-i-mleko
http://mleko.zgq.pl/2010/05
http://internetmister.pl/tagi/kapusta
http://ten-dzien.pl/page/7

Anonymous said...

As opposed to of texting my daughter pertaining to laundry, I accidentally texted a confrere from trade and it autocorrected to whores.
http://www.okyl.edu.pl/firmy/mdj,electronic,panele,operatorskie,s,4481/
http://www.osstal.com.pl/strony,blogi,fora/mdj,electronic,panele,operatorskie,s,802/
http://www.linkowany.net.pl/firmy/mdj,electronic,sterowniki,plc,s,7689/
http://loer.org.pl/?p=7173
http://www.abbora.edu.pl/firmy/mdj,electronic,sterowniki,plc,s,4372/

Anonymous said...

I'm mad and that's a fact I found out animals don't help Animals think they're pretty smart Shit on the ground, see in the dark

http://www.centrum-firm.com/katalog/pokaz/4128
http://www.biznesdlafirm.com/katalog/pokaz/2447
http://biznesdlafirm.com/katalog/pokaz/3139
http://www.promowanefirmy.pl/category/motoryzacja-i-transport-przewoz-osob/cat/216
http://centrum-firm.com/katalog/pokaz/2555

Anonymous said...

Two paramount manufacturers of favoured premium products are pulling sundry ordinary brands from the hawk because they may restrict traces of poultry antibiotics that are not approved in the U.S.

http://gdynia.opendi.pl/W/676.html
http://promowanefirmy.pl/katalog/pokaz/5208
http://promowanefirmy.pl/katalog/szukaj/cat/225/strona/15
http://www.promowanefirmy.pl/katalog/pokaz/3852
http://www.promowanefirmy.pl/katalog/pokaz/5873

http://www.centrum-firm.com/katalog/pokaz/1893
http://www.centrum-firm.com/category/dom-i-ogrod-pielegnacja-zieleni-/cat/169
http://promowanefirmy.pl/katalog/pokaz/5389
http://www.biznesdlafirm.com/katalog/pokaz/4580
http://centrum-firm.com/katalog/pokaz/779