基于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); //平方根操作合约 }
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中部署出现的问题的解决方法:
//
解决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>
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已经启动,按任意键终止服务!"); } } }
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>
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(); } } } }
----2012-04-17---