Continuing on our series about WCF service, we will now discuss how we can generate the wcf proxy using the SvcUtil.exe command line tool. If you have missed the previous articles, then ypu can read them here:
- About the A.B.C. of WCF Service
- Types of Contracts in WCF
- WCF Proxy generation using 'Add reference'
To use the svcutil, we need to use the Visual Studio command prompt and run a command which specifies the svcutil tool to generate a proxy file for us. The command looks like the following format:
svcutil http://service_url /out:proxy_file_name.cs /config: config_file_name.config /mergeConfig
- service_url : url wcf service is hosted.
- proxy_file_name : Proxy file name which we would like to be generated with.
- config_file_name: Name of the config file which will contain the configuration of the service to be consumed by client.
Here we replace the values with our actual values. So the url becomes:
svcutil http://localhost:13490/Service1.svc?wsdl /out:SampleServiceClient.cs /config:App.config /mergeConfig
Before we run the command, we need to consider following points:
- Run the Visual Studio command prompt in admin mode or else you may face issue with permission in generating files.
- Use of mergeConfig : This specifies that we want the configuration of the wcf service to get merged into our clients' config file. For this option to work properly, make sure that you specify the config file name (in the /config: option) same as that of the clients' config file. In our case, it was App.config in client application, so we specify it to be app.config.
- When we run the command prompt, it will generate the proxy file in the current directory or location and not in our project location. To avoid this, we change the directory to point to our project location, and then run the command.
- We could also have specified the path of the directory where we would like the output files to get generated, using the /directory option. For more options, refer to MSDN.If we do not want to config files to merge, we can specify any other name in the /config: option. When this file gets generated, we can copy the configuration to out clients' config file.
So here, we have changed the directory to our project location and the proxy files will be generated in the current directory i.e. project folders. We have also specified the merge option so that the required configuration is merged in client config file. So let's run the command and see the results.
Include the newly generated proxy file in the project. Now, rest of the things remain the same. The way we call the service from the client remains the same.
Run the code and see the results.
Works like charm. Happy coding...!!!