您的位置:首页 > 其它

构建一个简单的WCF应用

2011-04-25 12:40 741 查看
买了《WCF技术剖析》,按着书本的例子进行操作,写下我的操作过程。

参考博客:/article/1308643.html

步骤一 构建整个解决方案



步骤二 创建服务契约:ICalculator.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace xuwei.WcfServices.Contracts//服务契约
{
[ServiceContract(Name="CalculatorService",Namespace="http://blog.csdn.net/xw13106209")]//将接口定义成服务契约
public interface ICalculator
{
[OperationContract]
double Add(double x,double y);

[OperationContract]
double Subtract(double x,double y);

[OperationContract]
double Multiply(double x,double y);

[OperationContract]
double Divide(double x, double y);

}
}


步骤三 创建服务:CalculatorService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xuwei.WcfServices.Contracts;

namespace xuwei.WcfServices.Services//实现服务契约
{
public class CalculatorService:ICalculator
{
public double Add(double x,double y)
{
return x + y;
}

public double Subtract(double x, double y)
{
return x - y;
}

public double Multiply(double x, double y)
{
return x * y;
}

public double Divide(double x, double y)
{
return x / y;
}
}
}


步骤四 通过自我寄宿的方式寄宿服务:Hosting控制台中的program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xuwei.WcfServices.Contracts;
using xuwei.WcfServices.Services;
using System.ServiceModel.Description;
using System.ServiceModel;

namespace xuwei.WcfServices.Hosting//自主服务寄宿
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host=new ServiceHost(typeof(CalculatorService)))
{
host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");
host.Description.Behaviors.Add(behavior);
}

host.Opened += delegate
{
Console.WriteLine("CalculatorService已经启动,按任意键终止服务!");
};

host.Open();
Console.Read();
}
}
}
}


注意1:

完成以后需要编译Hosting下的program.cs。但是在通过Ctrl+F5执行(其实可以通过右键解决方案->生成解决方案完成,不需要通过Ctrl+F5执行)的时候可能报错:

无法直接启动带有“类库输出类型”的项目,如下图所示。



这时我们需要右键Hosting,然后选择“设为启动项目”,再次执行就不会报错了。

注意2

在进行真正的WCF应用开发时,一般不会直接通过编码的方式进行终结点的添加和服务行为的定义,而是通过配置的方式进行。上面添加终结点和定义服务行为的代码可以通过如下方法进行。首先在Hosting项目中创建应用程序配置文件App.config,在App.config中添加如下配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/calculatorservice/metadata" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metadataBehavior" name="xuwei.WcfServices.Services.CalculatorService">
<endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" contract="xuwei.WcfServices.Contracts.ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>


如果采用了上诉的配置,服务寄宿代码将会得到极大的精简,只需包含下面几行代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xuwei.WcfServices.Contracts;
using xuwei.WcfServices.Services;
using System.ServiceModel.Description;
using System.ServiceModel;

namespace xuwei.WcfServices.Hosting//自主服务寄宿
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host=new ServiceHost(typeof(CalculatorService)))
{
//host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");
//if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
//{
//    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
//    behavior.HttpGetEnabled = true;
//    behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");
//    host.Description.Behaviors.Add(behavior);
//}

host.Opened += delegate
{
Console.WriteLine("CalculatorService已经启动,按任意键终止服务!");
};

host.Open();
Console.Read();
}
}
}
}


步骤五 创建客户端调用服务:Client中的program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xuwei.WcfService.Client.CalculatorServices;
namespace xuwei.WcfService.Client
{
class Program
{
static void Main(string[] args)
{
using (CalculatorServiceClient proxy = new CalculatorServiceClient())
{
Console.WriteLine("x+y={2} when x={0} and y={1}",1,2,proxy.Add(1,2));
Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, proxy.Subtract(1, 2));
Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, proxy.Multiply(1, 2));
Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, proxy.Divide(1, 2));
Console.Read();
}
}
}
}


在执行步骤四以后E:/ms_workplace/WCF1/Hosting/bin/Debug目录下会有一个“Hosting.exe”的应用程序,双击打开该应用程序:



然后右键Client项目,选择“添加服务引用”



点击确定即可完成服务引用的添加,这时Client下就会多出一个Service Reference



双击CalculatorServices,在对象浏览器中能够看到如下视图



编译Client,会在E:/ms_workplace/WCF1/Client/bin/Debug有Client.exe,双击打开这个应用程序,会有如下结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: