共用方式為


KnownAssemblyAttribute

此範例示範如何使用 DataContractResolver 類別自訂序列化和還原序列化程序。 此範例示範如何在序列化和還原序列化期間動態加入已知的型別。

範例詳細資料

此範例是由四個專案所組成。 其中一個專案會對應到 IIS 所裝載的服務,這個專案會定義下列服務合約。

   // Definition of a service contract.
   [ServiceContract(Namespace = "http://Microsoft.Samples.KAA")]
   [KnownAssembly("Types")]
   public interface IDataContractCalculator
    {
       [OperationContract]
       ComplexNumber Add(ComplexNumber n1, ComplexNumber n2);

       [OperationContract]
       ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2);

       [OperationContract]
       ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2);

       [OperationContract]
       ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2);

       [OperationContract]
       List<ComplexNumber> CombineLists(List<ComplexNumber> list1, List<ComplexNumber> list2);
   }

服務合約的實作方式如下列範例所示。

   // Service class that implements the service contract.
    public class DataContractCalculatorService : IDataContractCalculator
    {
       public ComplexNumber Add(ComplexNumber n1, ComplexNumber n2)
       {
           return new ComplexNumberWithMagnitude(n1.Real + n2.Real, n1.Imaginary + n2.Imaginary);
       }

       public ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2)
       {
           return new ComplexNumberWithMagnitude(n1.Real - n2.Real, n1.Imaginary - n2.Imaginary);
       }

       public ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2)
       {
           double real1 = n1.Real * n2.Real;
           double imaginary1 = n1.Real * n2.Imaginary;
           double imaginary2 = n2.Real * n1.Imaginary;
           double real2 = n1.Imaginary * n2.Imaginary * -1;

           return new ComplexNumber(real1 + real2, imaginary1 + imaginary2);
       }

       public ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2)
       {
           ComplexNumber conjugate = new ComplexNumber(n2.Real, -1 * n2.Imaginary);
           ComplexNumber numerator = Multiply(n1, conjugate);
           ComplexNumber denominator = Multiply(n2, conjugate);

           return new ComplexNumber(numerator.Real / denominator.Real, numerator.Imaginary);
       }

       public List<ComplexNumber> CombineLists(List<ComplexNumber> list1, List<ComplexNumber> list2)
       {
           List<ComplexNumber> result  = new List<ComplexNumber>();
           result.AddRange(list1);
           result.AddRange(list2);

           return result;
       }
   }

