{"id":18517,"date":"2014-08-28T13:38:00","date_gmt":"2014-08-28T13:38:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2014\/08\/28\/using-wcf-in-net-native\/"},"modified":"2021-09-30T16:13:47","modified_gmt":"2021-09-30T23:13:47","slug":"using-wcf-in-net-native","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/using-wcf-in-net-native\/","title":{"rendered":"Using WCF in .NET Native"},"content":{"rendered":"<p style=\"padding-left: 30px\"><em>This post was authored by <strong>Ron Cain, a Software Development Engineer<\/strong> on the WCF team.<\/em><\/p>\n<p>In our <a href=\"http:\/\/blogs.msdn.com\/b\/dotnet\/archive\/2014\/08\/18\/try-out-the-new-releases-net-framework-vnext-asp-net-vnext-net-native-and-ryujit.aspx\">previous post<\/a>, we mentioned that some of the client components of Windows Communication Foundation (WCF) had been made available for .NET Native Windows Store apps in Visual Studio &#8220;14&#8221; CTP3. In this post, I will describe how to build a sample WCF Windows Store App using CTP3 and make it run using the .NET Native compiler.<\/p>\n<p>Note that most of this walk-through is not unique to .NET Native and applies equally well to a conventional Windows Store App. The final section of the walk- through describes the extra couple steps to make it run with .NET Native.<\/p>\n<p>Before you start, follow the directions at <a href=\"http:\/\/support.microsoft.com\/kb\/2967191\">http:\/\/support.microsoft.com\/kb\/2967191<\/a> to install Visual Studio &#8220;14&#8221; CTP3.<\/p>\n<p>If you want to read more about .NET Native, refer to <a href=\"http:\/\/blogs.msdn.com\/b\/dotnet\/archive\/2014\/04\/02\/announcing-net-native-preview.aspx\">Announcing .NET Native Preview<\/a>.<\/p>\n<h2>Create an IIS hosted WCF service to test our app<\/h2>\n<p>Before we build our Windows Store app, we need a WCF service it can use. And because Windows Store apps cannot connect to <code>localhost<\/code> we need to provide a host for this service. For this sample, we&#8217;ll use IIS on our own development machine.<\/p>\n<h2>Enable IIS<\/h2>\n<p>We can enable IIS by swiping in from the right edge of the screen, searching for <strong>Turn Windows features on or off<\/strong> and running it. Checking <strong>Internet Information Services<\/strong> enables IIS to run on this machine.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/6472.Picture_01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/6472.Picture_01.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<h2>Create the WCF service<\/h2>\n<p>We need to open Visual Studio as an Administrator because it will need to configure IIS to host the service. We&#8217;ll start by creating a new WCF application project:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/8372.Picture_02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/8372.Picture_02.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>The out-of-the-box service is fully functional but a little dull, so we&#8217;ll enhance it a bit. We do this by editing the generated WCF service contract (<code>IService1.cs<\/code>) to add a new <code>SayHello()<\/code> method. Marking it with <code>[OperationContract]<\/code> makes this method callable from WCF clients.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/2072.Picture_03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/2072.Picture_03.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>And of course we need to implement this new interface method, so we add code to the generated service implementation class (<code>Service1.svc.cs<\/code>):<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/7762.Picture_04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/7762.Picture_04.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>The WCF service is now complete. After building it, all that remains is to host it. We do this by right clicking on the MyWCFService in the <strong>Solution Explorer<\/strong> and opening its <strong>Properties<\/strong> window. In the Web tab, we choose &#8220;Local IIS&#8221;. This means the IIS we enabled on our development machine will be the host for this service.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/8055.Picture_05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/8055.Picture_05.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>We&#8217;ll be asked to create a <strong>Virtual Directory<\/strong> when we save, which is the final step to host the service. Say <strong>Yes<\/strong>. At this point, IIS is hosting our service.<\/p>\n<p>We can verify the service is available by right-clicking <code>Service1.svc<\/code> and choosing <strong>View in Browser<\/strong>. If we see something like the screenshot below, we know the service is running in IIS and ready to use. Notice the URL. We will need it later.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/3755.Picture_06.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/3755.Picture_06.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>It is a good idea to test this URL from another machine, after replacing <code>localhost<\/code> with the machine name. If necessary grant World Wide Web Services (HTTP) through Windows or any other Firewall on the machine(s). This URL with the machine name is the one we want to use from our Windows Store app.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/5751.Picture_07.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/5751.Picture_07.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<h2>Create the WCF Windows Store client app<\/h2>\n<p>So our WCF service is up and running. Let&#8217;s make a Windows Store client to call it. In this section, we&#8217;ll create a conventional Windows Store app. In the next section, we&#8217;ll make it run in .NET Native.<\/p>\n<p>We&#8217;ll start by creating a new <strong>Blank App<\/strong> Windows Store project. We can use the same development machine or a different one. I created mine on a different one to ensure I didn&#8217;t inadvertently rely on <strong>localhost<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/0763.Picture_08.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/0763.Picture_08.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>And this is the project generated for us.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/7532.Picture_09.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/7532.Picture_09.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>We can link to the WCF service we created by right-clicking <strong>References<\/strong> in <strong>Solution Explorer<\/strong> and choosing <strong>Add Service Reference<\/strong>. We set the <strong>Address<\/strong> field to be the URL we used to browse to the service. Because Windows Store apps cannot access <code>localhost<\/code>, we must use the URL with the machine name where IIS is running. Then we press <strong>Go<\/strong>, select <code>Service1<\/code>, and OK. For this exercise, I recommend keeping the default names in case you want to paste code from this post.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/2541.Picture_10.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/2541.Picture_10.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>By using <strong>Add Service Reference<\/strong> we have caused code to be generated that can communicate with our WCF service. This code is normally hidden, but if you&#8217;re curious what it looks like, click the <strong>Show All Files<\/strong> button in the <strong>Solution Explorer<\/strong> and navigate to <code>Reference.cs<\/code>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/8081.Picture_11.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/8081.Picture_11.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>To reach our external WCF service, our app will need access to the network. We do this by double-clicking <code>Package.appxmanifest<\/code>, selecting the <strong>Capabilities<\/strong> tab and checking the <strong>Internet and Private Network<\/strong> checkboxes.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/7242.Picture_12.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/7242.Picture_12.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>Now let&#8217;s add some UI to the store app. Here I&#8217;ve double-clicked <code>MainPage.xaml<\/code> and dragged from the <strong>Toolbox<\/strong> two <code>TextBox<\/code> controls and a <code>Button<\/code>. I used the <strong>Properties<\/strong> pane (F4) to change their names to <code>TheInputBox<\/code>, <code>TheOutputBox<\/code> and <code>TheButton<\/code>. I never claimed to be a designer or good at naming pets.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/0624.Picture_13.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/0624.Picture_13.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>I changed <code>TheButton<\/code>&#8216;s text, and I also double-clicked on it to create a handler. If you want to compare notes, this is what my XAML code looked like for the <code>Grid<\/code>:<\/p>\n<pre><code>&lt;Grid Background=\"{ThemeResource ApplicationPageBackgroundThemeBrush}\"&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;TextBox x:Name=\"TheInputBox\" HorizontalAlignment=\"Left\" Margin=\"203,197,0,0\" TextWrapping=\"Wrap\"<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text=\"TextBox\" VerticalAlignment=\"Top\" Height=\"31\" Width=\"164\" \/&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;TextBox x:Name=\"TheOutputBox\" HorizontalAlignment=\"Left\" Margin=\"203,300,0,0\" TextWrapping=\"Wrap\"<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text=\"TextBox\" VerticalAlignment=\"Top\" Height=\"28\" Width=\"164\" \/&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;Button x:Name=\"TheButton\" Click=\"TheButton_Click\" Content=\"Call WCF service\"<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HorizontalAlignment=\"Left\" Margin=\"200,244,0,0\" VerticalAlignment=\"Top\" Width=\"170\" \/&gt;<br \/>&lt;\/Grid&gt;<\/code><\/pre>\n<p>All that remains to complete this as a normal Windows Store app is to wire up the button. We do this by choosing <strong>View Code<\/strong> on <code>TheButton<\/code> and add this code:<\/p>\n<pre><code>private async void TheButton_Click(object sender, RoutedEventArgs e)<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var client = new ServiceReference1.Service1Client();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;TheOutputBox.Text = await client.SayHelloAsync(TheInputBox.Text);<br \/>}<\/code><\/pre>\n<p>It is worth looking at this code for a moment.<\/p>\n<p><code>ServiceReference1.Service1Client<\/code> is in the hidden file <code>Reference.cs<\/code> generated by <strong>Add Service Reference<\/strong>. <code>SayHelloAsync()<\/code> is the method that was generated to invoke the <code>SayHello()<\/code> method of our WCF service. The method name ends in <code>Async<\/code> to indicate it is asynchronous and returns a <code>Task<\/code>. In this code we create a <code>Service1Client<\/code>, call <code>SayHelloAsync()<\/code> and <code>await<\/code> the <code>Task<\/code> it returns. We use <code>await<\/code> because we don&#8217;t want to block the UI thread. The <code>Tasks<\/code>&#8216;s result obtained via <code>await<\/code> is the value returned from the server. Note that we needed to add <code>async<\/code> to the <code>TheButton_Click<\/code> method signature so that we could use <code>await<\/code>. I&#8217;ve omitted exception handling for brevity.<\/p>\n<p>At this point, we&#8217;ve finished our conventional Windows Store app and can run it. But the purpose of this sample is to show how we can make a conventional WCF store app into a .NET Native app, so let&#8217;s do that.<\/p>\n<h2>Final step &ndash; make the app use .NET Native<\/h2>\n<p>First, we must change the configuration from <strong>Any CPU<\/strong> to target a specific architecture. This is required for .NET Native. For this sample, let&#8217;s use <strong>x64<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/8637.Picture_14.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/8637.Picture_14.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>Then we right-click on the project and select <strong>Enable for .NET Native<\/strong>:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/4370.Picture_15.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/4370.Picture_15.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>Setting this property triggers analysis of our code for compatibility with .NET Native and produces a report identifying any issues. We&#8217;ll see the report, but we should not see any issues with this sample.<\/p>\n<p>The final step is to configure the application to build as a .NET Native app. We right-click on the project, choose Properties, and on the <strong>Build<\/strong> tab check the <strong>Compile with NET Native tool chain<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/1667.Picture_16.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/1667.Picture_16.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>That&#8217;s it. The app is complete and ready to use the .NET Native compiler when it builds.<\/p>\n<p>To test the app, we launch it in the debugger (<kbd>F5<\/kbd>), enter a name, press the button, and see the reply back from the WCF service. There we have it! We&#8217;ve sent a request from our .NET Native executable to the external WCF service and received a successful reply.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/5633.Picture_17.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/5633.Picture_17.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<h2>Next steps<\/h2>\n<p>We encourage you to use this sample app and change the hosted WCF service interface to do more interesting things, like exchanging complex <code>[DataContract]<\/code> types, etc. Be aware that when you change the public interface between the service and the client, you will need to click on the service reference and choose <strong>Update Service Reference<\/strong>. This will contact the WCF service, acquire the new service contract and update the generated code.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/6201.Picture_18.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/6201.Picture_18.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<h2>Troubleshooting<\/h2>\n<p>Because this is a very early preview of WCF in .NET Native, you may encounter issues. I&#8217;ll give an example of one such issue and how it was resolved.<\/p>\n<p>I built a .NET Native Windows Store app exactly as described in this post, but experimentally used <a href=\"http:\/\/dev.virtualearth.net\/webservices\/v1\/routeservice\/routeservice.svc\">Virtual Earth<\/a>. for the <strong>Add Service Reference<\/strong>. When I tried to run it, I encountered an <code>InvalidOperationException<\/code> creating the client. Debugging showed this app uses WCF&#8217;s <code>FaultContractAttribute<\/code>, and unfortunately we had not enabled Reflection over <code>FaultContractAttribute<\/code> in CTP3 (oops).<\/p>\n<p>The fix was to enable Reflection for that type from within the app itself. The file <code>default.rd.xml<\/code> in the app contains <strong>Runtime Directives<\/strong> to the .NET Native compiler. You can read more about the schema of this file and see some <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dn600639(v=vs.110).aspx\">examples<\/a>.<\/p>\n<p>In my case, I edited this file as shown below to say &#8220;my app needs to use Reflection on <code>System.ServiceModel.FaultContractAttribute<\/code>.&#8221; With that change, the exception was no longer thrown.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/5824.Picture_19.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2014\/08\/5824.Picture_19.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<h2>Conclusion<\/h2>\n<p>We will continue to stabilize and enhance WCF for .NET Native in upcoming releases. In future posts, we&#8217;ll talk more about these enhancements and provide more samples. We are very interested in hearing from you, especially about the features you need and why they are important to you.<\/p>\n<p>We welcome your feedback on the developer experience using WCF with.NET Native. Please leave comments on this post or send mail to&nbsp;<a href=\"mailto:dotnetnative@microsoft.com\">dotnetnative@microsoft.com<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post was authored by Ron Cain, a Software Development Engineer on the WCF team. In our previous post, we mentioned that some of the client components of Windows Communication Foundation (WCF) had been made available for .NET Native Windows Store apps in Visual Studio &#8220;14&#8221; CTP3. In this post, I will describe how to [&hellip;]<\/p>\n","protected":false},"author":335,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685],"tags":[66],"class_list":["post-18517","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","tag-dotnetnative"],"acf":[],"blog_post_summary":"<p>This post was authored by Ron Cain, a Software Development Engineer on the WCF team. In our previous post, we mentioned that some of the client components of Windows Communication Foundation (WCF) had been made available for .NET Native Windows Store apps in Visual Studio &#8220;14&#8221; CTP3. In this post, I will describe how to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/18517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/335"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=18517"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/18517\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=18517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=18517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=18517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}