WCF OperationContract的财产忘记值财产、WCF、OperationContract

2023-09-06 14:22:06 作者:捧猫少女

最近一直successful让我的IIS承载WCF服务与基本身份验证工作 。

由于成功地实施了。我注意到,属性值不会被记录。

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

下面是一些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:

我已经上传了沙盒项目,我的SkyDrive。我已经包含在API项目SETUP.TXT。

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?

谢谢

推荐答案

我从来没有见过一个属性用于传输数据的WCF的合同。即Message属性。 AFAIK它只是不可能的。

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);
}

[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.

 
精彩推荐
图片推荐