Search code examples
c#.netwcfwcf-client

WCF OperationContract property forgets value


recently have been successful getting my IIS hosted WCF service to work with basic authentication.

Since successfully implementing that. I have noticed that property values are not remembered.

Here is some code:

[ServiceContract]
public interface IEcho
{
    string Message { [OperationContract]get; [OperationContract]set; }
    [OperationContract]
    string SendEcho();
}

public class EchoProxy : IEcho
{
    public string Message { get; set; }
    public string SendEcho()
    {
        return string.Concat("You said: ", Message);
    }
}
public class EchoService : System.ServiceModel.ClientBase<IEcho>, IEcho
{
    //-- ..... CONSTRUCTORS OMITTED ....

    public string Message
    {
        get { return base.Channel.Message; }
        set { base.Channel.Message = value; }
    }
    public string SendEcho()
    {
        return base.Channel.SendEcho();
    }
}

Here is the console and the result:

EchoService client = new EchoService("SecureEndpoint");
client.ClientCredentials.UserName.UserName = "test";
client.ClientCredentials.UserName.Password = "P@ssword1";
client.Message = "Hello World";
Console.WriteLine(client.SendEcho());

Expected Result: You said: Hello World
Actual Result: You said:

I have Uploaded the sandbox project to my skydrive. I have included a SETUP.txt in the API project.

Click here to download.
How can I get properties to work?

thank you


Solution

  • I have never seen WCF contract used with a property to transfer data. i.e. the Message property. AFAIK its just not possible.

    My recommendation would be to keep the concerns that are part of the contract separate, i.e. Operation and Data.

    [ServiceContract]
    public interface IEcho
    {
        [OperationContract]
        string SendEcho(string Message);
    }
    

    Or

    [ServiceContract]
    public interface IEcho
    {
        [OperationContract]
        string SendEcho(Message message);
    }
    
    [DataContract]
    public class Message
    {
        [DataMember]
        public string Message {get; set;}
    }
    

    At some later point you may wish to change the Message Object.

    [DataContract]
    public class MessageV2 : Message
    {
        [DataMember]
        public DateTime Sent {get; set;}
    }
    

    While this changes the contract, changes like this can be backwardly compatible if managed carefully.