Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4113538
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:20:16+00:00 2026-05-20T22:20:16+00:00

I have a very simple WCF program where I have a simple self-host and

  • 0

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 [url]http://localhost:8000/WCFSampleService/HelloWCF[/url]. 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:20:17+00:00Added an answer on May 20, 2026 at 10:20 pm

    Would you not be better off just returning the string? Surely the string returned as a stream over an Http binding will be being Base64 encoded, which normally doubles the size of the data being returned!

    Maybe you could consider TcpBinding as an alternative.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple WCF service running that has a single method that
I have a very simple wcf server. When a client uses an operation contract,
I have a (very) simple WCF written in VB which I can build and
Problem is I have a very simple WCF REST service, which I wrote starting
I'm am trying to create a very simple WCF client application which will send
I have a WCF Web Service Framework 4 that exposes a very simple method
I have written a very simple WCF service, that worked fine (code below), then
I have a very simple (new to this) RESTful WCF service that uses a
I have a very simple table called Member , which consists of the following:
I have a very simple application which consists of an ASP.NET front end site,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.