Archive for the ‘WCF’ Category

Today I was having a look at one of our WCF Restful projects.To start with the code understanding,I decided to host that in my local host and then take a look.To my surprise,I was unable get a svc url as we usually get, for example: .Digging further I realized, being a wcf restful service it’s not ideal having an extension this way and it has been achieved through System.Web.Routing integration .Routing allows to host a WCF restful service that responds to URIs without an extension .Cool right?It’s a new feature with .Net 4.0.I am avoiding a rewriting of an example code here as msdn has got a beautiful simple explanation over here.Take a look!


My previous post,I have given a small tip on how to get the page url in code behind from,this one is to get the same url from a WCF service.

public string IM_Ping()
System.Uri uri = System.ServiceModel.OperationContext.Current.IncomingMessageHeaders.To;
return uri.AbsoluteUri;



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.

Exception – The HTTP request is unauthorized with client authentication scheme ‘Anonymous’. The authentication header received from the server was ‘Basic realm=’

This indicates that service authentication is configured as Basic & client authentication should be changed accordingly. Below web.config at client side can help you get rid of this exception

        <binding name="ExternalBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic"/>

<endpoint name="IServices" address="" binding="webHttpBinding" bindingConfiguration="ExternalBinding" behaviorConfiguration="RESTBehavior" contract="IVendor"></endpoint>
    <behavior name="RESTBehavior">
    <webHttp helpEnabled="true"/>


Make sure that from client code you supply the necessary credentials through proxy.

VendorProxy client = new VendorProxy();
client.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["ServiceUsername"].ToString();
client.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["ServicePassword"].ToString();

A sad afternoon & was feeling quite helpless few hours back.Suddenly like a piece of choclate the word WebFaultException came in &  I decided to forget everything & write about this 🙂 Well stories apart,lets get into the point. Consider having a situation,where you search for a particular user by hitting a wcf service.If the user is not found(say your database server is down),it will straight away throw an exception to client saying HTTP Error 404 Not found.I swear, the end user will not understand the actual meaning behind this.
Such cases,use the WebFaultException class to set the HTTP status code to 404 (Not Found) and give indication of the error description in the message body. 

throw new WebFaultException<string>(
            string.Format("There is no user available with the provided userName '{0}'.", userName),

Hope this helps you 🙂 This is my basic understanding about WebFaultException & I would appreciate if any of you can share some more information you have about this.

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>.

    public interface IVendorServices

        [WebGet(UriTemplate = "v1/Ping")]
        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();


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