简体   繁体   English

WCF OperationContract属性忘记了价值

[英]WCF OperationContract property forgets value

recently have been successful getting my IIS hosted WCF service to work with basic authentication . 最近,我成功地使我的IIS托管的WCF服务与基本身份验证一起使用

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 预期结果: You said: Hello World
Actual Result: You said: 实际结果: You said:

I have Uploaded the sandbox project to my skydrive. 我已将沙盒项目上传到我的skydrive。 I have included a SETUP.txt in the API project. 我在API项目中包含了SETUP.txt。

Click here to download. 点击这里下载。
How can I get properties to work? 我如何才能使用物业?

thank you 谢谢

I have never seen WCF contract used with a property to transfer data. 我从未见过WCF合约与属性一起使用来传输数据。 ie the Message property. 即消息属性。 AFAIK its just not possible. AFAIK只是不可能。

My recommendation would be to keep the concerns that are part of the contract separate, ie 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. 在更改合同的同时,如果仔细管理,这样的更改可以向后兼容。

To understand what's happening, you need to know how the lifetime of the service object you're connecting to is configured. 要了解发生了什么,您需要知道如何配置要连接的服务对象的生存期。 A good starting point is the MSDN article on Sessions, Instancing, and Concurrency . 一个很好的起点是有关Sessions,Instancing和Concurrency的MSDN文章

For example, with InstanceContextMode.PerCall, a new service object will be created for each call, so no properties of the service object will be remembered between calls. 例如,使用InstanceContextMode.PerCall,将为每个调用创建一个新的服务对象,因此在两次调用之间不会记住该服务对象的属性。

At the other end of the scale, InstanceContextMode.Single means a single instance handles all client requests for the lifetime of the application. 另一方面,InstanceContextMode.Single表示单个实例处理应用程序生命周期内的所有客户端请求。 In this case properties set by one client will be visible to all clients, not usually desirable. 在这种情况下,一个客户端设置的属性对所有客户端都是可见的,通常是不希望的。

In general, I would recommend using a stateless service object. 通常,我建议使用无状态服务对象。 But if you want a stateful service object (eg one with properties), you should use InstanceContextMode.PerSession, and (important) use a binding that supports sessions. 但是,如果您想要一个有状态的服务对象(例如带有属性的服务对象),则应使用InstanceContextMode.PerSession,并且(重要的)使用支持会话的绑定。

While I agree with @JTew that you shouldn't generally expose operations as properties, you will have the same problem if you try to use an object that stores state between calls in another way (such as a private field). 我同意@JTew的观点,即通常不应该将操作公开为属性,但如果尝试使用以其他方式(例如私有字段)在两次调用之间存储状态的对象,则会遇到相同的问题。 Ie the following would have exactly the same problem: 即以下内容将具有完全相同的问题:

[ServiceContract]
public interface IEcho
{
    [OperationContract]
    void SetMessage(string message);

    [OperationContract]
    string GetMessage();

    ... etc ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM