wcf传输地址分为HTTP,TCP,Peer network(对等网)和MSMQ

地址通常采用的格式为:

[基地址]/[可选的URL]

基地址通常的格式为;

[传输协议]://[机器名或者域名][:可选端口]

可以讲地址http://localhost:8001读作:"采用HTTP协议访问loaclhost机器,并在8001端口等待调用"

如果URL为http://localhost:8001/MyService,则读作“采用HTTP协议访问localhost机器。MyService服务在端口8001等待调用”

简单例子如下:

添加wcf文件DService1

添加using System.ServiceModel;

两个控制台文件

Host3启动服务,客户端WebWCF1

===============================================================

定义契约

namespace DService1
{
// 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。
[ServiceContract]
publicinterface IService1
{
//放回时间
[OperationContract]
DateTime happy();


}
}

定义服务返回时间

namespace DService1
{
// 注意: 如果更改此处的类名“IService1”,也必须更新 App.config 中对“IService1”的引用。
publicclass Service1 : IService1
{
#region IService1 成员

public DateTime happy()
{
return DateTime.Now;
}

#endregion
}
}

服务端:

//代码绑定
using (ServiceHost host =new ServiceHost(typeof(DService1.Service1)))
{
//绑定
host.AddServiceEndpoint(typeof(DService1.IService1), new WSHttpBinding(), "http://localhost:7000");
//设置数据交换方式
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() ==null)
{
ServiceMetadataBehavior behavior
=new ServiceMetadataBehavior();
behavior.HttpGetEnabled
=true;
host.Description.Behaviors.Add(behavior);
}
host.Opened
+=delegate
{
Console.WriteLine(
"CalculaorService已经启动,按任意键终止服务!");
};
host.Open();
Console.Read();

}

如果要契约appconfig配置的话

则服务端首先配置配置文件

<configuration>
<system.serviceModel>
<services>
<!--name服务名;behaviorConfiguration数据绑定配置必须和下面的behavior name一致-->
<service name="DService1.Service1" behaviorConfiguration="serviceBehavior">
<!--endpoint终结点;binding绑定协议;contract契约名-->
<!--定义终节点-->
<!--binding 绑定类型,NetTcpBinding、WSDualHttpBinding、WSHttpBindingBase、BasicHttpBinding、NetNamedPipeBinding、NetPeerTcpBinding、MsmqBindingBase、NetPeerTcpBinding、WebHttpBinding、MailBindingBase、CustomBinding-->
<!--DuplexBinding 双工-->
<!--使用契约:<命名空间>.<接口名称>-->
<!--address="DService1" 或者address==“http://localhost:7000”-->
<endpoint binding="basicHttpBinding" contract="DService1.IService1" address="DService1"/>
<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:7000"/>
<add baseAddress="net.tcp://localhost:8000"/>
</baseAddresses>
</host>
</service>
</services>
<!--绑定配置-->
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

再将服务端代码改为:

ServiceHost host =new ServiceHost(typeof(DService1.Service1));
//host.AddServiceEndpoint(typeof(DService1.IService1), new NetTcpBinding(), "net.tcp://localhost:8008/Service1");
host.Open();

Console.WriteLine(
"Service open");
Console.ReadLine();

这样服务端都配置完成

客户端:

情况一:当不添加服务引用是可以使用代理来执行,此时上面服务端注释的

 host.AddServiceEndpoint(typeof(DService1.IService1), new NetTcpBinding(), "net.tcp://localhost:8008/Service1");

启用

再前段代码

//使用代理方法,注:此接口与服务端的接口必须一致
[ServiceContract]
publicinterface IService1
{
[OperationContract]
DateTime happy();
}

protectedvoid Button1_Click(object sender, EventArgs e)
{
//代理
IService1 Proxy = ChannelFactory<IService1>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8008/Service1"));

DateTime client
= Proxy.happy();
string s = Convert.ToString(client);
Response.Write(s);
}

情况二:

将服务引用过来

localhost.Service1Client Proxy =new WebWCF1.localhost.Service1Client();
DateTime client
= Proxy.happy();
string s = Convert.ToString(client);
Console.WriteLine(s);
Console.WriteLine(
"按ENTER键退出");
Console.ReadLine();
posted on 2011-04-28 15:14  高兴happy  阅读(337)  评论(0编辑  收藏  举报