Search code examples
c#wcfchannelfactory

Wcf ChannelFactory


I have a question about WCF and Channel Factory usage.

On Host :

[ServiceContract]
public interface IGetMessage 
{
    [OperationContract]
    string ShowMessage(Sample p, string Username, string Password);
}

[DataContract]
public class Sample
{
    [DataMember]
    public string Name { get; set; }
}

public string ShowMessage(Sample p, string Username, string Password)
{
    return p.Name.ToString() + " - " + "Correct"; //Error line
}

On Client :

[ServiceContract()]
public interface IGetMessage
{
    [OperationContract()]
    string ShowMessage(Sample p, string Username, string Password);
}

[DataContract()]
public class Sample
{
    [DataMember]
    public string Name { get; set; }

}

private void button1_Click(object sender, EventArgs e)
{
    Sample p1 = new Sample();
    p1.Name = "ALEX";

    BasicHttpBinding myBinding = new BasicHttpBinding();

    EndpointAddress myEndpoint = new EndpointAddress("http://MYURL/GetMessage.svc");

    using (var myChannelFactory = new ChannelFactory<IGetMessage>(myBinding, myEndpoint))
    {
        IGetMessage client = null;

        try
        {
            client = myChannelFactory.CreateChannel();

            MessageBox.Show(client.ShowMessage(p1, "abc","123"));

            ((ICommunicationObject)client).Close();
            myChannelFactory.Close();
        }
        catch
        {
            (client as ICommunicationObject)?.Abort();
        }
    }

}

If I add on client as Service Reference, it works perfectly. I can get "ALEX - Correct" message.

When I test on WcfTestClient.exe, it works perfectly.

But, I have a problem when using on Winform with above codes. When I check on WcfServer Trace Log and Message Log;

System.NullReferenceException - Object reference not set to an instance of an object. Line number:22

Line number 22: p.Name.ToString() on the Host's GetMessage.cvs.cs file.

I think, there is no problem on host. Problem is Client side.

I'd like to ask you how I made a mistake on the client side?

Regards.


Solution

  • As mentioned in the comments, to solve the problem you only to do is adding the namespace property to ServiceContract and DataContract. be sure that the namespace is identical between the client and the server. the reason why we should do is an error occurred during serialization and deserialization, we must ensure that the service contract and data contract has a consistent namespace between the server and client.
    Here is my example(Add this feature on both the server and client.)

        [ServiceContract(Namespace ="http://mydomain")]
        public interface IGetMessage
        {
            [OperationContract]
            string ShowMessage(Sample p, string Username, string Password);
        }
    
        [DataContract(Namespace = "http://mydomain")]
        public class Sample
        {
            [DataMember]
            public string Name { get; set; }
    }
    

    Result.
    enter image description here
    Feel free to let me know if there is anything I can help with.