none
有两个问题请教一下 RRS feed

  • 问题

  • #1 wcf 客户端用代理方式的可以编程方法改 maxItemsInObjectGraph吗?

    MSDN上有个例子,是用channelfactory的,不知道用 add service refernece的代理改怎样用?

    http://msdn.microsoft.com/en-us/library/ms732038.aspx

    ChannelFactory<IDataService> factory = new ChannelFactory<IDataService>(binding, address); foreach (OperationDescription op in factory.Endpoint.Contract.Operations) { DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior; if (dataContractBehavior != null) { dataContractBehavior.MaxItemsInObjectGraph = 100000; } } IDataService client = factory.CreateChannel();

    PS:我不能通过修改配置文件达到修改那个值,因为我现在要做成一个libery application,让其它程序调用,通过调试发现这种程序都不读配置文件

    #2 我尝试过把我的WCF客户程序改为用channelfactory<T>,但是不知为什么调用远程方法后返回值为空

    我的做法是把服务端的接口文件(包含服务端接口和数据契约,不包含实现方法)拷到客户端,结果返回值为空

    代码是

    客户端

      EndpointAddress objAddress = new EndpointAddress(wcfaddr + "/Service1.svc");

                  WSHttpBinding wshttpbind = new WSHttpBinding();
               
                  wshttpbind.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;

                  wshttpbind.ReliableSession.Enabled = false;
                  wshttpbind.TransactionFlow = false;
                  wshttpbind.MaxBufferPoolSize = 524288000;
                  wshttpbind.MaxReceivedMessageSize = 65536000;
                  wshttpbind.Security.Mode = SecurityMode.None;
                  wshttpbind.Security.Message.ClientCredentialType = MessageCredentialType.None;
                  wshttpbind.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                 
                  ChannelFactory<IService1> factory = new ChannelFactory<IService1>(wshttpbind, objAddress);
                 
                  foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
                  {
                      
                      DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;
                      if (dataContractBehavior != null)
                      {
                          dataContractBehavior.MaxItemsInObjectGraph = 65536000;
                      }
                  }
                  try
                  {
                      IService1 m_proxy = factory.CreateChannel();
                       MyProdfulllist prodfullist=new MyProdfulllist();                     prodfullist = m_proxy.GetMyProdfull();
                          
                          int recordcount = prodfullist.Count;
                          int i = 0;
                          MyProdfullStruct[] prodfullstruct = new MyProdfullStruct[recordcount];
                          foreach (MyProdfullDatacontract prodfulldc in prodfullist)
                          {
                              prodfullstruct[i].prodno = prodfulldc.yprodno;
                              prodfullstruct[i].prodname = prodfulldc.prodname;
                              prodfullstruct[i].prodsize = prodfulldc.prodsize;
                            
                        
                              i++;
                          }

                         
                          ICommunicationObject channel = m_proxy as ICommunicationObject;
                          Debug.Assert(channel != null);
                          channel.Close();
                          factory.Close();
                          return prodfullstruct;

                  }
                  catch
                  {
                      factory.Abort();
                      throw;
                  }

    • 已编辑 ken yup 2013年3月11日 11:03
    2013年3月11日 5:28

答案

全部回复

  • 服务端

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

    namespace mybh
    {
       
        [ServiceContract]
        public interface IService1
        {

            [OperationContract]
            [FaultContract(typeof(ProdfullFault))]
            MyProdfulllist GetMyProdfull();

        }

        [DataContract]
        public class MyProdfullDatacontract
        {
            [DataMember]
            public string Myprodno
            {
                get;
                set;
            }

            [DataMember]
            public string prodname
            {
                get;
                set;
            }

            [DataMember]
            public string prodsize
            {
                get;
                set;
            }
           

        }

        [CollectionDataContract]
        public class MyProdfulllist : List<MyProdfullDatacontract>
        {

        }

        public MyProdfulllist GetMyProdfull()
            {

     

                string connstring1 = @"server =192.168.0.2;database=db1;uid=sa;pwd=''";
                string sqlquery1 = @"  SELECT prod_no,prod_name,prod_size FROM product ";
                SqlConnection conn1 = null;
                SqlDataReader reader1 = null;
                MyProdfulllist prodfullist = new MyProdfulllist();

                int i = 0;

                try
                {
                    conn1 = new SqlConnection(connstring1);
                    conn1.Open();
                    SqlCommand cmd1 = new SqlCommand(sqlquery1, conn1);
                    reader1 = cmd1.ExecuteReader();

                    if (reader1.Read())
                    {
                        while (reader1.Read())
                        {
                            MyProdfullDatacontract prodfuldc = new MyProdfullDatacontract
                            {
                                prodno = reader1[0].ToString(),
                                prodname = reader1[1].ToString(),
                                prodsize = reader1[2].ToString(),
                             
                          
                            
                            };

                            prodfullist.Add(prodfuldc);
                            i++;

                        }
                    }
                }
                catch (Exception ex)
                {
                    ProdfullFault pff = new ProdfullFault();
                    pff.Operation = "exception occur when get prodfull list";
                    pff.ProblemType = ex.ToString();
                    string expmessag = ex.ToString();
                    if (expmessag.Length > 900)
                        expmessag = ex.ToString().Substring(0, 900);
                    logtheexp(ip, myusername, pff.Operation + expmessag);
                    throw new FaultException<ProdfullFault>(pff, expmessag);

                }
                finally
                {
                    if (reader1 != null)
                        reader1.Close();
                    if (conn1 != null)
                        conn1.Close();
                }

    }

    }

    2013年3月11日 11:04
  • 问题解决了,这里和大家分享一下

    加上这句就OK了

    问题1的解决办法是 如下

    ServiceReference1.Service1Client m_proxy = new ServiceReference1.Service1Client(wshttpbind, objAddress);
                  ///     m_proxy.ClientCredentials.UserName.UserName = theusername;
                  ///     

                  foreach (var operationDescription in m_proxy.Endpoint.Contract.Operations)
                  {
                      DataContractSerializerOperationBehavior dcsob =
                          operationDescription.Behaviors.Find<DataContractSerializerOperationBehavior>();
                      if (dcsob != null)
                      {
                          dcsob.MaxItemsInObjectGraph = 65536000;
                      }
                  }

    问题2我仍然不知道为什么
    • 已标记为答案 ken yup 2013年3月12日 13:27
    • 取消答案标记 ken yup 2013年3月12日 13:28
    • 已编辑 ken yup 2013年3月12日 13:28
    2013年3月12日 13:27
  • 你好,

    如果你在客户端使用ChannelFactory类来创建终结点监听器的话,你需要先使用Svcutil.exe为客户端生成契约。

    http://msdn.microsoft.com/en-us/library/ms734681.aspx

    希望有所帮助。


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 ken yup 2013年3月13日 5:09
    2013年3月13日 3:01
    版主
  • 谢谢你,怪不得我老不成功,原来我漏了一个步骤

    很多书都没清楚写要这个部骤啊

    比如<WCF 服务编程><WCF 全面解释>等 我是抄书本的

    2013年3月13日 5:17