December 24, 2020

dotnet-svcutil - Generate Proxy Code with namespace

dotnet-svcutil helps you generate WCF Client proxy code for target service, for example:

dotnet-svcutil https://localhost:8081/OrderManagement/OrderManagement.svc 
               -d "Connected Services/MyOrderManagementService"

In this command we have provided directory path where we want to generate the proxy code output. In this case we have the path Connected Services/MyOrderManagementService from current directory.

You may notice that in the output code, it will generate the namespace with similar name, i.e. namespace Connected_Services_MyOrderManagementService:

If you want to change the namespace, you can use n (namespace) parameter.

Using this command:

dotnet-svcutil https://localhost:8081/OrderManagement/OrderManagement.svc 
               -d "Connected Services/MyOrderManagementService"
               -n "*,MyNewNamespaceForOrderManagementService"

This time, the output code will contain the namespace MyNewNamespaceForOrderManagementService.

References:

Related Post(s):

December 23, 2020

dotnet-svcutil - Generate Proxy Code with directory path

dotnet-svcutil helps you generate WCF Client proxy code for target service, for example:

dotnet-svcutil https://localhost:8081/OrderManagement/OrderManagement.svc

It will create a directory named ServiceReference, and generate Reference.cs file contains proxy code within ServiceReference directory.

ServiceReference directory will be created at the root from where you have executed above command.

If we run this command from the root of the .Net Core Project directory, it will create the same directory named ServiceReference.

Usualy we want it to generate proxy code under Connected Services directory.

For this we need to use the parameter d to provide directory to create files in.

dotnet-svcutil https://localhost:8081/OrderManagement/OrderManagement.svc 
               -d "Connected Services/MyOrderManagementService"

This will create the output at path Connected Services/MyOrderManagementService within the current directory where you are executed it.

References:

Related Post(s):

dotnet-svcutil - Generate Proxy Code With Collection Types

In the last post (WCF Proxy generation – using dotnet-svcutil) we have seen how to generate WCF proxy code using dotnet-svcutil.

You might have noticed that dotnet-svcutil has generated the collection types as arrays. For example, if the target service has any collection type in contract property like List<string>, then it is shown as array of string (string[]) in generated code.

You have to tell dotnet-svcutil to use that collection type by specifying a collection type parameter ct. But also you have to specify another reference parameter r to provide full path for the System.Collections assembly, otherwise it will give you error.

First run this command without providing the assembly path for System.Collection.

dotnet-svcutil https://localhost:8081/OrderManagement/OrderManagement.svc 
               -ct "System.Collections.Generic.List`1"

You will get this error message:

Error: No type could be loaded for the value 'System.Collections.Generic.List1' 
passed to the --collectionType option. Ensure that the assembly this type belongs 
to is specified via the --reference option.

Now provide a valid assembly path for System.Collection in r parameter:

dotnet-svcutil https://localhost:8081/OrderManagement/OrderManagement.svc
	-ct "System.Collections.Generic.List`1" 
	-r "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\
           microsoft.netcore.app\2.2.0\ref\netcoreapp2.2\System.Collections.dll"

This command will create a Reference.cs file within ServiceReference folder, this time with collection type List<string> rather than string[].

Note: If you are running this command from powershell you might still get above error. Try to run from Windows Command Prompt and it will run successfully.

References:

Related Post(s):

December 22, 2020

WCF Proxy generation – using dotnet-svcutil

In the last post (WCF Proxy generation – using SvcUtil.exe) we have seen how to generate WCF proxy code using SvcUtil.exe.

In this post we will be looking into another option dotnet-svcutil.

This is a .NET Core CLI tool and is available cross-platform on Linux, macOS, and Windows.

Following are some of the reasons you may want to use dotnet-svcutil:

  • You can easily install it as NuGet package.
  • It will work even if you have a lower version of Visual Studio like the VS2017 (15.5 or less) version.
  • Since it is not bounded with Visual Studio version, you can also use it with free VS Code.
  • It is available cross-platform on Linux, macOS, and Windows.

From Visual Studio you can install using Nuget package manager as below:

Another option is to install it globally through the command line so that it can be used across the system.

You can use below command to install globally:

dotnet tool install --global dotnet-svcutil --version 2.0.2

Once installed, use this command to generate proxy code:

dotnet-svcutil https://localhost:8081/OrderManagement/OrderManagement.svc

This command will create a Reference.cs file within ServiceReference folder.

References:

Related Post(s):

December 19, 2020

WCF Proxy generation – using SvcUtil.exe

SvcUtil.exe is the the ServiceModel Metadata Utility tool used to generate service model code from the service URL or wsdl file.

In this example, we will generate client proxy classes by using service URL.

First make sure to have the target service up and running.

After verifying the service status go to Visual Studio Command Prompt and run the following command.

svcutil https://localhost:8081/OrderManagement/OrderManagement.svc /Language=c# 
		/t:Code /out:OrderManagementProxy.cs 
                /config:OrderManagementProxy.config

It contains following parameters:

  • First is the URL of the target service.
  • /Language specifies the output language in which we want to generate code.
  • /t Specifies the output to be generated by the tool, since we are generating proxy code so we will provide the value code.
  • /out Specifies the target file name for the generated code.
  • /config Specifies the filename for the generated configuration file, Default value is output.config

For config file you can also use another parameter /mergeConfig. It merges the generated configuration into an existing file, instead of overwriting the existing file. This will be useful if you provide your regular application's config file in the /config paramter, so in that case you dont want to overwrite that config file but to merge the new changes with existing configuration. For example like this:

svcutil https://localhost:8081/OrderManagement/OrderManagement.svc /Language=c# 
		/t:Code /out:OrderManagementProxy.cs 
                /config:app.config /mergeConfig

You can find the generated output code in the file OrderManagementProxy.cs in the current directory from where you run this command.

References:

Related Post(s):