A day with .Net

My day to day experince in .net

Create proxy of WCF web service with svcutil

Posted by vivekcek on April 27, 2011

So dear friends today i am writing about how to consume a real world WCF service in your application.Below i am listing the steps you must follow.

1.Get the URL of our web service,like http://demo.myservice.com/Service/MyWebService.svc.

2.Open the visual studio command prompt type the following command

svcutil.exe http://demo.myservice.com/Service/MyWebService.svc?wsdl

3.Go to your visual studio command directory like (d:\Program Files\Microsoft Visual Studio 2008\VC).

4.You can find a c sharp class file named MyWebService.cs and an output.config file.

5.Add MyWebService.cs file to your project,Refer this in your classes.

6.The following name spaces should be needed in your consuming class.

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;

7.Open the output.config file you obtained through svcutil.An output.config file is given below

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ICarService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://demo.myservice.com/Service/MyWebService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
                contract="IMyService" name="BasicHttpBinding_IMyService" />
        </client>
    </system.serviceModel>
</configuration>

8.Note the enpoint address here is http://demo.myservice.com/Service/MyWebService.svc

9.Binding is BasicHttpBinding.

10.Create binding using the following code.

            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Name = "BasicHttpBinding_IMyService";
            binding.CloseTimeout = TimeSpan.FromMinutes(1);
            binding.OpenTimeout = TimeSpan.FromMinutes(1);
            binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            binding.SendTimeout = TimeSpan.FromMinutes(1);
            binding.AllowCookies = false;
            binding.BypassProxyOnLocal = false;
            binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            binding.MaxBufferSize = 65536;
            binding.MaxBufferPoolSize = 524288;
            binding.MessageEncoding = WSMessageEncoding.Text;
            binding.TextEncoding = System.Text.Encoding.UTF8;
            binding.TransferMode = TransferMode.Buffered;
            binding.UseDefaultWebProxy = true;

            binding.ReaderQuotas.MaxDepth = 32;
            binding.ReaderQuotas.MaxStringContentLength = 8192;
            binding.ReaderQuotas.MaxArrayLength = 16384;
            binding.ReaderQuotas.MaxBytesPerRead = 4096;
            binding.ReaderQuotas.MaxNameTableCharCount = 16384;

            binding.Security.Mode = BasicHttpSecurityMode.None;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
            binding.Security.Transport.Realm = "";
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;

11.Create an endpoint.

EndpointAddress ed = new EndpointAddress("http://demo.myservice.com/Service/MyWebService.svc");

12.Create a web service client of the proxy class using the binding and endpoint we created.

ServiceClient _client = new ServiceClient(binding, ed);
Advertisements

One Response to “Create proxy of WCF web service with svcutil”

  1. Udeesh S B said

    Hi,

    Nice article.

    but
    when I ran the (3.Go to your visual studio command directory like (d:\Program Files\Microsoft Visual Studio 2008\VC).) usign the URL.

    It is created only .cs file only and output.config file i’m not found in the ” VC” folder.

    and further steps im not understand to follow. Please help me. Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s