{"id":413,"date":"2007-09-12T19:29:00","date_gmt":"2007-09-12T19:29:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/andrewarnottms\/2007\/09\/12\/calling-wcf-services-from-netcf-3-5-using-compact-wcf-and-netcfsvcutil-exe\/"},"modified":"2019-04-03T22:37:35","modified_gmt":"2019-04-04T05:37:35","slug":"calling-wcf-services-from-netcf-3-5-using-compact-wcf-and-netcfsvcutil-exe","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/calling-wcf-services-from-netcf-3-5-using-compact-wcf-and-netcfsvcutil-exe\/","title":{"rendered":"Calling WCF services from NetCF 3.5 using Compact WCF and NetCFSvcUtil.exe"},"content":{"rendered":"<p><P>The <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/netframework\/aa497273.aspx\">.NET Compact Framework<\/A> 3.5 adds a subset of the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/netframework\/aa663324.aspx\">Windows Communication Foundation<\/A> (WCF or &#8220;Indigo&#8221;) to smart devices, allowing them to communicate with desktop WCF components with all the flexibility of multiple, interchangeable service bindings and endpoints.&nbsp; Although it ships out of the box only with support for message-level communication (no service calls, per se), we have a code generation tool that will allow you to call WCF or other WSDL-publishing services.<\/P>\n<P>Desktop has an <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa347733.aspx\">svcutil.exe<\/A> tool that generates client proxy code, but the source code it generates cannot compile against NetCF libraries because NetCF does not include all the classes referenced from that source code.&nbsp; While you can modify the code by hand to eventually get it to work with NetCF, we offer an easier way.<\/P>\n<P>The NetCFSvcUtil.exe tool that ships as part of the <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyId=C8174C14-A27D-4148-BF01-86C2E0953EAB&amp;displaylang=en\">Power Toys for .NET Compact Framework 3.5<\/A>&nbsp;supports many of the same command-line switches that svcutil.exe does, but performs a few additional\/modified steps:<\/P>\n<OL>\n<LI>Verifies that the service being consumed&nbsp;offers endpoints compatible&nbsp;with the feature subset included in NetCF 3.5. \n<LI>Generates the client proxy class in C# or VB&nbsp;that compiles against NetCF or desktop WCF. \n<LI>Generates code for a CFClientBase class that fills in where NetCF doesn&#8217;t support calling WCF services. \n<LI>Does <EM>not<\/EM> generate a .config file to store the endpoint information.&nbsp; This information is stored in the client proxy code.&nbsp; If an X509 certificate required by the endpoint, that must be passed into the proxy class&#8217; constructor by your code.<\/LI><\/OL>\n<P>Assuming that you have a service running at http:\/\/localhost\/service\/service.svc&nbsp;on your desktop computer and a device application in your C:\\deviceapp directory, you could generate the code to call the service from NetCF using the following command line.&nbsp; Be sure NetCFSvcUtil.exe is in your path.&nbsp; Also be sure to use a machine name that can be reached from your device.&nbsp; If you use &#8220;http:\/\/localhost\/&#8230;&#8221; as your argument to the tool, your device will look for localhost (which is the device itself!) and not find the service that is running on your Windows box.<\/P>\n<DIV class=\"command\">C:\\deviceapp&gt;netcfsvcutil.exe http:\/\/&lt;machinename&gt;\/service\/service.svc<\/DIV>\n<P>The tool will generate and list two source files.&nbsp; The source file named after your service will be the one that contains the proxy class you&#8217;ll use from your app.&nbsp; You can use it just like you would the svcutil.exe-generated code, except for the provision of secure endpoints made in my numbered list above.<\/P>\n<P>To use these source files in your application, add them to your Visual Studio project and be sure you have System.ServiceModel.dll and System.Runtime.Serialization.dll in your project&#8217;s Referenced Assemblies list in Solution Explorer.&nbsp; Calling your service from your device application can be as simple as a couple of lines of code:<\/P><!-- code formatted by http:\/\/manoli.net\/csharpformat\/ --><PRE class=\"csharpcode\">CalculatorServiceClient client = <SPAN class=\"kwrd\">new<\/SPAN> CalculatorServiceClient();\nMessageBox.Show(client.Add(3, 5));<\/PRE>\n<P>Some features that WSDLs and the desktop .NET WCF supports are not supported by the .NET Compact Framework 3.5.&nbsp; Although NetCFSvcUtil.exe generates code to enable some of these features,&nbsp;here is a list of features that remain <EM>unavailable<\/EM> for our first version of Compact WCF when calling a WCF service on desktop:<\/P>\n<UL>\n<LI>Fault contracts (they are not automatically deserialized for you, but you get the raw xml that you can parse manually) \n<LI>Custom message headers (including peer hop counts) \n<LI>Callback contracts \n<LI>Sessions \n<LI>Mandatory transactions<\/LI><\/UL>\n<P>And the only desktop binding&nbsp;NetCF 3.5&nbsp;supports is basicHttpBinding, with optional WS-Security (X.509 only) and no support for WS-ReliableMessaging.&nbsp; NetCF 3.5 adds the new mail transport as well, but NetCFSvcUtil does not support generating proxies for services exposed only on the mail transport.<\/P>\n<P>It&#8217;s worthwhile to note that any service or client that supports WSDL contracts should be able to intercommunicate, regardless of underlying implementation.&nbsp; This means that if you have a desktop WCF service that you want to call from NetCF, you may also use the already-existing <A href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/vsintro7\/html\/vxtskaddingremovingwebreferences.asp\">Add Web Reference<\/A> in Visual Studio to generate code for your client proxy class (the command line equivalent is <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/7h3ystb6(VS.80).aspx\">wsdl.exe<\/A>).&nbsp; The resulting proxy class may not have as friendly of a public API for you to use, and it won&#8217;t use the WCF stack, but it is a valid alternative that will work in&nbsp;many cases.<\/P>\n<P>The reverse is also true: you can use WCF to call into old-style .asmx web services.&nbsp; It is not necessary to even know which web service implementation is used on a server.&nbsp; So long as it publishes a WSDL, you&#8217;re in luck.&nbsp; If it doesn&#8217;t publish a WSDL, you may still be able to generate a client proxy for it as long as it uses WCF service model and you have access to the managed service assemblies.&nbsp; But that&#8217;s another topic.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The .NET Compact Framework 3.5 adds a subset of the Windows Communication Foundation (WCF or &#8220;Indigo&#8221;) to smart devices, allowing them to communicate with desktop WCF components with all the flexibility of multiple, interchangeable service bindings and endpoints.&nbsp; Although it ships out of the box only with support for message-level communication (no service calls, per [&hellip;]<\/p>\n","protected":false},"author":2685,"featured_media":37840,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[106,4617,3917,3918,383],"class_list":["post-413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-permierdev","tag-net","tag-andarno","tag-mobile-devices","tag-netcf","tag-wcf"],"acf":[],"blog_post_summary":"<p>The .NET Compact Framework 3.5 adds a subset of the Windows Communication Foundation (WCF or &#8220;Indigo&#8221;) to smart devices, allowing them to communicate with desktop WCF components with all the flexibility of multiple, interchangeable service bindings and endpoints.&nbsp; Although it ships out of the box only with support for message-level communication (no service calls, per [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/2685"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=413"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/413\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}