views:

660

answers:

2

I'm trying to get hold of an object from another application using WCF. With built in classes it works fine but I run into probems when trying to return a custom interface type from the WCF operation.

Whether I include the interface in both applications separately, or specif it as a shared assembly, I get the same result: a CommunicationException with the message "There was an error reading from the pipe: Unrecognized error 109".

The interface looks like this:

[ServiceContract]
public interface IBase {
    int IntTest {
        [OperationContract]
        get;
    }
    String StringTest {
        [OperationContract]
        get;
    }
    IOther OtherTest {
        [OperationContract]
        get;
    }
}

[ServiceContract]
public interface IOther {
    String StringTest {
        [OperationContract]
        get;
    }
}

My server looks like this:

public partial class MainWindow : Window {
    private Base fb;
    private ServiceHost host;

    public MainWindow() {
        InitializeComponent();
        fb = new Base();
        host = new ServiceHost(fb, new Uri[] { new Uri("net.pipe://localhost") });
        host.AddServiceEndpoint(typeof(IBase), new NetNamedPipeBinding(),
            "PipeReverse");
        host.Open();
    }

    private void Window_Closing(object sender, CancelEventArgs e) {
        host.Close();
    }
}

And here is my implementation of the interface:

[Serializable]
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class Base : MarshalByRefObject, IBase {
    public int IntTest {
        get { return 4; }
    }

    public string StringTest {
        get { return "A string from Base"; }
    }

    public IOther OtherTest {
        get { return new Other(); }
    }
}

[Serializable]
[DataContract]
public class Other : MarshalByRefObject, IOther {
    [DataMember]
    public string StringTest {
        get { return "A string from Other"; }
    }

}

The client looks like this:

public partial class Form1 : Form {

    IBase obj;

    public Form1() {
        InitializeComponent();
        ChannelFactory<IBase> pipeFactory = new ChannelFactory<IBase>(
            new NetNamedPipeBinding(), new EndpointAddress(
            "net.pipe://localhost/PipeReverse"));

        obj = pipeFactory.CreateChannel();
    }


    private void button2_Click(object sender, EventArgs e) {

        Console.WriteLine("Returns: " + obj.StringTest + " " + 
            obj.StringTest.Length);
        Console.WriteLine("Returns: " + obj.IntTest);
        Console.WriteLine(obj.OtherTest);

    }
}

Everything works like a charm except this line:

Console.WriteLine(obj.OtherTest);

It give me a CommunicationException with the message "There was an error reading from the pipe: Unrecognized error 109". As far as I can tell that is a broken pipe due to a faulted state but I can't figure out why, or more importantly how to fix it. Any ideas?

I have no config file as everthing is done in the code above so I don't know how to turn on tracing, otherwise I would have included that too.

A: 

This is typically a serialization error. Look to the [KnownType] attribute. The easiest way to test this out is to invoke the DataContractSerializer directly. You can use its WriteObject and ReadObject methods to get the true serialization errors. You can also inspect the stream(FileStream usually) to make sure you type serializes correctly.

Adam Fyles
+2  A: 

The returned property OtherTest needs to be a concrete type and not an interface, otherwise the serialization will not work.

Jose Basilio
That's it! Thanks. In effect this means that I'll have to have the business objects in a shared assembly. I was hoping for being able to just duplicate the interface but I suppose that'll have to do. Thanks again for helping out.
Farawin