WCF,请看下配置服务
一个WCF服务,使用控制台应用程序承载服务。
下面这段使用代码配置服务:
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/MyService");//①创建一个URI对象,用来保存服务的基址(监听地址)
ServiceHost selfHost = new ServiceHost(typeof(MyService), baseAddress); //②创建一个ServiceHost对象。也就是创建服务主机
try
{
selfHost.AddServiceEndpoint(typeof(IService),new WSHttpBinding(),"MyService"); //③将服务终结点添加到承载服务中
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();//④启用元数据交换
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb); //添加服务描述
selfHost.Open();//⑤启动服务
Console.WriteLine("服务已准备就绪");
Console.WriteLine("按下回车键以终止服务");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();//关闭ServiceHostBase以关闭服务
}
catch (CommunicationException ce)
{
Console.WriteLine("异常:{0}", ce.Message);
selfHost.Abort();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<!--配置服务和终结点-->
<services>
<service name="Services.MyService" behaviorConfiguration="MyBehavior">
<host><!--配置主机-->
<baseAddresses>
<add baseAddress="http://localhost:8000"/>
</baseAddresses>
</host>
<endpoint address="MyService" binding="wsHttpBinding" contract="Contracts.IService" bindingConfiguration="myHttpBinding">
</endpoint>
</service>
</services>
<!--配置绑定-->
<bindings>
<wsHttpBinding>
<binding name="myHttpBinding">
<security mode="None">
<message clientCredentialType="Windows" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<!--配置行为-->
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>