基于WCF应用的一个实例

基于WCF应用的一个实例

本文参考url:

http://www.cnblogs.com/artech/archive/2007/02/26/656901.html

根据上文的思想和代码做的一个调试通过的一个实例和在调试理解过程的一点经验

(1)合约接口的定义

Contracts工程 类库 .net4.0  引用System.ServiceModel

定义计算合约接口ICalculator如下所示:

/// <summary> /// 计算接口 合约 /// create date:2012-04-17 /// </summary> [ServiceContract(Name = "CalculatorService", Namespace = "http://www.locationinfo.net")] 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); //除法操作合约 [OperationContract] double Sqrt(double x); //平方根操作合约 }
(2)实现合约接口的服务类CalculatorService

Services工程 类库  .net4.0 引用System.ServiceModel   ,Contracts工程

实现计算合约接口的服务类CalculatorService 如下如示:

using System; using System.Collections.Generic; using System.Linq; using System.Text; // using Contracts; namespace Services { /// <summary> /// Services.CalculatorService /// </summary> public class CalculatorService:ICalculator { #region 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; } public double Sqrt(double x) { return Math.Sqrt(x); } #endregion } // } CalculatorService.svc      //iis内寄宿的wcf访问文件

<%@ServiceHost Service="Services.CalculatorService" %>

Web.config 的内容如下所示:

<?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="Services.CalculatorService"> <endpoint binding="wsHttpBinding" contract="Contracts.ICalculator" /> </service> </services> </system.serviceModel> <system.web> <identity impersonate ="false"/> </system.web> </configuration>
wcf在iis中部署出现的问题的解决方法:

//wcf在iis中部署出现的问题的解决方法:
//
解决IIS7.5中部署WCF时,访问.svc文件的404错误问题
分类: .net 2011-09-15 15:36 62人阅读 评论(0) 收藏 举报
如果你直接在IIS 7中配置WCF,访问.svc文件时会出现404错误。
解决方法,以管理员身份进入命令行模式,运行:
"%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r -y


-r         - 重新安装此版本的 Windows Communication Foundation,
              并更新 IIS 元数据库根处的脚本映射和根以下的所有
              脚本映射。无论原始版本是什么,都将现有的脚本映射
              升级到此版本。
-y         - 在卸载或重新安装组件之前不要求确认。

重新注册iis visual studio 10 下的cmd
.net版本为4.0
执行以下命令:
aspnet_regiis.exe -iru

iis中添加虛拟目录.net版本为.net4.0集成 模式运行

//
Services工程.net 4.0版本
Contracts工程.net 4.0版本


(3)Host工程  控制台程序 .net4.0

App.config文件内容如下所示:

<?xml version="1.0"?> <configuration> <system.serviceModel> <behaviors> <!-- 添加服务元数据行为--> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8999/calculatorservice/metadata"/> </behavior> </serviceBehaviors> </behaviors> <services> <!-- 添加服务终结点--> <service name="Services.CalculatorService" behaviorConfiguration="metadataBehavior"> <endpoint address="http://127.0.0.1:8999/calculatorservice" binding="wsHttpBinding" contract="Contracts.ICalculator"/> <!--在此添加其他服务终结点--> </service> </services> </system.serviceModel> <startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
Program.cs文件内容如下所示:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; // using Services; using Contracts; using System.ServiceModel.Description; namespace Hosting { class Program { static void Main(string[] args) { //引用app.config参数的方法 using(ServiceHost host=new ServiceHost(typeof(CalculatorService))) { host.Opened+=new EventHandler(host_Opened); host.Open(); Console.Read(); } // } //代码级添加wcf服务的方法 static void code_add_wcf_service_method() { string address = "http://192.168.1.232:8999/CalculatorService"; Uri uri = new Uri(address); using (ServiceHost host = new ServiceHost(typeof(CalculatorService))) { host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), address); if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) { //定义要添加服务元数据行为 ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; string md_address = address + "/metadata"; behavior.HttpGetUrl = new Uri(md_address); //添加服务元数据行为 host.Description.Behaviors.Add(behavior); } //添加Opened事件 host.Opened += new EventHandler(host_Opened); //打开服务 host.Open(); Console.Read(); } } static void host_Opened(object sender, EventArgs e) { Console.WriteLine("calculatorService已经启动,按任意键终止服务!"); } } }
(4)Client工程 控制台程序 .net4.0

App.Config文件内容如下所示:

<?xml version="1.0"?> <configuration> <system.serviceModel> <client> <!--http://127.0.0.1:8999/CalculatorService--> <endpoint address="http://127.0.0.1/services/calculatorservice.svc" binding="wsHttpBinding" contract="Contracts.ICalculator" name="CalculatorService"/> </client> </system.serviceModel> <startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
Client.Program.cs文件内容如下所示:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Client.CalculatorServices; using System.ServiceModel; using Contracts; namespace Client { class Program { static void Main(string[] args) { //添加服务引用方法调用Wcf服务的方法,和引用WebService方法一样的调用 /* using (CalculatorServiceClient proxy = new CalculatorServiceClient()) { Console.WriteLine("使用CalculatorServiceClient方式调用Wcf服务的方法"); 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.WriteLine("sqrt({0})={1}", 25, proxy.Sqrt(25)); // Console.Read(); }*/ // /*address=http://127.0.0.1:8999/CalculatorService *address=http://127.0.0.1/services/calculatorservice.svc *引用Contracts合约接口定义组件 */ using (ChannelFactory<ICalculator> cnfac = new ChannelFactory<ICalculator>("CalculatorService")) { Console.WriteLine("使用ChannelFactory<ICalculator>方式调用Wcf服务的方法"); ICalculator proxy = cnfac.CreateChannel(); // 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.WriteLine("sqrt({0})={1}", 25, proxy.Sqrt(25)); // Console.Read(); } } } }
---the---end---

----2012-04-17---

转载于:https://www.cnblogs.com/sqlite3/archive/2012/04/17/2566723.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值