Posts Tagged ‘WCF’

A small tip for you..If you come across this error,The type or namespace name ‘DataContractAttribute’ could not be found (are you missing a using directive or an assembly reference?) make sure that you add the reference to dll System.Runtime.Serialization and refer the namespace.

Manually writing proxies

Posted: February 23, 2011 in WCF
Tags: , ,

Whoever comes from a soap service background always may have an assumption that for generating proxies you need to rely on svcutil.exe.If you think so,this assumption is wrong.You can create a proxy manually too.I am explaining here how you can do for a WCF service.  

The proxy class derives from the class ClientBase<T> & ClientBase<T> accepts a single generic type parameter which is the service contract this proxy encapsulates. Say you have a servicecontract IVendorServices,then ClientBase<T> gets replaced with ClientBase<IVendorServices>.

   [ServiceContract]
    public interface IVendorServices
    {

        [WebGet(UriTemplate = "v1/Ping")]
        [OperationContract]
        string Ping();
    }

public class VendorServices : IVendorServices
    {

        [Description("Service for Testing")]
        public string Ping()
        {
            return DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss:fffffffK");
        }

    }

public class VendorProxy : ClientBase<IVendorServices>, IVendorServices
    {
        public VendorProxy()
        {
        }
        #region IVendorServices Members

        public string Ping()
        {
            return Channel.Ping();
        }

        #endregion
    }

After you write the proxy class,if you place mouse over the interface name,it will create the members of class automatically.The Channel property of ClientBase<T> is of the type of  T parameter passed & if you write Channel.Ping(),it delegates the call to the method’s actual implementation & returns you the result.

 Hope you got some idea on how it works 🙂

The Configuration Editor Tool (SvcConfigEditor.exe) helps you to create and edit configuration settings WCF bindings, behaviors, services,diagnostics for wcfservices without having them modified manually.You can find the exe in the below path.

“C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\SvcConfigEditor.exe”

After you launch the Service Configuration Editor, you can use the File/Open menu to browse for the service or assembly you want to manage.

More about this,you can read from here

If your client receives error The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element from WCF Service,it means that your client web.config has MaxReceivedMessageSize property set to default value 65536 which has exceeded resulting in above exception.Change the MaxReceivedMessageSize settings in web.config to a large value to overcome this.

<basicHttpBinding>
<binding name="BasicHttpBinding_InternalSOAP" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="20000000" maxBufferPoolSize="524288" maxReceivedMessageSize="20000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
</binding>
</basicHttpBinding>

Say,you have a WCF client that invokes a Windows Service.If you make changes to the Windows Service, before you call from client,you must first stop Windows Service and uninstall it. To stop the service, right-click the service in the SCM and select “Stop”.To uninstall the Windows Service type installutil /u bin\service.exe at the command prompt.

Note that if you stop the Windows Service and then run a client, an EndpointNotFoundException exception occurs when a client attempts to access the service.Make sure that your service is running before you make a call.

Exception – Cannot have two operations in the same contract with the same name, methods GetDataForTeacher and GetDataForStudent in type WcfSample.IStock violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute

When I got this exception while using same Name,was wondering what is the role of Name parameter,Methods are implemented with two different names & why is it throwing this exception for the below contract.

[ServiceContract]
public interface IStock
{

[OperationContract(Name ="GetData")]
string GetDataForTeacher(int value);
[OperationContract(Name ="GetData")]
string GetDataForStudent(int value);

}

First lesson learnt,do not use anything by simply copy pasting :)Understand the purpose & if it is required then only use it.

Now let me come to the point what does this Name property do?Name has to be specified when you need to override the element name in WSDL.ie to override the name of methods GetDataForTeacher & GetDataForStudent in wsdl.When you specify Name,now what has happend is it reads as two methods with name GetData.Ideally,Name should be specified only in cases where you have the same implementing method names(overloading scenario) as shown below.

[ServiceContract]
public interface IStock
{

[OperationContract(Name ="GetDataForTeacher")]
string GetData(int value);
[OperationContract(Name ="GetDataForStudent")]
string GetData(string value);

}