当前位置:编程学习 > C#/ASP.NET >>

WCF 新手求教

SERVICE 部分



using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceLibrary1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations
    [DataContract]
    [ServiceKnownType(typeof(CompositeType))]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceLibrary1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}




CLIENT部分:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
      

            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            ServiceReference1.CompositeType ct = new ServiceReference1.CompositeType();

            client.GetDataUsingDataContract(ct);

            Console.WriteLine(ct.StringValue);
        
        }
    }
}



为什么 ServiceReference1.CompositeType ct = new ServiceReference1.CompositeType();  
ct.BoolValue 不是 true ,是false?
ct.StringValue 不是 "Hello",是NULL? --------------------编程问答--------------------  client.GetDataUsingDataContract(ct);

为什么不是引用传递? --------------------编程问答-------------------- --------------------编程问答-------------------- bool boolValue = true;
string stringValue = "Hello ";

并没有在你的代理类里生成。非公开字段且不是DataMember属性,不会被序列化。 --------------------编程问答-------------------- 有兴趣看看这篇,说得很详细:

http://www.cnblogs.com/artech/archive/2007/03/10/669874.html --------------------编程问答--------------------
引用 3 楼 fangxinggood 的回复:
bool boolValue = true;
string stringValue = "Hello ";

并没有在你的代理类里生成。非公开字段且不是DataMember属性,不会被序列化。


明白了
那,

client.GetDataUsingDataContract(ct);

为什么不是引用传递?而是类似值传递? --------------------编程问答-------------------- --------------------编程问答-------------------- client.GetDataUsingDataContract(ct);

为什么不是引用传递?而是类似值传递?

==========

没明白什么意思。。。怎么看出来是值传递?
(想讨论引用传递还是值传递,最好换个示例)

ct.StringValue = "Hello";
ct.BoolValue = true;
var result = client.GetDataUsingDataContract(ct);



--------------------编程问答-------------------- CLIENT 调用:
ct.StringValue = "Hello";
ct.BoolValue = true;
var result = client.GetDataUsingDataContract(ct);

调用后:
result.StringValue = "HelloSuffix";
但是 还是
ct.StringValue = "Hello";
 
 CT不用OUT和REF,也应该是HelloSuffix 吧???


--------------------编程问答-------------------- 所以说你要想研究值类型还是引用类型的区别不要用这个做例子...
这个过程很复杂。。。

ct.StringValue = "Hello";
ct.BoolValue = true;
---------> 这里为止,ct是客户端实例
var result = client.GetDataUsingDataContract(ct); --> ct序列化到服务端

此处省略一千字...

服务端反序列化这个对象然后赋值,然后return,然后再序列化。
客户端拿到再反序列化,即在客户端重新生成个对象。
也就是说客户端的ct和服务端composite不是一个引用。
(本来也不是,分别在两边的内存里)

WCF建立这样一个机制,通过序列化和反序列化,让编程者使用起来好像没有
跨进程的差异,都像是在操作一个对象而已。


--------------------编程问答-------------------- 俺也是新手。。。赐教 --------------------编程问答--------------------
引用 9 楼 fangxinggood 的回复:
所以说你要想研究值类型还是引用类型的区别不要用这个做例子...
这个过程很复杂。。。

ct.StringValue = "Hello";
ct.BoolValue = true;
---------> 这里为止,ct是客户端实例
var result = client.GetDataUsingDataContract(ct); --> ct序列化到服务端

此处省略一千字...……


大概明了。。。同时也理解序列化和反序列化,看描述不太理解,被你这么一说,清晰很多。
谢谢大大 --------------------编程问答-------------------- 当然WCF不是序列化反序列化这么简单。我只是想告诉你客户端对象和服务端对象是两个不同的实例。

另外你可以把方法加上 ref 再试试。呵呵。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,