刚开始学习wcf,根据官方网站的说明写下的代码
第一步:
建立一个类库项目GettingStartedLib,首先添加wcf引用System.ServiceModel; 添加接口ICalculator,添加类CalculatorService实现接口ICalculator
代码:ICalculator
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace GettingStartedLib { [ServiceContract] //契约声明 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); } }
代码:CalculatorService
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GettingStartedLib { public class CalculatorService : ICalculator { public double Add(double n1, double n2) { double result = n1 + n2; Console.WriteLine("相加数:({0},{1})", n1, n2); // 输出结果到控制台 Console.WriteLine("结果: {0}", result); return result; } public double Subtract(double n1, double n2) { double result = n1 - n2; Console.WriteLine("相减数:({0},{1})", n1, n2); Console.WriteLine("结果: {0}", result); return result; } public double Multiply(double n1, double n2) { double result = n1 * n2; Console.WriteLine("相乘数:({0},{1})", n1, n2); Console.WriteLine("结果: {0}", result); return result; } public double Divide(double n1, double n2) { double result = n1 / n2; Console.WriteLine("相除数:({0},{1})", n1, n2); Console.WriteLine("结果: {0}", result); return result; } } }
然后在这个类库中添加app.config文件。刚开始做不知道文件格式,可以另建立一个wcf类库服务,找到自动生成的配置文件当做模板,复制到当前的项目中,少做修改就行了。
代码:app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" /> </system.web> <!-- 部署服务库项目时,必须将配置文件的内容添加到 主机的 app.config 文件中。System.Configuration 不支持库的配置文件。--> <system.serviceModel> <services> <service name="GettingStartedLib.CalculatorService"> <host> <baseAddresses> <add baseAddress = "http://localhost:8100" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否则地址将与上面提供的基址相关 --> <endpoint address="" binding="basicHttpBinding" contract="GettingStartedLib.ICalculator"> <!-- 部署时,应删除或替换下列标识元素,以反映 用来运行所部署服务的标识。删除之后,WCF 将 自动推断相应标识。 --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 --> <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息, 请在部署前将以下值设置为 false --> <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/> <!-- 要接收故障异常详细信息以进行调试, 请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息--> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
第二步:
建立服务启动程序,这个创建一个控制台应用程序GettingStartedHost,首先添加wcf引用System.ServiceModel,添加GettingStartedLib类库的引用。
代码:GettingStartedHost
using GettingStartedLib; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Threading.Tasks; namespace GettingStartedHost { class Program { static void Main(string[] args) { // Step 1 创建一个基地址 Uri baseAddress = new Uri("http://localhost:8100/GettingStarted"); // Step 2 创建 ServiceHost 实例 ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); try { // Step 3 增加服务终结点类型 selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService"); // Step 4 允许媒体类型转换. ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb); // Step 5 启动服务. selfHost.Open(); Console.WriteLine("服务已经启动."); Console.WriteLine("按Enter键终止服务."); Console.WriteLine(); Console.ReadLine(); // 关闭服务. selfHost.Close(); } catch (CommunicationException ce) { Console.WriteLine("异常出现: {0}", ce.Message); selfHost.Abort(); } } } }
第三步:
建立控制台应用程序GettingStartedClient,首先添加引用System.ServiceModel,和GettingStartedLib类库
代码:GettingStartedClient.Program
using GettingStartedClient.ServiceReference1; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GettingStartedClient { class Program { static void Main(string[] args) { //Step 1: 创建一个Wcf代理实例. CalculatorClient client = new CalculatorClient(); // Step 2: 请求服务操作. // 请求加法操作. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); //请求减法操作. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // 请求乘法操作. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // 请求除法操作. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); //Step 3:关闭服务 client.Close(); } } }
这里建立好客户端程序还是不可以使用,需要与wcf服务添加到客户端,我是通过vs的命令行工具 切换到客户端的项目目录输入:
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service
,最后会自动生成引用文件。
界面:
第四步:
启动应用测试:首先启动Host程序,找到bin目录打开生成的.exe程序,启动服务。然后打开客户端程序。
代码下载:http://files.cnblogs.com/files/dearbeans/GettingStartedLib.rar
时间: 2024-11-05 18:55:17