看《Programming Indigo》有一段时间了,今天算是第一次动手实现一个小程序。
因为书出的比较早,与目前发布的.NET3.0 SDK有不小的差别(主要是API)。所以这里把程序也贴出来以便有看这本书的朋友对照修改。这个小程序有可有多种实现方式,这里现贴出code-first方式。有空了再帖contract-first方式的。注意,要记着在server和client的project里加入对system.ServiceModel的引用。粗体是代码改动的地方。
首先,server端:
using System;
using System.ServiceModel;
namespace ProgrammingIndigo
{
//Contract definition.
[ServiceContract]
public interface IHello
{
[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 implementation.
public class HelloService : IHello
{
public double Add(double n1, double n2)
{
Console.WriteLine("Add called");
return n1 + n2;
}
public double Subtract(double n1, double n2)
{
Console.WriteLine("Subtract called");
return n1 - n2;
}
public double Multiply(double n1, double n2)
{
Console.WriteLine("Multiply called");
return n1 * n2;
}
public double Divide(double n1, double n2)
{
Console.WriteLine("Divide called");
return n1 / n2;
}
// Host the service.
public static void Main()
{
// Create a ServiceHost.
using (ServiceHost serviceHost = new ServiceHost(typeof(HelloService)))
{
// Add an endpoint.
WSHttpBinding binding = new WSHttpBinding();
Uri uri = new Uri("http://localhost:8000/hello1/");
serviceHost.AddServiceEndpoint(typeof(IHello),binding,uri);
// Open the service.
serviceHost.Open();
// The service can now be accessed.
// Hold it open until user presses ENTER.
Console.WriteLine("The service is ready");
Console.WriteLine();
Console.WriteLine("Press ENTER to shut down service.");
Console.WriteLine();
Console.ReadLine();
// Close the service.
serviceHost.Close();
}
}
}
}
client端:
using System;
using System.ServiceModel;
namespace ProgrammingIndigo
{
//Contract definition.
[ServiceContract]
public interface IHello
{
[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);
}
//Client implementation code.
class Client
{
static void Main()
{
// Create a proxy.
WSHttpBinding binding = new WSHttpBinding();
Uri uri = new Uri("http://localhost:8000/hello1/");
ChannelFactory channelFactory = new ChannelFactory(binding,"http://localhost:8000/hello1/");
IHello proxy = channelFactory.CreateChannel(); try
{
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
Console.WriteLine("Calling Add({0},{1})", value1, value2);
double result = proxy.Add(value1, value2);
Console.WriteLine(" Result: {0}", result);
// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
Console.WriteLine("Calling Subtract({0},{1})", value1, value2);
result = proxy.Subtract(value1, value2);
Console.WriteLine(" Result: {0}", result);
// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
Console.WriteLine("Calling Multiply({0},{1})", value1, value2);
result = proxy.Multiply(value1, value2);
Console.WriteLine(" Result: {0}", result);
// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
Console.WriteLine("Calling Divide({0},{1})", value1, value2);
result = proxy.Divide(value1, value2);
Console.WriteLine(" Result: {0}", result);
}
finally
{
((System.ServiceModel.Channels.IChannel)proxy).Close();//
//((System.ServiceModel.Channels.IChannel)proxy).Dispose(); }
Console.WriteLine();
Console.WriteLine("Press ENTER to shut down client");
Console.ReadLine();
}
}
}
结果: