标签:order osi rac 简单的 介绍 turn [] 计算机 static
这一系列文章的内容是从MSDN中COPY过来的,讲述的是最简单的WCF程序示例:如何在控制台应用程序实现和承载WCF服务,以及如何创建、配置和使用WCF客户端。
文章主体可分为两部分,分别介绍服务器端和客户端的编程实现。细分的话,可以分为六项任务。
这是创建基本 Windows Communication Foundation (WCF) 服务和可以使用该服务的客户端所需的六项任务中的第一项任务。
创建基本 WCF 服务时,第一项任务是为与外界共享的服务创建协定,并在其中描述如何与该服务进行通信。
具体步骤为:
1、 创建新的控制台应用程序项目。 在“新建项目”对话框中,选中“Visual Basic”或“Visual C#”,并选择“控制台应用程序”模板,并命名为Service。 使用默认的位置。
2、将默认的Service 命名空间更改为 Microsoft.ServiceModel.Samples。
3、为项目提供对 System.ServiceModel 命名空间的引用:右击“解决方案资源管理器”中的“Service”项目,选择“添加引用”项,在弹出的对话框中的“.NET”选项卡里的“组件名称”中选择“System.ServiceModel”,然后单击“确定”。
下面是编程步骤:
1、为 System.ServiceModel 命名空间添加一个 using 语句。
using System.ServiceModel;
2、创建一个新的ICalculator 接口,并将 ServiceContractAttribute 属性应用于该接口,并将 Namespace 值设置为“http://Microsoft.ServiceModel.Samples”。 此命名空间指定该服务在计算机上的路径,并构成该服务的基址部分。 请注意,在通过采用方括号表示法的属性来批注接口或类时,该属性类可以从其名称中去掉“Attribute”部分。
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
3、在接口中创建方法声明,并将 OperationContractAttribute 属性应用于每个要作为公共 WCF 协定的一部分公开的方法。
[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);
下面是创建服务协定的完整代码段:
using System;
// Add the using statement for the Sytem.ServiceModel namespace
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples
{
// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
// Create the method declaration for the contract.
[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);
}
}
........(整个服务器端的代码未完,等讲完任务三时,再给出完整代码)
1、创建一个新 CalculatorService 类,该类从用户定义的 ICalculator 接口继承而来并实现该接口定义的协定功能。
public class CalculatorService : ICalculator
2、实现每个算术运算符的功能。
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
// Code added to write output to the console window.
Console.WriteLine("Return: {0}", result);
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Received Multiply({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
在创建和实现了服务协定后,下一步是运行该服务。 运行服务由三个步骤组成:配置、承载和打开服务。
为服务的基址创建 Uri 实例。 此 URI 指定 HTTP 方案、本地计算机、端口号 8000,以及服务协定中为服务命名空间指定的服务路径ServiceModelSample/Services。
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); |
selfHost.AddServiceEndpoint( typeof(ICalculator), new WSHttpBinding(), "CalculatorService"); |
ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb); |
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
下面是服务器端(即“Service”项目中program.cs文件中)的完整程序代码:
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
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 that implements the service contract.
// Added code to write output to the console window.
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Received Multiply({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
}
class Program
{
static void Main(string[] args)
{
// Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
// Create ServiceHost
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
// Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof(ICalculator),
new WSHttpBinding(),
"CalculatorService");
// Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
// Start (and then stop) the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
若要运行服务,启动项目文件夹下bin目录中的 Service.exe即可。
前面三篇文章是讲服务器端的部署和运行,下面讲讲客户端如何配置和使用。
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service
在 Visual Studio 中,将在前一过程中生成的 App.config 配置文件添加到客户端项目中。 在“解决方案资源管理器”中右击该客户端,选择“添加现有项”,然后从 C:\Documents and Settings\<用户名>\Documents\Visual Studio 2008\Projects\Service\Client\bin 目录中选择 App.config 配置文件。
将app.config添加到项目中后,就算是完成了wcf客户端的配置。因为具体的配置信息,我们在使用svcutil.exe工具时,它就帮我们配置好并写入了app.config文件。
1、为要调用的服务的基址创建 EndpointAddress 实例,然后创建 WCF Client 对象。
//Create an endpoint address and an instance of the WCF Client.
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);
2、从 Client 内调用客户端操作。
// Call the service operations.
// 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);
3、在 WCF 客户端上调用 Close。
// Closing the client gracefully closes the connection and cleans up resources.
client.Close();
下面是客户端的完整代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace ServiceModelSamples
{
class Client
{
static void Main()
{
//Step 1: Create an endpoint address and an instance of the WCF Client.
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);
// Step 2: Call the service operations.
// 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);
//Step 3: Closing the client gracefully closes the connection and cleans up resources.
client.Close();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
}
}
若要启动客户端,请在“开始”菜单中的“Microsoft Windows SDK”项下选择“CMD Shell”,从而启动 Windows SDK 控制台会话。 定位至 C:\Documents and Settings\<用户名>\Documents\Visual Studio 2008\Projects\Service\Client\obj\Debug 目录,键入 client,然后按Enter。 操作请求和响应将出现在客户端控制台窗口中,如下所示。
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Press <ENTER> to terminate client.
最后,附上我的ASP.NET学习群,欢迎各位同行入群指导交流。技术群:【ASP.NET技术社区】872894940
标签:order osi rac 简单的 介绍 turn [] 计算机 static
原文地址:https://www.cnblogs.com/xu-yi/p/10035821.html