wpf学习笔记(4)《都是自己根据网络资源学习记录的仅供参考》

    技术2022-05-11  80

    什么是WCF ?WCF和WPF怎么整合在一起。wcf是微软下一代的通讯标准,是将很多种通讯标准整合在一起,提供统一的通信操作方式.他的底层把这些服务整合在一起,上层提供给用户统一的访问方式,就是说写程序的时候不用再考虑是采用web服务还是其他通讯方式,如tcp它是一个通讯框架,不是任何服务.

    来我们写个小程序看下,下面是修改从微软官方下载的一个程序.(我用的是Visual Studio 2005 Extensions for Windows Workflow Foundation (CHS) CTP版本的).

    首先我们创建服务端的wcf项目起名叫service 添加一个类service.代码: //   Copyright (c) Microsoft Corporation.  All Rights Reserved. using  System; using  System.ServiceModel; 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);    }    // 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;        }    }} 添加一个service.svc的文件.代码: < %> @ServiceHost language = c# Debug = " true "  Service = " Microsoft.ServiceModel.Samples.CalculatorService "   % 添加一个web.config文件.代码: <? xml version = " 1.0 "  encoding = " utf-8 "   ?> < configuration >    < system.serviceModel >      < services >        < service           name = " Microsoft.ServiceModel.Samples.CalculatorService "           behaviorConfiguration = " CalculatorServiceBehavior " >          <!--  ICalculator  is  exposed at the  base  address provided by host: http: // localhost/servicemodelsamples/service.svc  -->            < endpoint address = ""                     binding = " wsHttpBinding "                     contract = " Microsoft.ServiceModel.Samples.ICalculator "   />            <!--  the mex endpoint  is  exposed at http: // localhost/servicemodelsamples/service.svc/mex -->          < endpoint address = " mex "                   binding = " mexHttpBinding "                   contract = " IMetadataExchange "   />        </ service >      </ services >      <!-- For debugging purposes  set  the includeExceptionDetailInFaults attribute to  true -->      < behaviors >        < serviceBehaviors >          < behavior name = " CalculatorServiceBehavior " >            < serviceMetadata httpGetEnabled = " True " />            < serviceDebug includeExceptionDetailInFaults = " False "   />          </ behavior >        </ serviceBehaviors >      </ behaviors >    </ system.serviceModel > </ configuration > 服务端写好了

    我们再写个客户端

    建立个client项目. 增加app.config文件.代码: <? xml version = " 1.0 "  encoding = " utf-8 "   ?> < configuration >    < system.serviceModel >      < client >        < endpoint address = " http://localhost/servicemodelsamples/service.svc?wsdl "                  binding = " wsHttpBinding "                  contract = " Microsoft.ServiceModel.Samples.ICalculator "   />      </ client >    </ system.serviceModel > </ configuration > 增加client类.代码: //   Copyright (c) Microsoft Corporation.  All Rights Reserved. using  System; using  System.ServiceModel; namespace  Microsoft.ServiceModel.Samples {    //The service contract is defined in generatedClient.cs, generated from the service by the svcutil tool.    //Client implementation code.    class Client    {        static void Main()        {            // Create a client            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);            //Closing the client gracefully closes the connection and cleans up resources            client.Close();            Console.WriteLine();            Console.WriteLine("Press <ENTER> to terminate client.");            Console.ReadLine();        }    }} 增加generatedClient类.代码: // ------------------------------------------------------------------------------ //  <auto-generated> //      This code was generated by a tool. //      Runtime Version:2.0.50727.42 // //      Changes to this file may cause incorrect behavior and will be lost if //      the code is regenerated. //  </auto-generated> // ------------------------------------------------------------------------------ namespace  Microsoft.ServiceModel.Samples {            [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""3.0.0.0")]    [System.ServiceModel.ServiceContractAttribute(Namespace = "http://Microsoft.ServiceModel.Samples", ConfigurationName = "Microsoft.ServiceModel.Samples.ICalculator")]    public interface ICalculator    {                [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]        double Add(double n1, double n2);                [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]        double Subtract(double n1, double n2);                [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]        double Multiply(double n1, double n2);                [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]        double Divide(double n1, double n2);    }        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""3.0.0.0")]    public interface ICalculatorChannel : Microsoft.ServiceModel.Samples.ICalculator, System.ServiceModel.IClientChannel    {    }        [System.Diagnostics.DebuggerStepThroughAttribute()]    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""3.0.0.0")]    public partial class CalculatorClient : System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>, Microsoft.ServiceModel.Samples.ICalculator    {                public CalculatorClient()        {        }                public CalculatorClient(string endpointConfigurationName) :                 base(endpointConfigurationName)        {        }                public CalculatorClient(string endpointConfigurationName, string remoteAddress) :                 base(endpointConfigurationName, remoteAddress)        {        }                public CalculatorClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :                 base(endpointConfigurationName, remoteAddress)        {        }                public CalculatorClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :                 base(binding, remoteAddress)        {        }                public double Add(double n1, double n2)        {            return base.Channel.Add(n1, n2);        }                public double Subtract(double n1, double n2)        {            return base.Channel.Subtract(n1, n2);        }                public double Multiply(double n1, double n2)        {            return base.Channel.Multiply(n1, n2);        }                public double Divide(double n1, double n2)        {            return base.Channel.Divide(n1, n2);        }    }}

    好了我们开始配置服务端.

    ......


    最新回复(0)