<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.microsoft.co.il/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Ido Flatow&amp;#39;s Blog&lt;h3&gt;Veni Vidi Scripsi&lt;/h3&gt; : mex</title><link>http://blogs.microsoft.co.il/blogs/idof/archive/tags/mex/default.aspx</link><description>Tags: mex</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Calling a WCF service from a client without having the contract interface</title><link>http://blogs.microsoft.co.il/blogs/idof/archive/2012/02/10/calling-a-wcf-service-from-a-client-without-having-the-contract-interface.aspx</link><pubDate>Fri, 10 Feb 2012 05:47:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1010779</guid><dc:creator>Ido Flatow</dc:creator><slash:comments>33</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/idof/rsscomments.aspx?PostID=1010779</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/idof/archive/2012/02/10/calling-a-wcf-service-from-a-client-without-having-the-contract-interface.aspx#comments</comments><description>&lt;p&gt;I was asked yesterday in the Hebrew C#/.NET Framework MSDN forums a &lt;a href="http://social.msdn.microsoft.com/Forums/he-IL/nethe/thread/1678e16f-877c-44c9-9ccf-9b9c39bc51b4"&gt;tough question&lt;/a&gt; – is it possible to dynamically call a WCF service using only the contract name, operation name, and metadata address?&lt;/p&gt;
&lt;p&gt;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? &lt;/p&gt;
&lt;p&gt;The short answer is – YES! &lt;/p&gt;
&lt;p&gt;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?)&lt;/p&gt;
&lt;p&gt;How is it done you ask?&lt;/p&gt;
&lt;p&gt;First let’s start with the contract – you have a simple contract that looks like so:&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[ServiceContract]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ICalculator&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;  [OperationContract]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;  &lt;span class="kwrd"&gt;double&lt;/span&gt; Add(&lt;span class="kwrd"&gt;double&lt;/span&gt; n1, &lt;span class="kwrd"&gt;double&lt;/span&gt; n2);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;  [OperationContract]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;  &lt;span class="kwrd"&gt;double&lt;/span&gt; Subtract(&lt;span class="kwrd"&gt;double&lt;/span&gt; n1, &lt;span class="kwrd"&gt;double&lt;/span&gt; n2);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;  [OperationContract]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;  &lt;span class="kwrd"&gt;double&lt;/span&gt; Multiply(&lt;span class="kwrd"&gt;double&lt;/span&gt; n1, &lt;span class="kwrd"&gt;double&lt;/span&gt; n2);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;  [OperationContract]&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;  &lt;span class="kwrd"&gt;double&lt;/span&gt; Divide(&lt;span class="kwrd"&gt;double&lt;/span&gt; n1, &lt;span class="kwrd"&gt;double&lt;/span&gt; n2);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;At this point the implementation doesn’t matter, but you can assume the service compiles and loads successfully.&lt;/p&gt;
&lt;p&gt;Second, make sure your service has either a MEX endpoint or metadata exposed over HTTP GET. Read &lt;a href="http://blogs.microsoft.co.il/blogs/idof/archive/2011/08/10/wsdl-vs-mex-knockout-or-tie.aspx"&gt;here&lt;/a&gt; for more info about the difference between the two.&lt;/p&gt;
&lt;p&gt;Third – do the client coding!!! to create the client code I took some ideas from the following links:&lt;/p&gt;
&lt;p&gt;&lt;a title="http://msdn.microsoft.com/en-us/library/ms733780.aspx" href="http://msdn.microsoft.com/en-us/library/ms733780.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms733780.aspx&lt;/a&gt; – generating client-side type information for WCF contracts &lt;/p&gt;
&lt;p&gt;&lt;a title="http://www.codeproject.com/Articles/42278/Call-a-Web-Service-Without-Adding-a-Web-Reference" href="http://www.codeproject.com/Articles/42278/Call-a-Web-Service-Without-Adding-a-Web-Reference"&gt;http://www.codeproject.com/Articles/42278/Call-a-Web-Service-Without-Adding-a-Web-Reference&lt;/a&gt; – the same concept of dynamic calls, but for ASP.NET web services (ASMX).&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.ServiceModel;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.ServiceModel.Description;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Globalization;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.ObjectModel;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.CodeDom.Compiler;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; Client&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            &lt;span class="rem"&gt;// Define the metadata address, contract name, operation name, and parameters. &lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            &lt;span class="rem"&gt;// You can choose between MEX endpoint and HTTP GET by changing the address and enum value.&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;            Uri mexAddress = &lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(&lt;span class="str"&gt;&amp;quot;http://localhost:8732/CalculatorService/?wsdl&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;            &lt;span class="rem"&gt;// For MEX endpoints use a MEX address and a mexMode of .MetadataExchange&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;            MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGet;            &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; contractName = &lt;span class="str"&gt;&amp;quot;ICalculator&amp;quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; operationName = &lt;span class="str"&gt;&amp;quot;Add&amp;quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;            &lt;span class="kwrd"&gt;object&lt;/span&gt;[] operationParameters = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;[] { 1, 2 };&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;            &lt;span class="rem"&gt;// Get the metadata file from the service.&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;            MetadataExchangeClient mexClient = &lt;span class="kwrd"&gt;new&lt;/span&gt; MetadataExchangeClient(mexAddress, mexMode);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;            mexClient.ResolveMetadataReferences = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;            MetadataSet metaSet = mexClient.GetMetadata();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;            &lt;span class="rem"&gt;// Import all contracts and endpoints&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;            WsdlImporter importer = &lt;span class="kwrd"&gt;new&lt;/span&gt; WsdlImporter(metaSet);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;            Collection&amp;lt;ContractDescription&amp;gt; contracts = importer.ImportAllContracts();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;            ServiceEndpointCollection allEndpoints = importer.ImportAllEndpoints();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;            &lt;span class="rem"&gt;// Generate type information for each contract&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;            ServiceContractGenerator generator = &lt;span class="kwrd"&gt;new&lt;/span&gt; ServiceContractGenerator();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;            var endpointsForContracts = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, IEnumerable&amp;lt;ServiceEndpoint&amp;gt;&amp;gt;();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (ContractDescription contract &lt;span class="kwrd"&gt;in&lt;/span&gt; contracts)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;            {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;                generator.GenerateServiceContractType(contract);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;                &lt;span class="rem"&gt;// Keep a list of each contract&amp;#39;s endpoints&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;                endpointsForContracts[contract.Name] = allEndpoints.Where(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt;                    se =&amp;gt; se.Contract.Name == contract.Name).ToList();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  47:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (generator.Errors.Count != 0)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  48:  &lt;/span&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Exception(&lt;span class="str"&gt;&amp;quot;There were errors during code compilation.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  49:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  50:  &lt;/span&gt;            &lt;span class="rem"&gt;// Generate a code file for the contracts &lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  51:  &lt;/span&gt;            CodeGeneratorOptions options = &lt;span class="kwrd"&gt;new&lt;/span&gt; CodeGeneratorOptions();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  52:  &lt;/span&gt;            options.BracingStyle = &lt;span class="str"&gt;&amp;quot;C&amp;quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  53:  &lt;/span&gt;            CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider(&lt;span class="str"&gt;&amp;quot;C#&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  54:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  55:  &lt;/span&gt;            &lt;span class="rem"&gt;// Compile the code file to an in-memory assembly&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  56:  &lt;/span&gt;            &lt;span class="rem"&gt;// Don&amp;#39;t forget to add all WCF-related assemblies as references&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  57:  &lt;/span&gt;            CompilerParameters compilerParameters = &lt;span class="kwrd"&gt;new&lt;/span&gt; CompilerParameters(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  58:  &lt;/span&gt;                &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] { &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  59:  &lt;/span&gt;                    &lt;span class="str"&gt;&amp;quot;System.dll&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;System.ServiceModel.dll&amp;quot;&lt;/span&gt;, &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  60:  &lt;/span&gt;                    &lt;span class="str"&gt;&amp;quot;System.Runtime.Serialization.dll&amp;quot;&lt;/span&gt; });&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  61:  &lt;/span&gt;            compilerParameters.GenerateInMemory = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  62:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  63:  &lt;/span&gt;            CompilerResults results = codeDomProvider.CompileAssemblyFromDom(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  64:  &lt;/span&gt;                compilerParameters, generator.TargetCompileUnit);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  65:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  66:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (results.Errors.Count &amp;gt; 0)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  67:  &lt;/span&gt;            {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  68:  &lt;/span&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Exception(&lt;span class="str"&gt;&amp;quot;There were errors during generated code compilation&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  69:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  70:  &lt;/span&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  71:  &lt;/span&gt;            {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  72:  &lt;/span&gt;                &lt;span class="rem"&gt;// Find the proxy type that was generated for the specified contract&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  73:  &lt;/span&gt;                &lt;span class="rem"&gt;// (identified by a class that implements the contract and ICommunicationbject)&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  74:  &lt;/span&gt;                Type clientProxyType = results.CompiledAssembly.GetTypes().First(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  75:  &lt;/span&gt;                    t =&amp;gt; t.IsClass &amp;amp;&amp;amp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  76:  &lt;/span&gt;                        t.GetInterface(contractName) != &lt;span class="kwrd"&gt;null&lt;/span&gt; &amp;amp;&amp;amp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  77:  &lt;/span&gt;                        t.GetInterface(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(ICommunicationObject).Name) != &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  78:  &lt;/span&gt;                        &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  79:  &lt;/span&gt;                &lt;span class="rem"&gt;// Get the first service endpoint for the contract&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  80:  &lt;/span&gt;                ServiceEndpoint se = endpointsForContracts[contractName].First();                    &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  81:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  82:  &lt;/span&gt;                &lt;span class="rem"&gt;// Create an instance of the proxy&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  83:  &lt;/span&gt;                &lt;span class="rem"&gt;// Pass the endpoint&amp;#39;s binding and address as parameters&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  84:  &lt;/span&gt;                &lt;span class="rem"&gt;// to the ctor&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  85:  &lt;/span&gt;                &lt;span class="kwrd"&gt;object&lt;/span&gt; instance = results.CompiledAssembly.CreateInstance(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  86:  &lt;/span&gt;                    clientProxyType.Name, &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  87:  &lt;/span&gt;                    &lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  88:  &lt;/span&gt;                    System.Reflection.BindingFlags.CreateInstance, &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  89:  &lt;/span&gt;                    &lt;span class="kwrd"&gt;null&lt;/span&gt;,&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  90:  &lt;/span&gt;                    &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;[] { se.Binding, se.Address }, &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  91:  &lt;/span&gt;                    CultureInfo.CurrentCulture, &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  92:  &lt;/span&gt;                &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  93:  &lt;/span&gt;                &lt;span class="rem"&gt;// Get the operation&amp;#39;s method, invoke it, and get the return value&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  94:  &lt;/span&gt;                &lt;span class="kwrd"&gt;object&lt;/span&gt; retVal = instance.GetType().GetMethod(operationName).&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  95:  &lt;/span&gt;                    Invoke(instance, operationParameters);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  96:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  97:  &lt;/span&gt;                Console.WriteLine(retVal.ToString());&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  98:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  99:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 100:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 101:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;I’ve placed comments that describe the code, but basically it imports the WSDL, generates types for the contract (service + data), generates C# code from it, compiles it, and uses reflection to create a proxy and invoke the correct method.&lt;/p&gt;
&lt;p&gt;If you want to use this technique to call methods that require a data contract, you will need some extra work to create the correct type and initialize it.&lt;/p&gt;
&lt;p&gt;A compiled and running version of this code (+ the service) can be found here: &lt;a title="https://skydrive.live.com/redir.aspx?cid=5ef5be1ab30a6056&amp;amp;resid=5EF5BE1AB30A6056!466&amp;amp;parid=5EF5BE1AB30A6056!129" href="https://skydrive.live.com/redir.aspx?cid=5ef5be1ab30a6056&amp;amp;resid=5EF5BE1AB30A6056!466&amp;amp;parid=5EF5BE1AB30A6056!129"&gt;https://skydrive.live.com/redir.aspx?cid=5ef5be1ab30a6056&amp;amp;resid=5EF5BE1AB30A6056!466&amp;amp;parid=5EF5BE1AB30A6056!129&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Hope you find this piece of code useful.&lt;/p&gt;
&lt;p&gt;Feb-12:&lt;/p&gt;
&lt;p&gt;Just found out this great blog post that uses the same implementation only to enable calling WCF services from PowerShell.&amp;nbsp; &lt;br /&gt;&lt;a title="http://www.justaprogrammer.net/2012/02/11/using-powershell-to-call-a-wcf-service/" href="http://www.justaprogrammer.net/2012/02/11/using-powershell-to-call-a-wcf-service/"&gt;http://www.justaprogrammer.net/2012/02/11/using-powershell-to-call-a-wcf-service/&lt;/a&gt;&lt;/p&gt;
&lt;div style="PADDING-BOTTOM:0px;MARGIN:0px;PADDING-LEFT:0px;PADDING-RIGHT:0px;PADDING-TOP:0px;" class="wlWriterHeaderFooter"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://blogs.microsoft.co.il/blogs/idof/archive/2012/02/10/calling-a-wcf-service-from-a-client-without-having-the-contract-interface.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://blogs.microsoft.co.il/blogs/idof/archive/2012/02/10/calling-a-wcf-service-from-a-client-without-having-the-contract-interface.aspx&amp;amp;bgcolor=6600FF" /&gt;&lt;/a&gt; &lt;a href="http://dotnetshoutout.com/Submit?url=http://blogs.microsoft.co.il/blogs/idof/archive/2012/02/10/calling-a-wcf-service-from-a-client-without-having-the-contract-interface.aspx"&gt;&lt;img style="BORDER-BOTTOM:0px;BORDER-LEFT:0px;BORDER-TOP:0px;BORDER-RIGHT:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http://blogs.microsoft.co.il/blogs/idof/archive/2012/02/10/calling-a-wcf-service-from-a-client-without-having-the-contract-interface.aspx" /&gt;&lt;/a&gt; 
&lt;div class="addthis_toolbox addthis_default_style "&gt;&lt;a class="addthis_button_preferred_1"&gt;&lt;/a&gt;&lt;a class="addthis_button_preferred_2"&gt;&lt;/a&gt;&lt;a class="addthis_button_preferred_3"&gt;&lt;/a&gt;&lt;a class="addthis_button_preferred_4"&gt;&lt;/a&gt;&lt;a class="addthis_button_compact"&gt;&lt;/a&gt;&lt;a class="addthis_counter addthis_bubble_style"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4f27f7864794397c"&gt;&lt;/script&gt;
&lt;a style="DISPLAY:none;" href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=2199681" rel="tag"&gt;CodeProject&lt;/a&gt; &lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1010779" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/CodeDom/default.aspx">CodeDom</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/mex/default.aspx">mex</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/WsdlImporter/default.aspx">WsdlImporter</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/WCF+Client/default.aspx">WCF Client</category></item><item><title>WSDL vs MEX, knockout or tie?</title><link>http://blogs.microsoft.co.il/blogs/idof/archive/2011/08/10/wsdl-vs-mex-knockout-or-tie.aspx</link><pubDate>Wed, 10 Aug 2011 15:12:47 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:882407</guid><dc:creator>Ido Flatow</dc:creator><slash:comments>17</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/idof/rsscomments.aspx?PostID=882407</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/idof/archive/2011/08/10/wsdl-vs-mex-knockout-or-tie.aspx#comments</comments><description>&lt;p&gt;When teaching WCF I am always asked about the difference between getting the service’s metadata by using the WSDL’s http get url, and getting the metadata by calling the MEX endpoint.&lt;/p&gt;  &lt;p&gt;To answer that question we first need to understand the different parts of the configuration that affect metadata creation.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The ServiceMetadata behavior&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;This behavior controls whether metadata is created for the service. When this behavior is used, the service is scanned, and metadata is created for the service’s contracts (a list of operations and types exposed by the service). &lt;/p&gt;  &lt;p&gt;If the behavior is not used, no metadata will be created for the service, and you will not be able to create MEX endpoints. &lt;/p&gt;  &lt;p&gt;&lt;u&gt;The ServiceMetadata’s httpGetEnabled flag&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;This flag defines whether the metadata will be accessible by an http get request. If this attribute is set to true, then a default url will be created for the metadata (usually the service’s address with the suffix of&amp;#160; ‘?wsdl’). The url will lead you to a WSDL file containing the description of the service operations, but without the description of the data contracts – these files are accessible by different urls, usually the service’s url with the suffix of ‘?xsd=xsdN’. The list of these urls are pointed out from the WSDL file.&lt;/p&gt;  &lt;p&gt;If you do not set this attribute to true, you will not be able to access the metadata using http get requests. If you prefer using https for the get requests, you can use the &lt;strong&gt;httpsGetEnabled&lt;/strong&gt; attribute instead of the httpGetEnabled.&lt;/p&gt;  &lt;p&gt;There are several other settings for the get options – you can read more about them on &lt;a href="http://msdn.microsoft.com/en-us/library/ms731317.aspx"&gt;MSDN&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;The MEX endpoint&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;MEX endpoints are special endpoints that allow clients to receive the service’s metadata by using SOAP messages instead of http get requests. You can create MEX endpoint that can be accessed through http, https, tcp, and even named pipes.&lt;/p&gt;  &lt;p&gt;The response that you will receive when calling a MEX endpoint’s GetMetadata operation will include the content of the WSDL and all the XSD files that are linked to it.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;So what exactly is the difference between MEX and WSDL? &lt;/u&gt;&lt;/p&gt;  &lt;p&gt;There is no difference! &lt;/p&gt;  &lt;p&gt;MEX and WSDL both output the same thing – a web service description language (WSDL) document, only MEX does it by getting a SOAP message over some transport (http, tcp, named pipes) and returning one message with all the parts, while the WSDL urls use http get requests and require sending several requests to get all the parts.&lt;/p&gt;  &lt;p&gt;Don’t believe me? &lt;/p&gt;  &lt;p&gt;The following diff diagram was produced by comparing the output of a MEX call to the output of the aggregated results gathered by calling all the WSDL related urls using http get:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/idof/image_66D682B0.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://blogs.microsoft.co.il/blogs/idof/image_thumb_3E772A8F.png" width="437" height="484" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As you can see, there are only several sections that are different between the files (marked in red and yellow), while most of the content is identical. Let’s look at one of these parts:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/idof/image_21AD8FAD.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://blogs.microsoft.co.il/blogs/idof/image_thumb_7F095131.png" width="716" height="159" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The red lines come from the MEX result, and the yellow lines from the WSDL file. The difference is because when using WSDL files, the rest of the XSD files are linked by using the &amp;lt;xsd:import&amp;gt; tag with the schemaLocation attribute. Since the MEX response includes all the XSD content in it, the import tag doesn’t include the location attribute. &lt;/p&gt;  &lt;p&gt;As for the other different sections – they are different because the MEX response wraps each WSDL/XSD part with an xml element that does not appear in the aggregated files. These elements are part of the MEX standard declared by the &lt;a href="http://schemas.xmlsoap.org/ws/2004/09/mex/"&gt;W3C&lt;/a&gt; - surprise surprise, MEX is a W3C standard, it’s not a Microsoft proprietary spec.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;So they are the same, still, which one to use?&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;In most cases there is no need for MEX endpoint – using WSDLs with http get is usually enough.&lt;/p&gt;  &lt;p&gt;The “Add Service Reference” option in Visual Studio 2010 will work for both options. The same goes for the svcutil command line tool.&lt;/p&gt;  &lt;p&gt;So when would I use MEX?&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;If you want to make as less calls as possible to your service in order to get its metadata (one call instead of several).&lt;/li&gt;    &lt;li&gt;If you don’t want to use HTTP to get the metadata, but prefer using TCP or named pipes (not so common).&lt;/li&gt;    &lt;li&gt;If you want people to ask you why you declared a MEX endpoint.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;So is it a knockout or a tie? I say tie.&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:0px 0px 0px 0px;"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://blogs.microsoft.co.il/blogs/idof/archive/2011/08/10/wsdl-vs-mex-knockout-or-tie.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://blogs.microsoft.co.il/blogs/idof/archive/2011/08/10/wsdl-vs-mex-knockout-or-tie.aspx&amp;amp;bgcolor=6600FF" /&gt;&lt;/a&gt; &lt;a href="http://dotnetshoutout.com/Submit?url=http://blogs.microsoft.co.il/blogs/idof/archive/2011/08/10/wsdl-vs-mex-knockout-or-tie.aspx"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http://blogs.microsoft.co.il/blogs/idof/archive/2011/08/10/wsdl-vs-mex-knockout-or-tie.aspx" style="border:0px;" /&gt;&lt;/a&gt; &lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=882407" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/WSDL+vs+MEX/default.aspx">WSDL vs MEX</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/mex/default.aspx">mex</category><category domain="http://blogs.microsoft.co.il/blogs/idof/archive/tags/wsdl/default.aspx">wsdl</category></item></channel></rss>