另一個專案會對應到用戶端,以便與伺服器進行通訊並叫用所公開的方法。 用戶端的定義顯示在以下範例中。

    // Client implementation code.
    class Client
    {
       static void Main()
       {
           // Create a channel.
            EndpointAddress address = new EndpointAddress("https://localhost/servicemodelsamples/service.svc/IDataContractCalculator");
           BasicHttpBinding binding = new BasicHttpBinding();
           ChannelFactory<IDataContractCalculator> factory = new ChannelFactory<IDataContractCalculator>(binding, address);
           IDataContractCalculator channel = factory.CreateChannel();

           // Call the Add service operation.
            ComplexNumber value1 = new ComplexNumber(1, 2);
           ComplexNumber value2 = new ComplexNumberWithMagnitude(3, 4);
           ComplexNumber result = channel.Add(value1, value2);
           Console.WriteLine("Add({0} + {1}i, {2} + {3}i) = {4} + {5}i",
               value1.Real, value1.Imaginary, value2.Real, value2.Imaginary, result.Real, result.Imaginary);
           if (result is ComplexNumberWithMagnitude)
           {
               Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
           }
           else
            {
               Console.WriteLine("No magnitude was sent from the service");
           }
           Console.WriteLine();

           // Call the Subtract service operation.
            value1 = new ComplexNumber(1, 2);
           value2 = new ComplexNumber(3, 4);
           result = channel.Subtract(value1, value2);
           Console.WriteLine("Subtract({0} + {1}i, {2} + {3}i) = {4} + {5}i",
               value1.Real, value1.Imaginary, value2.Real, value2.Imaginary, result.Real, result.Imaginary);
           if (result is ComplexNumberWithMagnitude)
           {
               Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
           }
           else
            {
               Console.WriteLine("No magnitude was sent from the service");
           }
           Console.WriteLine();

           // Call the Multiply service operation.
            value1 = new ComplexNumber(2, 3);
           value2 = new ComplexNumber(4, 7);
           result = channel.Multiply(value1, value2);
           Console.WriteLine("Multiply({0} + {1}i, {2} + {3}i) = {4} + {5}i",
               value1.Real, value1.Imaginary, value2.Real, value2.Imaginary, result.Real, result.Imaginary);
           if (result is ComplexNumberWithMagnitude)
           {
               Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
           }
           else
            {
               Console.WriteLine("No magnitude was sent from the service");
           }
           Console.WriteLine();

           // Call the Divide service operation.
            value1 = new ComplexNumber(3, 7);
           value2 = new ComplexNumber(5, -2);
           result = channel.Divide(value1, value2);
           Console.WriteLine("Divide({0} + {1}i, {2} + {3}i) = {4} + {5}i",
               value1.Real, value1.Imaginary, value2.Real, value2.Imaginary, result.Real, result.Imaginary);
           if (result is ComplexNumberWithMagnitude)
           {
               Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
           }
           else
            {
               Console.WriteLine("No magnitude was sent from the service");
           }
           Console.WriteLine();

           // Call the CombineLists service operation.
            List<ComplexNumber> list1 = new List<ComplexNumber>();
           List<ComplexNumber> list2 = new List<ComplexNumber>();
           list1.Add(new ComplexNumber(1, 1));
           list1.Add(new ComplexNumber(2, 2));
           list1.Add(new ComplexNumberWithMagnitude(3, 3));
           list1.Add(new ComplexNumberWithMagnitude(4, 4));
           List<ComplexNumber> listResult = channel.CombineLists(list1, list2);
           Console.WriteLine("Lists combined:");
           foreach (ComplexNumber n in listResult)
           {
               Console.WriteLine("{0} + {1}i", n.Real, n.Imaginary);
           }
           Console.WriteLine();

           // Close the channel
            ((IChannel)channel).Close();

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

服務合約的定義會以 KnownAssembly 屬性標示。 此屬性包含類型程式庫的名稱,在執行階段,服務和用戶端都會知道所有類型。

KnownAssembly 屬性會實作 IContractBehavior 以定義 DataContractSerializer,並針對每個作業行為定義 DataContractResolver。 建立 DataContractResolver 時,會透過組件反映,並以序列化和還原序列化不同類型時使用之類型和名稱的對應建立字典。 如此一來,ResolveTypeResolveName 類型就必須在字典中查閱所需的資料。

針對此範例定義的 DataContractResolver 會顯示在以下範例中。

public class MyDataContractResolver : DataContractResolver
    {
       Dictionary<string, XmlDictionaryString> dictionary = new Dictionary<string, XmlDictionaryString>();
       Assembly assembly;

       public MyDataContractResolver(string assemblyName)
       {
           this.KnownTypes = new List<Type>();

           assembly = Assembly.Load(new AssemblyName(assemblyName));
           foreach (Type type in assembly.GetTypes())
           {
               bool knownTypeFound = false;
               System.Attribute[] attrs = System.Attribute.GetCustomAttributes(type);
               if (attrs.Length != 0)
               {
                   foreach (System.Attribute attr in attrs)
                   {
                       if (attr is KnownTypeAttribute)
                       {
                           Type t = ((KnownTypeAttribute)attr).Type;
                           if (this.KnownTypes.IndexOf(t) < 0)
                           {
                               this.KnownTypes.Add(t);
                           }
                           knownTypeFound = true;
                       }
                   }
               }
               if (!knownTypeFound)
               {
                   string name = type.Name;
                   string namesp = type.Namespace;
                   if (!dictionary.ContainsKey(name))
                   {
                       dictionary.Add(name, new XmlDictionaryString(XmlDictionary.Empty, name, 0));
                   }
                   if (!dictionary.ContainsKey(namesp))
                   {
                       dictionary.Add(namesp, new XmlDictionaryString(XmlDictionary.Empty, namesp, 0));
                   }
               }
           }
       }

       public IList<Type> KnownTypes
       {
           get; set;
       }

       // Used at deserialization
        // Allows users to map xsi:type name to any Type 
        public override Type ResolveName(string typeName, string typeNamespace, DataContractResolver knownTypeResolver)
       {
           XmlDictionaryString tName;
           XmlDictionaryString tNamespace;

           if (dictionary.TryGetValue(typeName, out tName) && dictionary.TryGetValue(typeNamespace, out tNamespace))
           {
               return this.assembly.GetType(tNamespace.Value + "." + tName.Value);
           }
           else
            {
               return knownTypeResolver.ResolveName(typeName, typeNamespace, null);
           }
       }

       // Used at serialization
        // Maps any Type to a new xsi:type representation
        public override void ResolveType(Type dataContractType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
       {
           knownTypeResolver.ResolveType(dataContractType, null, out typeName, out typeNamespace);
           if (typeName == null || typeNamespace == null)
           {
               typeName = new XmlDictionaryString(XmlDictionary.Empty, dataContractType.Name, 0);
               typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, dataContractType.Namespace, 0);
           }
       }
   }

此範例中所使用之類型的程式庫會顯示在以下範例中。

    [DataContract]
    public class ComplexNumber
    {
       [DataMember]
       private double real;

       [DataMember]
       private double imaginary;

       public ComplexNumber(double r1, double i1)
       {
           this.Real = r1;
           this.Imaginary = i1;
       }

       public double Real
       {
           get { return real; }
           set { real = value; }
       }

       public double Imaginary
       {
           get { return imaginary; }
           set { imaginary = value; }
       }
   }

   [DataContract]
   public class ComplexNumberWithMagnitude : ComplexNumber
    {
       public ComplexNumberWithMagnitude(double real, double imaginary) : base(real, imaginary) { }

       [DataMember]
       public double Magnitude
       {
           get { return Math.Sqrt(Imaginary * Imaginary + Real * Real); }
           set { }
       }
   }

請注意,ComplexNumber 不需要以靜態方式了解 ComplexNumberWithMagnitude 類型,因為在執行階段就會知道它。

範例建立完成並執行時,這是用戶端中取得的預期輸出:

Add(1 + 2i, 3 + 4i) = 4 + 6i
Magnitude: 7.21110255092798

Subtract(1 + 2i, 3 + 4i) = -2 + -2i
Magnitude: 2.82842712474619

Multiply(2 + 3i, 4 + 7i) = -13 + 26i
No magnitude was sent from the service

Divide(3 + 7i, 5 + -2i) = 0.0344827586206897 + 41i
No magnitude was sent from the service

Lists combined:
1 + 1i
2 + 2i
3 + 3i
4 + 4i

若要設定、執行及建立範例

  1. 以滑鼠右鍵按一下 [KnownAssemblyAttribute] 方案,然後選取 [屬性]。

  2. 選取 [通用屬性] 中的 [啟始專案],然後按一下 [多個啟始專案]。

  3. 將 [開始] 動作加入至 [服務] 和 [用戶端] 專案。

  4. 按一下 [確定],然後按 F5 執行範例。

  5. 如果應用程式沒有正確執行,請遵循下列步驟確認您的環境已正確設定:

  6. 確認您已經執行 Windows Communication Foundation 範例的單次安裝程序

  7. 若要建立方案,請遵循建置 Windows Communication Foundation 範例中的指示進行。

  8. 若要在單一或多台電腦組態中執行此範例,請遵循執行 Windows Communication Foundation 範例中的指示進行。

Dd807519.Important(zh-tw,VS.100).gif 注意:
這些範例可能已安裝在您的電腦上。 請先檢查下列 (預設) 目錄,然後再繼續。

<InstallDrive>:\WF_WCF_Samples

如果此目錄不存在,請移至用於 .NET Framework 4 的 Windows Communication Foundation (WCF) 與 Windows Workflow Foundation (WF) 範例 (英文),以下載所有 Windows Communication Foundation (WCF) 和 WF 範例。 此範例位於下列目錄。

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Contract\Data\KnownAssemblyAttribute