Pages

Monday, July 29, 2013

Generating a proxy using svcutil.exe in WCF

Generating a proxy using svcutil.exe in WCF
Step 1:- When a service is run we can see all the .svc files available in that service.  We will get a command as follows    
Step 2:- Right Click on the Visual Studio Command Prompt (2010) available under the Visual Studio Tools and run it as administrator.
Step 3:- In the command prompt copy paste the
svcutil.exe http://localhost:25016/Service1.svc?wsdl and click on enter it will generate the proxy as well as the config file. The output paths of these files are also mentioned.

C:\Windows\system32>svcutil.exe http://localhost:25016/Service1.svc?wsdl
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1]
Copyright (c) Microsoft Corporation.  All rights reserved.

Attempting to download metadata from 'http://localhost:25016/Service1.svc?wsdl'
using WS-Metadata Exchange or DISCO.
Generating files...
C:\Windows\system32\Service1.cs
C:\Windows\system32\output.config

C:\Windows\system32>


If we open up the Service1.cs we can see the Service Contracts, Operation Contracts and the DataContracts that We have created in the WCF Service. The contents of the proxy file i.e. Service1.cs is as follows:-
Service1.cs
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.17929
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace DemoWcfService
{
    using System.Runtime.Serialization;
   
   
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="CompositeType", Namespace="http://schemas.datacontract.org/2004/07/DemoWcfService")]
    public partial class CompositeType : object, System.Runtime.Serialization.IExtensibleDataObject
    {
       
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
       
        private bool BoolValueField;
       
        private string StringValueField;
       
        public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }
       
        [System.Runtime.Serialization.DataMemberAttribute()]
        public bool BoolValue
        {
            get
            {
                return this.BoolValueField;
            }
            set
            {
                this.BoolValueField = value;
            }
        }
       
        [System.Runtime.Serialization.DataMemberAttribute()]
        public string StringValue
        {
            get
            {
                return this.StringValueField;
            }
            set
            {
                this.StringValueField = value;
            }
        }
    }
}


[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="IService1")]
public interface IService1
{
   
    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
    string GetData(int value);
   
    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetDataUsingDataContract", ReplyAction="http://tempuri.org/IService1/GetDataUsingDataContractResponse")]
    DemoWcfService.CompositeType GetDataUsingDataContract(DemoWcfService.CompositeType composite);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface IService1Channel : IService1, System.ServiceModel.IClientChannel
{
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1
{
   
    public Service1Client()
    {
    }
   
    public Service1Client(string endpointConfigurationName) :
            base(endpointConfigurationName)
    {
    }
   
    public Service1Client(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {
    }
   
    public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {
    }
   
    public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
    {
    }
   
    public string GetData(int value)
    {
        return base.Channel.GetData(value);
    }
   
    public DemoWcfService.CompositeType GetDataUsingDataContract(DemoWcfService.CompositeType composite)
    {
        return base.Channel.GetDataUsingDataContract(composite);
    }
}




The End Points can be found in the output.config file .The endpoint  and the binding informantion which needs to be added in the client can be found in the config file. The file contents will be similar to as follows:-
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:25016/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>



In a case when you might have added a new operation contract or need to verify if that operation contract is showing in the service then click on the svcutil.exe http://localhost:25016/Service1.svc?wsdl.
The new operation contract should be available in the wsdl otherwise check in the code if there are any tags missing or some other issue. Similarly we can do the same for datacontracts , members of a datacontract.

No comments:

Post a Comment