WCF步步为营(二):使用配置文件改变发布服务的方式
2008-07-02 17:57 敏捷的水 阅读(868) 评论(0) 编辑 收藏 举报1. 打开上一个项目
2. 修改Program.cs, 注意注释掉的绿色部分,因为我们使用配置文件公布EndPoint
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JackWangWCFService;
using System.ServiceModel;
namespace JackWangServiceHost
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host=new ServiceHost(typeof(JackWangWCFService.Calc)))
{
//host.AddServiceEndpoint(typeof(JackWangWCFService.ICalc),
// new NetTcpBinding(), "net.tcp://localhost:9000/Add");
host.Open();
Console.Out.WriteLine("calc web service started at:"+DateTime.Now.ToString());
Console.ReadLine();
}
}
}
}
3.添加一个App.Config文件,注意Service 的name是实现服务的类,客户端访问的地址是http://localhost:9000/Add
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="JackWangWCFService.Calc" behaviorConfiguration="serviceBehavior">
<endpoint binding="basicHttpBinding" contract="JackWangWCFService.ICalc" address="Add"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9000"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
4.修改客户端的代码,注意这次使用的是http的传输方式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace JackWangServiceClient
{
[ServiceContract]
public interface ICalc
{
[OperationContract]
long Add(int a, int b);
}
class Program
{
static void Main(string[] args)
{
ICalc proxy = ChannelFactory<ICalc>.CreateChannel(new BasicHttpBinding(),
new EndpointAddress("http://localhost:9000/Add"));
long result = proxy.Add(50, 60);
Console.Out.WriteLine("result from server is:" + result);
Console.ReadLine();
}
}
}
扫码关注公众号,了解更多管理,见识,育儿等内容
出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。