Showing posts with label WCF service debugging. Show all posts
Showing posts with label WCF service debugging. Show all posts

Thursday, June 7, 2012

Remote server return an error from WCF service.

We get this error if there is any error between client and server which  means you will not have clue what exactly the issue is.

To figure out what exactly the issue is very simple.
You need to diagnose your WCF service service which will tell you where is the problem.

To enable diagnostic for you WCF service is



  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData= "c:\Logs\Traces.svclog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

Make sure you have a folder with name Logs under "C" drive or where you place and and have permission to create to files under that folder. 

Then run your application and and follow the process that gives you the error. After error close the Visual studio and open Traces.svclog. You will see error on the left hand side in red color and you will find the description on the right side. Then you can easily fix your issue with Remote Server returned an error.

The most common causes for this error is
1. Mismatch Binding
2. Mismatch of DataContract
3. Total data size exceeds the limit and many more but using this diagnostic tools makes your life easier to find out what exactly is the reason for your application to get this error.

Leave your question/suggestions on this if have any.

Cheers!
Vinod







Wednesday, June 6, 2012

WCF endpoint issue fixed

In silverlight when we you add refrenece to a WCF service it added a configuration file servicereference.config.

This file contains endpoint of the service and Binding information of the service. When you publish the website and put on server everytime you need to change the end point of service in servicerefernece.config file before publishing it. I have go a very simple solution for this issue.

Add this class as a service helper class

 public class ServiceProxy
    {
        private Uri _baseUri;
        private EndpointAddress _endpointAddress;
        private CustomBinding _binding;
        private T _client;

        public ServiceProxy(String serviceUri)
        {
            _baseUri = new Uri
                (Application.Current.Host.Source.ToString().Substring(0, Application.Current.Host.Source.ToString().IndexOf("ClientBin")));
            //("http://localhost/GacOil");
            _endpointAddress = new EndpointAddress(new Uri(_baseUri, serviceUri), null);

            HttpTransportBindingElement transport = new HttpTransportBindingElement();
            transport.MaxBufferSize = int.MaxValue;
            transport.MaxReceivedMessageSize = int.MaxValue;
            transport.TransferMode = TransferMode.Buffered;
         
            /*Please use the same binding as your service use here if BasicHttpBinding then use BasicHttpBinding only here*/
         
            _binding = new CustomBinding(
               new HttpCookieContainerBindingElement(),
                new BinaryMessageEncodingBindingElement(),
                 transport
            );

            _binding.OpenTimeout = _binding.ReceiveTimeout = _binding.SendTimeout = _binding.CloseTimeout = new TimeSpan(0, 5, 0);

            _client = (T)Activator.CreateInstance(typeof(T), new object[] { _binding, _endpointAddress });
        }

        public T Client
        {
            get
            {
                return _client;

            }
        }
    }


Now you are all set to use this wrapper class to call WCF service.

ServiceProxy mProxy = new ServiceProxy< TestServiceClient >("Services/TestService.svc");

Where TestServiceClient is the Service client class and TestService.svc is under Services folder of your root directory. No you don't have to worry about changing end point everytime you pubish it on server.

Let me know if you any questions/doubts regarding this.

Regards
Vinod