sachintha81 17 Junior Poster in Training

I have a very simple WCF program where I have a simple self-host and a client running on the same computer. There is a single method which returns a System.IO.Stream, which is actually a serialized form of a simple string. (This could be any number of data types, but for the time being let's take it as a string).

Following is the code I use if you want to take a look at. SerializeData() and DeserializeData() are methods used to do just that, and works fine.

Host Service:

namespace HelloWCF1
{
    [ServiceContract(Namespace = "http://My.WCF.Samples")]
    public interface IService1
    {
        [OperationContract]
        Stream GetString();
    }

    public class Service1 : IService1
    {
        //Simple String
        public Stream GetString()
        {
            string str = "ABC";

            Stream ms = new MemoryStream();
            SerializeData<string>(str, ms);

            return ms;
        }

        /// <summary>
        /// Serialize an object of the type T to a Stream
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objectToSerialize"></param>
        /// <param name="str"></param>
        public void SerializeData<T>(T objectToSerialize, Stream str)
        {
            BinaryFormatter bf = new BinaryFormatter();

            try
            {
                bf.Serialize(str, objectToSerialize);
                str.Position = 0;
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Deserialize a Stream
        /// </summary>
        /// <param name="dataToDeserialize"></param>
        /// <returns></returns>
        public object DeserializeData(Stream dataToDeserialize)
        {
            BinaryFormatter bf = new BinaryFormatter();
            object ret = null;

            try
            {
                ret = bf.Deserialize(dataToDeserialize);
            }
            catch (Exception)
            {
            }

            return ret;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddr = new Uri("http://localhost:8000/WCFSampleService");

            //ServiceHost is created by defining Service Type and Base Address
            using (ServiceHost svcHost = new ServiceHost(typeof(Service1), baseAddr))
            {

                //Trace message for service start
                Console.WriteLine("Service Starting...");

                //Adding an end point
                svcHost.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "HelloWCF");

                //Open service host
                svcHost.Open();

                Console.WriteLine("Press [Enter] to terminate.");
                Console.ReadLine();

                //Close service host
                svcHost.Close();
            }
        }
    }
}

Client Program:

namespace Client
{
    [ServiceContract(Namespace = "http://My.WCF.Samples")]
    public interface IService1
    {
        [OperationContract]
        Stream GetString();
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private IService1 proxy = null;
        private Stream memStr = new MemoryStream();
        
        private void button1_Click(object sender, EventArgs e)
        {
            //Create end point
            EndpointAddress epAddr = new EndpointAddress("http://localhost:8000/WCFSampleService/HelloWCF");

            //Create proxy
            proxy = ChannelFactory<IService1>.CreateChannel(new BasicHttpBinding(), epAddr);

            //WCF Service Method is called to aquire the stream
            try
            {
                memStr = proxy.GetString();
                string str = (string)DeserializeData(memStr);
                MessageBox.Show(str);
            }
            catch (CommunicationException commEx)
            {
                MessageBox.Show("Service Call Failed:" + commEx.Message);
            }
        }       

        /// <summary>
        /// Serialize an object of the type T to a Stream
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objectToSerialize"></param>
        /// <param name="str"></param>
        public static void SerializeData<T>(T objectToSerialize, Stream str)
        {
            BinaryFormatter bf = new BinaryFormatter();

            try
            {
                bf.Serialize(str, objectToSerialize);
                str.Position = 0;
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Deserialize a Stream
        /// </summary>
        /// <param name="dataToDeserialize"></param>
        /// <returns></returns>
        public static object DeserializeData(Stream dataToDeserialize)
        {
            BinaryFormatter bf = new BinaryFormatter();
            object ret = null;

            try
            {
                ret = bf.Deserialize(dataToDeserialize);
            }
            catch (Exception)
            {
            }

            return ret;
        }
    }
}

Now, this works fine. It serializes a string into a Stream and sends using HTTP quite fine. However, I have a need to convert of cast this Stream into an object before sending. Simply put, I need the GetString() method to look like this:

public object GetString()
{
            string str = "ABC";

            Stream ms = new MemoryStream();

            SerializeData<string>(str, ms);

            return (object)ms;
}

However, when I do this, inside the Button1_Click event, at the line memStr = proxy.GetString(); I get a CommunicationException. I work in a Japanese OS, so the exception message I get is in Japanese so I'll try to translate into English as best as I can. Forgive me if it is not very clear.

Error occured in the reception of HTTP response concerning http://localhost:8000/WCFSampleService/HelloWCF. Cause of the error could be Service End Point binding is not using an HTTP protocol. Or as a different cause, it is also possible that HTTP request context is suspended according to the server (as in the case of server being shut down). Please refer the server log for more details.

What exactly is the problem and how can I get the program to work the way I want? It says to look at the log files but where can I find them?

Thanks in advance!