Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

How to solve CommunicationException.....? RSS

12 replies

Last post Jan 07, 2009 08:56 AM by avtar

(1)
  • avtar

    avtar

    Participant

    777 Points

    208 Posts

    How to solve CommunicationException.....?

    Jan 05, 2009 02:43 PM | LINK

    Hi,

    I am using WCF with silverlight and my silverlight application has web reference to my WCF service.

    My server function is this....
     public object[]  GetComponentData(string queryString, bool authflag)
        {
         
            DataTable dt = ComponentHelper.GetComponentData(queryString, ref authflag);
            dt.TableName = "IterationResult";
            Object[] rows = CreateItemsSource(dt);
            return rows;

        }

    this function will return 2 object.

     but once control pass from this function, i am getting CommuncationException with this message

    "The remote server returned an error: NotFound"
    {System.Net.WebException: The remote server returned an error: NotFound ---> System.Net.WebException: The remote server returned an error: NotFound
       at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
       at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
       at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
       --- End of inner exception stack trace ---
       at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
       at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
       at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)}

     inside this function at client side...

     public System.Collections.ObjectModel.ObservableCollection<object> EndGetComponentData(System.IAsyncResult result) {
                    object[] _args = new object[0];
                    System.Collections.ObjectModel.ObservableCollection<object> _result = ((System.Collections.ObjectModel.ObservableCollection<object>)(base.EndInvoke("GetComponentData", _args, result)));
                    return _result;
                }

    second line of this function throw this error...

    How to fix this problem.

    Hope you understand my problem.

     

    Thanks and Regards

     Avtar.

     

     

    Please click on "Mark As Answer", if this answered your query partially or fully.
  • Ken Tucker

    Ken Tucker

    All-Star

    23250 Points

    3534 Posts

    Re: How to solve CommunicationException.....

    Jan 05, 2009 02:54 PM | LINK

     Silverlight does not support datatables.  You need to return a list of a class instead of a datatable

  • ccoombs

    ccoombs

    Star

    9658 Points

    1355 Posts

    Re: How to solve CommunicationException.....

    Jan 05, 2009 02:56 PM | LINK

     if you're trying to return a datatable, you can't.  return a list of business objects instead.

    if you're just using the dt to create a custom object, you might need to apply the datamember attribute.  look at the service through your browser at the localhost address and invoke it.  you should see a more descriptive error message.

  • avtar

    avtar

    Participant

    777 Points

    208 Posts

    Re: How to solve CommunicationException.....

    Jan 06, 2009 04:23 AM | LINK

     Hello ken and ccombs,

    Thanks for your quick reply.

     I know that we cann't use DataTable in silverligt, i.e my CreateItemsSource(dt) method return array of object each of them represent row of the datatable.

    Here is more details ......

    GetComponentData() method is a WCF method.


    CreateItemSource(DataTable dt) // method to get array of object.

    this method accept datatable as a agrgument and retrun array of object respectively each row.

    So i am not returning DataTable to silverlight client, instead i am return a custom object i.e object[], now this object[] can bound to ItemSource of Silverlight DataGrid.

    but before bind take place i am getting exception.

    {System.Net.WebException: The remote server returned an error: NotFound ---> System.Net.WebException: The remote server returned an error: NotFound


    In this function...

    public System.Collections.ObjectModel.ObservableCollection<object> EndGetComponentData(System.IAsyncResult result) {
                    object[] _args = new object[0];
                    System.Collections.ObjectModel.ObservableCollection<object> _result = ((System.Collections.ObjectModel.ObservableCollection<object>)(base.EndInvoke("GetComponentData", _args, result)));
                    return _result;
                }


    hope it clerify my problem.

    Thanks lot

    Avtar.

    Please click on "Mark As Answer", if this answered your query partially or fully.
  • spiderman110

    spiderman110

    Member

    226 Points

    290 Posts

    Re: Re: How to solve CommunicationException.....

    Jan 06, 2009 08:11 AM | LINK

    I have met this Exception.But not alwalys.In my case I use web service.

    The reason sometimes is timeout, sometimes I still dont know.

  • avtar

    avtar

    Participant

    777 Points

    208 Posts

    Re: Re: How to solve CommunicationException.....

    Jan 06, 2009 08:36 AM | LINK

     hi, Thanks for your reply,

     Time out may be problem, but while debugging some time i get time out erro ....i.e different from my current problem, and i am able to check my data at server side which is apsolutely  right, but once server send data back to silverlight client this error arise.

     

    Avtar.

    Please click on "Mark As Answer", if this answered your query partially or fully.
  • Ken Tucker

    Ken Tucker

    All-Star

    23250 Points

    3534 Posts

    Re: How to solve CommunicationException.....

    Jan 06, 2009 10:33 AM | LINK

    avtar

     Hello ken and ccombs,

    Thanks for your quick reply.

     I know that we cann't use DataTable in silverligt, i.e my CreateItemsSource(dt) method return array of object each of them represent row of the datatable.

     

     

    Still siliverlight does not understand datarows you need to convert them to a class. 

  • avtar

    avtar

    Participant

    777 Points

    208 Posts

    Re: How to solve CommunicationException.....

    Jan 06, 2009 10:44 AM | LINK

    ken tucker

    avtar

     Hello ken and ccombs,

    Thanks for your quick reply.

     I know that we cann't use DataTable in silverligt, i.e my CreateItemsSource(dt) method return array of object each of them represent row of the datatable.

     

     

    Still siliverlight does not understand datarows you need to convert them to a class. 

     

    Hi, 

    I did that,  i  am creating a class _rowClass at run time each object of this class represent row of my DataTable and my wcf method return array of Object[].

    here is my WCF code...return array of object...

     

    public class Service : IService
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    
        public object[]  GetComponentData(string queryString, bool authflag)
        {
            try
            {
                DataTable dt = ComponentHelper.GetComponentData(queryString, ref authflag);
                dt.TableName = "IterationResult";
                Object[] rows = CreateItemsSource(dt);
                return rows;
            }
            catch (Exception ex)
            {
                throw ex;
            }       
        }
    
        private object[] CreateItemsSource(DataTable dt)
        {
            Type _RowClass;
            AssemblyName an = new AssemblyName("Result" + this.GetHashCode());
            System.Reflection.Emit.AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
            TypeBuilder tb = moduleBuilder.DefineType("RowType" + this.GetHashCode(), TypeAttributes.Public | 
                                                                                      TypeAttributes.Class | 
                                                                                      TypeAttributes.AutoClass | 
                                                                                      TypeAttributes.AnsiClass | 
                                                                                      TypeAttributes.BeforeFieldInit | 
                                                                                      TypeAttributes.AutoLayout
                                                                                      , typeof(object));
    
            ConstructorBuilder constructor = tb.DefineDefaultConstructor( MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
            IEnumerator columns = dt.Columns.GetEnumerator();
            
            while (columns.MoveNext())
            {
                DataColumn info = (DataColumn)columns.Current; 
                
                
                 FieldBuilder field=  tb.DefineField(info.ColumnName.Replace(' ', '_'), info.DataType, FieldAttributes.Private);
                 PropertyBuilder propBldr = tb.DefineProperty(info.ColumnName.Replace(' ', '_'), System.Reflection.PropertyAttributes.HasDefault,info.DataType, new Type[]{info.DataType});
                // The property set and property get methods require a special
                // set of attributes.
                MethodAttributes getSetAttr = MethodAttributes.Public;
                // Define the "get" accessor method for CustomerName.
                MethodBuilder getPropMthdBldr =
                    tb.DefineMethod("get_"+info.ColumnName,
                                               getSetAttr,
                                               info.DataType,
                                                new Type[] { });
                ILGenerator custNameGetIL = getPropMthdBldr.GetILGenerator();
    
                custNameGetIL.Emit(OpCodes.Ldarg_0);
                custNameGetIL.Emit(OpCodes.Ldfld, field);
                custNameGetIL.Emit(OpCodes.Ret);
    
                // Define the "set" accessor method for CustomerName.
                MethodBuilder setPropMthdBldr =
                    tb.DefineMethod("set_"+info.ColumnName,
                                               getSetAttr,
                                               null,
                                               new Type[] { info.DataType });
                ILGenerator custNameSetIL = setPropMthdBldr.GetILGenerator();
    
                custNameSetIL.Emit(OpCodes.Ldarg_0);
                custNameSetIL.Emit(OpCodes.Ldarg_1);
                custNameSetIL.Emit(OpCodes.Stfld, field);
                custNameSetIL.Emit(OpCodes.Ret);
    
    
    
                // Last, we must map the two methods created above to our PropertyBuilder to 
                // their corresponding behaviors, "get" and "set" respectively. 
                propBldr.SetGetMethod(getPropMthdBldr);
                propBldr.SetSetMethod(setPropMthdBldr);
    
    
            }
            Type[] ctorParams = new Type[] {};
            ConstructorInfo classCtorInfo = typeof(DataContractAttribute).GetConstructor(ctorParams);
    
            CustomAttributeBuilder myCABuilder = new CustomAttributeBuilder(
                           classCtorInfo,
                           new object[]{});
    
            tb.SetCustomAttribute(myCABuilder);
    
            _RowClass = tb.CreateType();
            object[] itemsSource = new object[dt.Rows.Count];
            for (int rowIdx = 0; rowIdx < dt.Rows.Count; rowIdx++)
            {
                object row = Activator.CreateInstance(_RowClass);
                columns = dt.Columns.GetEnumerator();
                int i = 0;
                while (columns.MoveNext())
                {
                    DataColumn info = (DataColumn)columns.Current;
    
                    PropertyInfo field = _RowClass.GetProperty(info.ColumnName.Replace(' ', '_'));
                    object val = dt.Rows[rowIdx][i++];
                    //IFormatProvider format =
                    //            new CultureInfo("en-GB", true);    
                    field.SetValue(row, Convert.ChangeType(val, field.PropertyType),null);
                }
    
                itemsSource[rowIdx] = row;
            }
            return itemsSource;
        }
       
    Please click on "Mark As Answer", if this answered your query partially or fully.
  • struggle-luan

    struggle-luan

    Member

    592 Points

    104 Posts

    Re: How to solve CommunicationException.....?

    Jan 06, 2009 11:11 AM | LINK

    ehh

    Sometimes the WCF throws an Unexpected exception, if your WCF service is just on your Local base, it will be easier for you to make a debug.

    1) Try to add an breakpoint to the beginning of GetComponentData function. If the request has brought your App into the Breakpoint, then your Connection is alright. Otherwise, you should check your Binding.

    2) If step 1 shows no-problem, try to add Breakpoint to "return something" in remote function. And use VS to see what is happenning inside the Object list, and check that your data has picked out in a right way. If List contains nothing, the Problem may be caused by your Query. 

    3) IF STEP 2 STIILL SHOWS NOTHING... Please focus on the Client Code. If your serivce has been modified recently, try to Update your Proxy which is in your Client side. And also check your calling.

     

    Not found, sometimes = Timeout, sometimes = Remote- File-Not Found

    sometimes = Remote Serivce Connection Error, sometimes = Remote-bad request -- null param input or someother..

     

    This is a little trap of developing SL_WCF app, the hardest part is not your error, but your Debuging

     

     

    ................And pls provide more infomation about your Client Call, and what you've found in those steps.............

     

     

    ehh... this is what I called "POOR ENGLISH"..
  • avtar

    avtar

    Participant

    777 Points

    208 Posts

    Re: How to solve CommunicationException.....?

    Jan 06, 2009 11:36 AM | LINK

    struggle-luan

    ehh

    Sometimes the WCF throws an Unexpected exception, if your WCF service is just on your Local base, it will be easier for you to make a debug.

    1) Try to add an breakpoint to the beginning of GetComponentData function. If the request has brought your App into the Breakpoint, then your Connection is alright. Otherwise, you should check your Binding.

    2) If step 1 shows no-problem, try to add Breakpoint to "return something" in remote function. And use VS to see what is happenning inside the Object list, and check that your data has picked out in a right way. If List contains nothing, the Problem may be caused by your Query. 

    3) IF STEP 2 STIILL SHOWS NOTHING... Please focus on the Client Code. If your serivce has been modified recently, try to Update your Proxy which is in your Client side. And also check your calling.

     

    Not found, sometimes = Timeout, sometimes = Remote- File-Not Found

    sometimes = Remote Serivce Connection Error, sometimes = Remote-bad request -- null param input or someother..

     

     

     

    Hi bro,

     Thanks for your reply, i like your way of asking question.....steps...[:)]  here is an answer to your questions....

     step 1 )  i am able to debug.

    step 2 ) I have put try catch blog on my wcf method and able to debug bug no expception has take place, but when control reaches at return rows;  line and then i press "F10" an error has come in Reference.cs file at client side.

    step3 ) I have updated my web reference at silverlight client.

     this function is causing the problem at client side

     

     public System.Collections.ObjectModel.ObservableCollection<object> EndGetComponentData(System.IAsyncResult result) { 
                    object[] _args = new object[0]; 
                    System.Collections.ObjectModel.ObservableCollection<object> _result = ((System.Collections.ObjectModel.ObservableCollection<object>)(base.EndInvoke("GetComponentData", _args, result))); 
                    return _result; 
                }  

     second line of this method causing the problem.

    hope it help.

    Avtar.

     
    Please click on "Mark As Answer", if this answered your query partially or fully.