|
1 | 1 | ---
|
2 | 2 | title: "How to: Register and Configure a Service Moniker"
|
3 | 3 | ms.date: "03/30/2017"
|
4 |
| -helpviewer_keywords: |
| 4 | +helpviewer_keywords: |
5 | 5 | - "COM [WCF], configure service monikers"
|
6 | 6 | - "COM [WCF], register service monikers"
|
7 | 7 | ms.assetid: e5e16c80-8a8e-4eef-af53-564933b651ef
|
8 | 8 | ---
|
9 | 9 | # How to: Register and Configure a Service Moniker
|
10 |
| -Before using the Windows Communication Foundation (WCF) service moniker within a COM application with a typed contract, you must register the required attributed types with COM, and configure the COM application and the moniker with the required binding configuration. |
11 |
| - |
12 |
| -### To register the required attributed types with COM |
13 |
| - |
14 |
| -1. Use the [ServiceModel Metadata Utility Tool (Svcutil.exe)](../../../../docs/framework/wcf/servicemodel-metadata-utility-tool-svcutil-exe.md) tool to retrieve the metadata contract from the WCF service. This generates the source code for a WCF client assembly and a client application configuration file. |
15 |
| - |
16 |
| -2. Ensure that the types in the assembly are marked as `ComVisible`. To do so, add the following attribute to the AssemblyInfo.cs file in your Visual Studio project. |
17 |
| - |
| 10 | + |
| 11 | +Before using the Windows Communication Foundation (WCF) service moniker within a COM application with a typed contract, you must register the required attributed types with COM, and configure the COM application and the moniker with the required binding configuration. |
| 12 | + |
| 13 | +## Register the required attributed types with COM |
| 14 | + |
| 15 | +1. Use the [ServiceModel Metadata Utility Tool (Svcutil.exe)](../servicemodel-metadata-utility-tool-svcutil-exe.md) tool to retrieve the metadata contract from the WCF service. This generates the source code for a WCF client assembly and a client application configuration file. |
| 16 | + |
| 17 | +2. Ensure that the types in the assembly are marked as `ComVisible`. To do so, add the following attribute to the *AssemblyInfo.cs* file in your Visual Studio project. |
| 18 | + |
18 | 19 | ```csharp
|
19 |
| - [assembly: ComVisible(true)] |
20 |
| - ``` |
21 |
| - |
22 |
| -3. Compile the managed WCF client as a strong-named assembly. This requires signing with a cryptographic key pair. For more information, see [Signing an Assembly with a Strong Name](../../../standard/assembly/sign-strong-name.md). |
23 |
| - |
24 |
| -4. Use the Assembly Registration (Regasm.exe) tool with the `/tlb` option to register the types in the assembly with COM. |
25 |
| - |
26 |
| -5. Use the Global Assembly Cache (Gacutil.exe) tool to add the assembly to the global assembly cache. |
27 |
| - |
| 20 | + [assembly: ComVisible(true)] |
| 21 | + ``` |
| 22 | + |
| 23 | +3. Compile the managed WCF client as a strong-named assembly. This requires signing with a cryptographic key pair. For more information, see [Signing an Assembly with a Strong Name](../../../standard/assembly/sign-strong-name.md). |
| 24 | + |
| 25 | +4. Use the Assembly Registration (Regasm.exe) tool with the `-tlb` option to register the types in the assembly with COM. |
| 26 | + |
| 27 | +5. Use the Global Assembly Cache (Gacutil.exe) tool to add the assembly to the global assembly cache. |
| 28 | + |
28 | 29 | > [!NOTE]
|
29 |
| - > Signing the assembly and adding it to the Global Assembly Cache are optional steps, but they can simplify the process of loading the assembly from the correct location at runtime. |
30 |
| - |
31 |
| -### To configure the COM application and the moniker with the required binding configuration |
32 |
| - |
33 |
| -- Place the binding definitions (generated by the [ServiceModel Metadata Utility Tool (Svcutil.exe)](../../../../docs/framework/wcf/servicemodel-metadata-utility-tool-svcutil-exe.md) in the generated client application configuration file) in the client application's configuration file. For example, for a Visual Basic 6.0 executable named CallCenterClient.exe, the configuration should be placed in a file named CallCenterConfig.exe.config within the same directory as the executable. The client application can now use the moniker. Note that the binding configuration is not required if using one of the standard binding types provided by WCF. |
34 |
| - |
35 |
| - The following type is registered. |
36 |
| - |
37 |
| - ```csharp |
38 |
| - using System.ServiceModel; |
39 |
| - |
40 |
| - [ServiceContract] |
41 |
| - public interface IMathService |
42 |
| - { |
43 |
| - [OperationContract] |
44 |
| - public int Add(int x, int y); |
45 |
| - [OperationContract] |
46 |
| - public int Subtract(int x, int y); |
47 |
| - } |
48 |
| - ``` |
49 |
| - |
50 |
| - The application is exposed using a `wsHttpBinding` binding. For the given type and application configuration, the following example moniker strings are used. |
51 |
| - |
52 |
| - ``` |
53 |
| - service4:address=http://localhost/MathService, binding=wsHttpBinding, bindingConfiguration=Binding1 |
54 |
| - ``` |
55 |
| - |
56 |
| - `or` |
57 |
| - |
58 |
| - ``` |
59 |
| - service4:address=http://localhost/MathService, binding=wsHttpBinding, bindingConfiguration=Binding1, contract={36ADAD5A-A944-4d5c-9B7C-967E4F00A090} |
60 |
| - ``` |
61 |
| - |
62 |
| - You can use either of these moniker strings from within a Visual Basic 6.0 application, after adding a reference to the assembly that contains the `IMathService` types, as shown in the following sample code. |
63 |
| - |
| 30 | + > Signing the assembly and adding it to the Global Assembly Cache are optional steps, but they can simplify the process of loading the assembly from the correct location at runtime. |
| 31 | + |
| 32 | +## Configure the COM application and the moniker with the required binding configuration |
| 33 | + |
| 34 | +- Place the binding definitions (generated by the [ServiceModel Metadata Utility Tool (Svcutil.exe)](../servicemodel-metadata-utility-tool-svcutil-exe.md) in the generated client application configuration file) in the client application's configuration file. For example, for a Visual Basic 6.0 executable named CallCenterClient.exe, the configuration should be placed in a file named CallCenterConfig.exe.config within the same directory as the executable. The client application can now use the moniker. Note that the binding configuration is not required if using one of the standard binding types provided by WCF. |
| 35 | + |
| 36 | + The following type is registered. |
| 37 | + |
| 38 | + ```csharp |
| 39 | + using System.ServiceModel; |
| 40 | + |
| 41 | + [ServiceContract] |
| 42 | + public interface IMathService |
| 43 | + { |
| 44 | + [OperationContract] |
| 45 | + public int Add(int x, int y); |
| 46 | + [OperationContract] |
| 47 | + public int Subtract(int x, int y); |
| 48 | + } |
| 49 | + ``` |
| 50 | + |
| 51 | + The application is exposed using a `wsHttpBinding` binding. For the given type and application configuration, the following example moniker strings are used. |
| 52 | + |
| 53 | + ``` |
| 54 | + service4:address=http://localhost/MathService, binding=wsHttpBinding, bindingConfiguration=Binding1 |
| 55 | + ``` |
| 56 | + |
| 57 | + or |
| 58 | + |
| 59 | + ``` |
| 60 | + service4:address=http://localhost/MathService, binding=wsHttpBinding, bindingConfiguration=Binding1, contract={36ADAD5A-A944-4d5c-9B7C-967E4F00A090} |
| 61 | + ``` |
| 62 | + |
| 63 | + You can use either of these moniker strings from within a Visual Basic 6.0 application, after adding a reference to the assembly that contains the `IMathService` types, as shown in the following sample code. |
| 64 | + |
64 | 65 | ```vb
|
65 |
| - Dim MathProxy As IMathService |
66 |
| - Dim result As Integer |
67 |
| - |
68 |
| - Set MathProxy = GetObject( _ |
69 |
| - "service4:address=http://localhost/MathService, _ |
70 |
| - binding=wsHttpBinding, _ |
71 |
| - bindingConfiguration=Binding1") |
72 |
| - |
73 |
| - result = MathProxy.Add(3, 5) |
74 |
| - ``` |
75 |
| - |
76 |
| - In this example, the definition for the binding configuration `Binding1` is stored in a suitably named configuration file for the client application, such as vb6appname.exe.config. |
77 |
| - |
| 66 | + Dim mathProxy As IMathService |
| 67 | + Dim result As Integer |
| 68 | + |
| 69 | + Set mathProxy = GetObject( _ |
| 70 | + "service4:address=http://localhost/MathService, _ |
| 71 | + binding=wsHttpBinding, _ |
| 72 | + bindingConfiguration=Binding1") |
| 73 | + |
| 74 | + result = mathProxy.Add(3, 5) |
| 75 | + ``` |
| 76 | + |
| 77 | + In this example, the definition for the binding configuration `Binding1` is stored in a suitably named configuration file for the client application, such as *vb6appname.exe.config*. |
| 78 | + |
78 | 79 | > [!NOTE]
|
79 |
| - > You can use similar code in a C#, a C++, or any other .NET Language application. |
80 |
| - |
| 80 | + > You can use similar code in a C#, a C++, or any other .NET Language application. |
| 81 | + |
81 | 82 | > [!NOTE]
|
82 |
| - > : If the moniker is malformed or if the service is unavailable, the call to `GetObject` returns an error of "Invalid Syntax". If you receive this error, make sure the moniker you are using is correct and the service is available. |
83 |
| - |
84 |
| - Although this topic focuses on using the service moniker from Visual Basic 6.0 code, you can use a service moniker from other languages. When using a moniker from C++ code the Svcutil.exe generated assembly should be imported with "no_namespace named_guids raw_interfaces_only" as shown in the following code. |
85 |
| - |
| 83 | + > If the moniker is malformed or if the service is unavailable, the call to `GetObject` returns an error of "Invalid Syntax". If you receive this error, make sure the moniker you are using is correct and the service is available. |
| 84 | + |
| 85 | + Although this topic focuses on using the service moniker from Visual Basic 6.0 code, you can use a service moniker from other languages. When using a moniker from C++ code the Svcutil.exe generated assembly should be imported with "no_namespace named_guids raw_interfaces_only" as shown in the following code. |
| 86 | + |
86 | 87 | ```cpp
|
87 |
| - #import "ComTestProxy.tlb" no_namespace named_guids |
88 |
| - ``` |
89 |
| - |
90 |
| - This modifies the imported interface definitions so that all methods return an `HResult`. Any other return values are converted into out parameters. The overall execution of the methods remains the same. This allows you to determine the cause of an exception when calling a method on the proxy. This functionality is only available from C++ code. |
91 |
| - |
| 88 | + #import "ComTestProxy.tlb" no_namespace named_guids |
| 89 | + ``` |
| 90 | + |
| 91 | + This modifies the imported interface definitions so that all methods return an `HResult`. Any other return values are converted into out parameters. The overall execution of the methods remains the same. This allows you to determine the cause of an exception when calling a method on the proxy. This functionality is only available from C++ code. |
| 92 | + |
92 | 93 | ## See also
|
93 | 94 |
|
94 |
| -- [ServiceModel Metadata Utility Tool (Svcutil.exe)](../../../../docs/framework/wcf/servicemodel-metadata-utility-tool-svcutil-exe.md) |
| 95 | +- [ServiceModel Metadata Utility Tool (Svcutil.exe)](../servicemodel-metadata-utility-tool-svcutil-exe.md) |
0 commit comments