WCF的绑定方式有很多种,如BasicHttpBinding,WSHttpBinding,NetTcpBing
WS 开头的的邦定是独立于平台的,也就是说window,linux一样,支持Web服务规范,以Net开头绑定使用二进制格式,使.net应用程序之间通信有很高的性能。
其他的支持会话,可靠的会话,事物处理和双向通讯。
主机的绑定有两种方式 :代码绑定和配置文件绑定
代码绑定
//this will be used binding by code
internal static void StartServiceCode()
{
Uri baseAddress = new Uri("http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/");
myService = new ServiceHost(typeof(Service1));
WSHttpBinding binding = new WSHttpBinding();
myService.AddServiceEndpoint(typeof(IService1), binding, baseAddress);
myService.Open();
}
不需要配置config,路经 端口在代码里指定 ,
配置文件绑定
需要在config里配置system.serviceModel节点,其实这个节点只要从服务工程里复制出来放到这里就行了。主要端口号修改, 否则调试状态出错。
<system.serviceModel>
<services>
<service name="WcfServiceLibrary1.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
客户端的注册:三种方式
1、Visual Studio 添加服务引用,这个工具会从服务中的原数据中创建代理类。
2。ServiceModel元数 工具 Svcutil.exe创建,这个工具会以编程的方式从服务中读取数据v,创建代理类。
3。ChannelFactory类,就是和Remoting一样,需要引用广播出来的类。
1。 服务引用
右键-〉添加服务引用-〉 找到该服务-〉填写服务引用名 -〉确定
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
2。没有试,
3。添加服务项目,
WSHttpBinding binding = new WSHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/");
ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding, address);
IService1 channel = factory.CreateChannel();