none
compress压缩.客户端List<user>无返回. RRS feed

  • 问题

  • 利用的是microsoft的DEMO.


    //  Copyright (c) Microsoft Corporation.  All Rights Reserved.

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;
    using System.Runtime.Serialization;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;


    namespace Microsoft.ServiceModel.Samples
    {
        // Define a service contract.
        [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
        public interface ICalculator
        {
            [OperationContract]
            double Add(double n1, double n2);
            [OperationContract]
            double Subtract(double n1, double n2);
            [OperationContract]
            double Multiply(double n1, double n2);
            [OperationContract]
            double Divide(double n1, double n2);

            //操作契约,数据表
            [OperationContract]
            System.Data.DataTable GetDataByTable();
            //操作契约,数据集
            [OperationContract]
            System.Data.DataSet GetDataByDataSet();
            //操作契约,数据集合
            [OperationContract]
            List<User> GetDataByCollection();
        }
     
        // Service class which implements the service contract.
        public class CalculatorService : ICalculator
        {
            public double Add(double n1, double n2)
            {
                return n1 + n2;
            }

            public double Subtract(double n1, double n2)
            {
                return n1 - n2;
            }

            public double Multiply(double n1, double n2)
            {
                return n1 * n2;
            }

            public double Divide(double n1, double n2)
            {
                return n1 / n2;
            }

            public System.Data.DataTable GetDataByTable()
            {
                //这里可以定义数据持久化操作,访问数据库等
                System.Data.DataSet dataSet = new System.Data.DataSet();
                System.Data.DataTable dataTable = null;
                SqlConnection sqlConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database\\DatabaseWCF.mdf;Integrated Security=True;User Instance=True");
                try
                {

                    System.Data.SqlClient.SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter("SELECT id, name, english_name FROM TableWCF", sqlConnection);
                    sqlDataAdapter.Fill(dataSet, "TableWCF");
                    if (dataSet != null && dataSet.Tables.Count > 0)
                    {
                        dataTable = dataSet.Tables[0];
                    }
                }
                catch (Exception e)
                {
                }
                finally
                {
                    sqlConnection.Close();
                }
                Console.WriteLine("Calling WCF Service,Transfer data using DataTable...");
                return dataTable;
            }
            //实现接口定义的方法,DataSet传递数据
            public System.Data.DataSet GetDataByDataSet()
            {
                //这里可以定义数据持久化操作,访问数据库等
                System.Data.DataSet dataSet = new System.Data.DataSet();
                SqlConnection sqlConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database\\DatabaseWCF.mdf;Integrated Security=True;User Instance=True");
                try
                {
                    System.Data.SqlClient.SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter("SELECT id, name, english_name FROM TableWCF", sqlConnection);
                    sqlDataAdapter.Fill(dataSet, "TableWCF");
                }
                catch (Exception e)
                {
                }
                finally
                {
                    sqlConnection.Close();
                }
                Console.WriteLine("Calling WCF Service,Transfer data using dataSet...");
                return dataSet;

            }
            //实现接口定义的方法,Collection传递数据
            public List<User> GetDataByCollection()
            {
                //这里可以定义数据持久化操作,访问数据库等
                List<User> list = new List<User>();
                for (int i = 0; i < 10; i++)
                {
                    User user = new User();
                    user.age = 20 + i;
                    user.name = "Frank Xu Lei:" + i.ToString();
                }
                Console.WriteLine("Calling WCF Service,Transfer data using Collection...");
                return list;

            }
        }

     

        //3数据契约
        [DataContract]
        public class User
        {
            [DataMember]
            public string name;
            [DataMember]
            public int age;
        }
    }

     

    app.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <!-- use appSetting to configure base address provided by host -->
        <add key="baseAddress" value="http://localhost:8000/windspeed/" />
      </appSettings>

      <system.diagnostics>
        <trace autoflush="true"></trace>
        <sources>
          <source name="System.ServiceModel.MessageLogging">
            <listeners>
              <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="Service.e2e"/>
            </listeners>

          </source>
        </sources>
      </system.diagnostics>
     
      <system.serviceModel>
          <diagnostics>
          <messageLogging
        logMalformedMessages="false"
        logMessagesAtServiceLevel="false"
        logEntireMessage="false"
        logMessagesAtTransportLevel="false"/>
        </diagnostics>
       
        <services>
          <service behaviorConfiguration="MyServiceTypeBehaviors" name="Microsoft.ServiceModel.Samples.CalculatorService">
            <endpoint address="" binding="customBinding" bindingConfiguration="sampleBinding"
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>

     


        <behaviors>
          <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
              <!-- 将下列元素添加到服务行为配置中。 -->
              <serviceMetadata httpGetEnabled="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>

        <bindings>
          <customBinding>
            <binding name="sampleBinding">
              <compression compressionMode="GZip" compressionLevel="Normal"/>
              <httpTransport/>
            </binding>
          </customBinding>
        </bindings>
        <extensions>
          <bindingElementExtensions>
            <add name="compression" type="Microsoft.ServiceModel.Samples.CompressionSection, library"/>
          </bindingElementExtensions>
        </extensions>
      </system.serviceModel>
    </configuration>

     

    客户端:

       //Client implementation code.
        class Client
        {
            static void Main()
            {
                // Create a client with given client endpoint configuration
                //CalculatorProxy client = new CalculatorProxy();
                CalculatorClient client = new CalculatorClient();

                // Call the Add service operation.
                double value1 = 100.00D;
                double value2 = 15.99D;
                double result = client.Add(value1, value2);
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

                // Call the Subtract service operation.
                value1 = 145.00D;
                value2 = 76.54D;
                result = client.Subtract(value1, value2);
                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

                // Call the Multiply service operation.
                value1 = 9.00D;
                value2 = 81.25D;
                result = client.Multiply(value1, value2);
                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = client.Divide(value1, value2);
                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

                List<User> t = new List<User>();
                t=client.GetDataByCollection();
                if (t != null)
                {
                    Console.WriteLine("datatable");//绑定数据源到控件

                }

                //Closing the client gracefully closes the connection and cleans up resources
                client.Close();

                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate client.");
                Console.ReadLine();
            }

     

    加粗部分无返回.

    2010年4月4日 11:55

答案

全部回复