第一个WCF程序

看《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();
}
}
}
结果:
HELLO1.JPG
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值