zoukankan      html  css  js  c++  java
  • 利用wcf传递字节的简单例子

    1创建接口   ICalculator

       [ServiceContract(Name = "CalculatorService",Namespace = "liufuchu")]
        public interface ICalculator
        {
            [OperationContract]
            string UpLoadData(byte[] bbbbb);
        }

    2 创建计算服务 CalculatorService

        public class CalculatorService :ICalculator
        {
            public string UpLoadData(byte[] bbbbb)
            {
                string fefe = Encoding.UTF8.GetString(bbbbb);
                
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes("0000000000000000");
                byte[] ivArray = UTF8Encoding.UTF8.GetBytes("0000000000000000");
    
                string roundtrip = DecryptStringFromBytes_Aes(bbbbb, keyArray, ivArray);
                
                DataSet ds = new DataSet();
                //string vv = Encoding.UTF8.GetString(bbbbb);
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(roundtrip));
                //MemoryStream ms = new MemoryStream(bbbbb);
    
                ds.ReadXml(ms);
                ms.Close();
                ms.Dispose();
                return "aa";
            }
    
            static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
            {
                // Check arguments.
                if (cipherText == null || cipherText.Length <= 0)
                    throw new ArgumentNullException("cipherText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("Key");
    
                // Declare the string used to hold
                // the decrypted text.
                string plaintext = null;
    
                // Create an AesCryptoServiceProvider object
                // with the specified key and IV.
                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                {
                    aesAlg.Key = Key;
                    aesAlg.IV = IV;
    
                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
    
                    // Create the streams used for decryption.
                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
    
                                // Read the decrypted bytes from the decrypting stream
                                // and place them in a string.
                                plaintext = srDecrypt.ReadToEnd();
                            }
                        }
                    }
    
                }
    
                return plaintext;
    
            }
        }

    3. 寄宿于IIS

    CalculatorService.svc

    <%@ ServiceHost  Service = "Services.CalculatorService"%>

    服务类输出路径为bin\

    4.服务端配置文件 Web.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="metadataBehavior">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="metadataBehavior" name="Services.CalculatorService">
            <endpoint binding="basicHttpBinding" contract="Contracts.ICalculator"></endpoint>        
          </service>
        </services>
      </system.serviceModel>
    </configuration>

    5 客户端调用类

        class Program
        {
            static void Main(string[] args)
            {
                //using (CalculatorServiceClient proxy = new CalculatorServiceClient())
                using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
                {
                    ICalculator proxy = channelFactory.CreateChannel();
                    using (proxy as IDisposable)
                    {
    
                        DataTable dt = new DataTable("update");
                        dt.Columns.Add("aa", typeof(string));
    
                        DataRow dr = dt.NewRow();
                        dr["aa"] = 1;
                        dt.Rows.Add(dr);
    
                        DataSet ds = new DataSet("aab");
                        ds.Tables.Add(dt);
    
                        ds.GetXml();
    
    
                        using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
                        {
                            byte[] keyArray = UTF8Encoding.UTF8.GetBytes("0000000000000000");
                            byte[] ivArray = UTF8Encoding.UTF8.GetBytes("0000000000000000");
    
                            byte[] encrypted = EncryptStringToBytes_Aes(ds.GetXml(), keyArray, ivArray);
    
                            string aa = proxy.UpLoadData(encrypted);
                            Console.WriteLine(aa);
                        }
                    }
                }
            }
    
            static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
            {
                // Check arguments.
                if (plainText == null || plainText.Length <= 0)
                    throw new ArgumentNullException("plainText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("Key");
                byte[] encrypted;
                // Create an AesCryptoServiceProvider object
                // with the specified key and IV.
                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                {
                    aesAlg.Key = Key;
                    aesAlg.IV = IV;
    
                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
    
                    // Create the streams used for encryption.
                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
    
                                //Write all data to the stream.
                                swEncrypt.Write(plainText);
                            }
                            encrypted = msEncrypt.ToArray();
                        }
                    }
                }
    
    
                // Return the encrypted bytes from the memory stream.
                return encrypted;
    
            }
    
        }

    6. 客户端配置文件

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <bindings />
        <client>
          <endpoint address="http://127.0.0.1/wcf/calculatorservice.svc"
            binding="basicHttpBinding" contract="Contracts.ICalculator"
            name="calculatorservice" />
        </client>
      </system.serviceModel>
    </configuration> 
  • 相关阅读:
    汉字数组排序及如何检测汉字
    响应式web布局中iframe的自适应
    CSS3的flex布局
    关于BFC不会被浮动元素遮盖的一些解释
    趣谈unicode,ansi,utf-8,unicode big endian这些编码有什么区别(转载)
    深入seajs源码系列三
    深入seajs源码系列二
    深入seajs源码系列一
    韩国"被申遗" (转自果壳)
    Understanding delete
  • 原文地址:https://www.cnblogs.com/smileberry/p/2625328.html
Copyright © 2011-2022 走看看