标签:style blog http color io os ar 文件 数据
using System.ServiceModel; namespace IService { [ServiceContract(Name="CalculatorService", Namespace="http://www.artech.com")] public interface ICalculator { [OperationContract] double Add(double x, double y); } }
using IService; namespace Service { public class CalculatorService:ICalculator { public double Add(double x, double y) { return x + y; } } }
using System; using System.ServiceModel; using Service; namespace ConsoleHost { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(CalculatorService))) { host.Opened += delegate { Console.WriteLine("CalculatorService已经启动,按任意键终止服务!"); }; host.Open(); Console.Read(); } } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="false"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Service.CalculatorService" behaviorConfiguration="metadataBehavior"> <endpoint address="http://127.0.0.1:3721/calculatorservice" binding="wsHttpBinding" contract="IService.ICalculator"/> </service> </services> </system.serviceModel> </configuration>
<%@ ServiceHost Language="C#" Debug="true" Service="Service.CalculatorService"%>
注意修改Web.config里面的代码,关键部分如下:
<system.serviceModel> <services> <service name="Service.CalculatorService" behaviorConfiguration="WebHost.Service1Behavior"> <!-- Service Endpoints --> <endpoint address="" binding="wsHttpBinding" contract="IService.ICalculator"> <!-- 部署时,应删除或替换下列标识元素,以反映 在其下运行部署服务的标识。删除之后,WCF 将 自动推导相应标识。 --> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="WebHost.Service1Behavior"> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点--> <serviceMetadata httpGetEnabled="true" /> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息--> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
第二步、修改版本
第三步、启动IIS成功
using System; using System.ServiceModel; using IService; namespace WCFClient { class Program { static void Main(string[] args) { using(ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice")) { ICalculator proxy = channelFactory.CreateChannel(); Console.WriteLine("3 + 1 = {0}", proxy.Add(3, 1)); } Console.Read(); } } }
App.config文件的代码如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="calculatorservice" address="http://127.0.0.1:3721/calculatorservice" binding="wsHttpBinding" contract="IService.ICalculator" /> </client> </system.serviceModel> </configuration>
配置文件配的是调用控制台宿主的WCF服务,如果要调用IIS的WCF就把黄色部分的代码改为:http://127.0.0.1:8024/CalculatorService.svc,Console的端口号是在ConsoleHost项目下的App.config中配置的,而IIS的端口号是搭建Web的时候,配置的。
运行结果如下:
标签:style blog http color io os ar 文件 数据
原文地址:http://www.cnblogs.com/tianxue/p/3975814.html