WCF中的消息合同 [英] Message contract in WCF

查看:86
本文介绍了WCF中的消息合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个消息合同类,如下所示

 [MessageContract] 
public < span class =code-keyword> class 凭证
{
private string key = String .Empty;
private string val = String .Empty;

[MessageBodyMember(
Name = test
Namespace = http://www.examples.com
)]
public string
{
获取 {返回键; }
set { this .key = value ; }
}

[MessageHeader(
Name = testval
Namespace = http://www.examples.com
)]
public string Val
{
get { return val; }
set { this .val = value ; }
}
}



添加了如下服务合同

 [ServiceContract ] 
public interface IService1
{

[ OperationContract]
Hashtable GetHeader();

Credential obj
{
[OperationContract]
get ;
[OperationContract]
set ;
}
// TODO:在此处添加服务操作
}



现在在WCF服务中我正在实现该属性

  public   class  Service1:IService1 
{
private 凭证_obj;
public 凭据obj
{
获取 { return _obj; }
set {_ obj = value ; }
}

public Hashtable GetHeader()
{
Hashtable ht = new Hashtable();
foreach (MessageHeaderInfo h in OperationContext.Current.IncomingMessageHeaders)
{
ht.Add(h.Name,h.ToString());
}
return ht;
}
}



现在在客户端我试图设置如下的消息合约对象。

< pre lang =c#> localhost.Service1 obj = new localhost.Service1();
localhost.Credential objCredential = new localhost.Credential();
objCredential.test = test;
obj.set_obj(objCredential);
obj.GetHeader();



但是肥皂标题看起来似乎没有我赋予objCredential.test的值。为什么会这样?

解决方案

嗨Buddy,

您使用属性作为操作合同,这就是为什么r不能在soap中获取值。

创建属性作为操作合同是不可取的。

使用属性作为Datamember,如下面的链接所示

http://stackoverflow.com/questions/556775/wcf-datamember-attribute-on-property-vs-member [ ^ ]



请参考以下链接中的wcf教程(注意:在Internet Explorer中打开链接)

http://wcftutorial.net/Data-Contract.aspx [ ^ ]


I added a message contract class like below

[MessageContract]
    public class Credential
    {
        private string key = String.Empty;
        private string val = String.Empty;

        [MessageBodyMember(
          Name = "test",
          Namespace = "http://www.examples.com"
        )]
        public string Key
        {
            get { return key; }
            set { this.key = value; }
        }

        [MessageHeader(
          Name = "testval",
          Namespace = "http://www.examples.com"
        )]
        public string Val
        {
            get { return val; }
            set { this.val = value; }
        }
    }


Added a Service contract like below

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        Hashtable GetHeader();

        Credential obj
        {
            [OperationContract]
            get;
            [OperationContract]
            set;
        }
        // TODO: Add your service operations here
    }


Now in the WCF service i am implementing the property

public class Service1 : IService1
    {
        private Credential _obj;
        public Credential obj
        {
            get { return _obj; }
            set { _obj = value; }
        }

        public Hashtable GetHeader()
        {
            Hashtable ht = new Hashtable();
            foreach (MessageHeaderInfo h in OperationContext.Current.IncomingMessageHeaders)
            {
                ht.Add(h.Name, h.ToString());
            }
            return ht;
        }
    }


Now in the client i am trying to set the message contract object like below.

localhost.Service1 obj = new localhost.Service1();
            localhost.Credential objCredential = new localhost.Credential();
            objCredential.test = "test";
            obj.set_obj(objCredential);
            obj.GetHeader();


But the soap header never seems to have the value i assigned to " objCredential.test".Why is it so?

解决方案

Hi Buddy,
You r using property as operation contract that''s why do r not getting values in soap.
Creating property as operation contract is not advicible.
Use property as Datamember as explained in below link
http://stackoverflow.com/questions/556775/wcf-datamember-attribute-on-property-vs-member[^]

Please refer wcf tutorial on below link (Note: Open link in Internet Explorer)
http://wcftutorial.net/Data-Contract.aspx[^]


这篇关于WCF中的消息合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