MSDN Magazine - December 2007 - (Page 64) populates the Services and Operations lists, as shown in Figure 7. Click OK to generate the proxy code. Now you can add a proxy class field to the add-in class, initialize it in ThisAddIn_Startup, and invoke the WCF Service GetDefinition method through the proxy. In the FishEyeClickEvent handler, replace the message box with a call to GetDefinition, parse the returned XML to extract the Key and Definition, and insert these into the Word document. Note that after inserting the Key text, I move to the end of the insertion before adding the Definition text—this is so that I don’t overwrite the previous selection (see Figure 8). Security Figure 7 Add Service Reference Dialog Given the nature of WPF hosting within VSTO solutions, it should be clear that one of the limitations is that you cannot use Windows Vista Aero™ Glass in the normal way. That is, if your WPF control uses Aero Glass, it will only be transparent to the underlying Windows Forms hosting control, not to the document surface or native Office window. Of course, you can mitigate this by specifying a background image as part of your control; then any Aero Glass controls you lay on top can be transparent to that image. Consuming the WCF Service in the VSTO Solution There are several ways to develop the necessary client-side code to consume a WCF service. You can generate metadata exchange information and client proxy code from the compiled service assembly or from the running service itself. You can execute svcutil.exe manually to generate the code, or you can use the Add Service Reference wizard in Visual Studio 2008. With Visual Studio 2005, you have to use svcutil manually. To do this, first start the service, then open a command window and navigate to the client project folder. Then run svcutil to generate the client-side proxy code. An example command line might look like this: “C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin\svcutil” /language:C# /config:app.config http://localhost:8080/ImageServiceHost/ImageService/ mex /n:*,WordImageSelecter On your development machine, security is set up for the addin solution when you build it. When you deploy the solution, security will be set up as part of the publish operation. Office 2007 introduced new security features and streamlined the way security works for macros, ActiveX controls, and add-ins. All installed add-ins are now accessible through the Office Trust Center. VSTO also made very significant changes to its security model for Visual Studio 2008. The model no longer relies on the version-specific code access security (CAS) repository and is instead integrated into the ClickOnce model. VSTO also layers on additional security for both add-ins and document solutions that intelligently uses digital certificates and/or a registry-based store of the user’s trust Figure 8 Using the WCF Service Client-Side Proxy internal ImageServiceClient serviceClient; private void ThisAddIn_Startup(object sender, System.EventArgs e) { // Connect to the WCF service. serviceClient = new ImageServiceClient(); } // (previously discussed code omitted for brevity). This specifies the URL for the running service along with the metadata exchange endpoint address. It also indicates that C# should be used as the target language, that the configuration information should be output to a file named app.config, and that WordImageSelecter should be the namespace (in this sample, this is the namespace for the add-in project). Once svcutil has generated the C# proxy code and the app.config, they must be added to the add-in project. Also, a reference to System.ServiceModel.dll must be added. Visual Studio 2008 provides a graphical wizard you can use instead of running svcutil manually. To use the wizard, start the service, then right-click the add-in project in Solution Explorer and select the Add Service Reference option from the context menu. In the Add Service Reference dialog, enter the service URL and the target namespace, then click Go. This fetches the service contract and configuration information from the running service and 64 msdnmagazine Office Services private void FishEye_FishEyeClickEvent(object source, FishEyeEventArgs e) { //System.Windows.Forms.MessageBox.Show(e.ButtonName); // Invoke the WCF Service and parse the returned XML. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml( Globals.ThisAddIn.serviceClient.GetDefinition(e.ButtonName)); String keyText = xmlDoc.SelectSingleNode( “descendant::Item/Key”).InnerText; String definitionText = xmlDoc.SelectSingleNode( “descendant::Item/Definition”).InnerText; // Insert the Key text into the Word document. this.Application.Selection.InsertAfter( String.Format(“{0}{1}{2}”, Environment.NewLine, keyText, Environment.NewLine)); object titleStyle = Word.WdBuiltinStyle.wdStyleTitle; this.Application.Selection.set_Style(ref titleStyle); // Move to the end of the insertion. object gotoItem = Word.WdGoToItem.wdGoToLine; object gotoDirection = Word.WdGoToDirection.wdGoToLast; this.Application.Selection.GoTo( ref gotoItem, ref gotoDirection, ref missing, ref missing); // Insert the Definition text into the Word document. Word.Range insertionStart = this.Application.Selection.Range; this.Application.Selection.InsertAfter( String.Format(“{0}{1}”, definitionText, Environment.NewLine)); object headingStyle2 = Word.WdBuiltinStyle.wdStyleHeading2; this.Application.Selection.set_Style(ref headingStyle2); }
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.