webabcd - 专注于asp.net

ASP.NET
从现在开始 一切都不晚
posts - 150, comments - 4182, trackbacks - 344, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理
[索引页]
[源码下载]


化零为整WCF(3) - 绑定Binding(basicHttpBinding和netTcpBinding)


作者:webabcd


介绍
WCF(Windows Communication Foundation) - 绑定Binding:Http以basicHttpBinding为例,Tcp以netTcpBinding为例。


示例
1、服务
IHello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.Binding
{
    
/// <summary>
    
/// IHello接口
    
/// </summary>

    [ServiceContract]
    
public interface IHello
    
{
        
/// <summary>
        
/// 打招呼方法
        
/// </summary>
        
/// <param name="name">人名</param>
        
/// <returns></returns>

        [OperationContract]
        
string SayHello(string name);
    }

}

Hello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.Binding
{
    
/// <summary>
    
/// Hello类
    
/// </summary>

    public class Hello : IHello
    
{
        
/// <summary>
        
/// 打招呼方法
        
/// </summary>
        
/// <param name="name">人名</param>
        
/// <returns></returns>

        public string SayHello(string name)
        
{
            
return "Hello: " + name;
        }

    }

}


2、宿主
Hello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceHost2.Binding
{
    
class Hello
    
{
        
static void Main(string[] args)
        
{
            
using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Binding.Hello)))
            
{
                
// 写代码的方式做host
                
// host.AddServiceEndpoint(typeof(WCF.ServiceLib.Binding.IHello), new NetTcpBinding(), "net.tcp://localhost:54321/Binding/Hello");
                host.Open();

                Console.WriteLine(
"服务已启动");
                Console.WriteLine(
"按<ENTER>停止服务");
                Console.ReadLine();
            }

        }

    }

}

App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.serviceModel>
    
<services>
      
<!--name - 提供服务的类名-->
      
<!--behaviorConfiguration - 指定相关的行为配置-->
      
<service name="WCF.ServiceLib.Binding.Hello" behaviorConfiguration="BindingBehavior">
        
<!--address - 服务地址-->
        
<!--binding - 通信方式-->
        
<!--contract - 服务契约-->
        
<!--<endpoint binding="basicHttpBinding" contract="WCF.ServiceLib.Binding.IHello" address="Hello" />-->
        
<endpoint binding="netTcpBinding" contract="WCF.ServiceLib.Binding.IHello" address="net.tcp://localhost:54321/Binding/Hello" />
        
<!--元数据交换的endpoint-->
        
<!--注:address是mex,它会和host/baseAddresses节点中的baseAddress做拼接,即提供元数据交换的地址为:http://localhost:12345/Binding/mex-->
        
<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />
        
<host>
          
<baseAddresses>
            
<add baseAddress="http://localhost:12345/Binding/"/>
          
</baseAddresses>
        
</host>
      
</service>
    
</services>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="BindingBehavior">
          
<!--httpGetEnabled - 使用get方式提供服务-->
          
<serviceMetadata httpGetEnabled="true" />
        
</behavior>
      
</serviceBehaviors>
    
</behaviors>
  
</system.serviceModel>
</configuration>


3、客户端
Hello.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.ServiceModel;

namespace Client2.Binding
{
    
public partial class Hello : Form
    
{
        
public Hello()
        
{
            InitializeComponent();
        }


        
private void btnSayHello_Click(object sender, EventArgs e)
        
{
            
// 写代码的方式做client
            
// IHello proxy = ChannelFactory<IHello>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:54321/Binding/Hello"));

            Binding.HelloClient proxy 
= new Binding.HelloClient();

            MessageBox.Show(proxy.SayHello(txtName.Text));

            proxy.Close();
        }

    }

}


App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.serviceModel>
    
<client>
      
<!--address - 服务地址-->
      
<!--binding - 通信方式-->
      
<!--contract - 服务契约-->
      
<!--<endpoint address="http://localhost:12345/Binding/Hello" binding="basicHttpBinding" contract="Binding.IHello" />-->
      
<endpoint address="net.tcp://localhost:54321/Binding/Hello" binding="netTcpBinding" contract="Binding.IHello" />
    
</client>
  
</system.serviceModel>
</configuration>


运行结果:
单击"Hello"按钮后弹出提示框,显示"Hello: webabcd"


OK
[源码下载]

Feedback

#1楼    回复  引用    

2007-12-27 21:14 by o0myself0o [未注册用户]
一直关注你的系列~

#2楼 [楼主]   回复  引用  查看    

2007-12-28 08:58 by webabcd      
@o0myself0o
:)
多谢支持

#3楼    回复  引用    

2007-12-28 10:03 by ivw [未注册用户]
支持。。。。
请问,我在用innerHTML添加一个IMG的时候为什么他会自动在前加上域名啊?
如<img src="aa.gif" />
用innerHTML赋值后就变成了
<img src="http://localhost/aa.gif" />
有什么办法可以不给它这样转换呢?

#4楼 [楼主]   回复  引用  查看    

2007-12-28 12:41 by webabcd      
@ivw
其实是浏览器解析的时候把它转换的
代码里其实还是<img src="aa.gif" />

#5楼    回复  引用    

2007-12-28 13:26 by ivw [未注册用户]
但如果我直接用control.innerHTML的方法获取它就只能取到解析后的值了。拿不回原来的<img src="aa.gif" /> 了

#6楼 [楼主]   回复  引用  查看    

2007-12-28 15:53 by webabcd      
@ivw
默认情况下,浏览器会使用当前页面的url来解析相对路径
你可以在页面中使用base元素,用指定的一个基准来解析相对路径

不过肯定是要解析的
要想拿回原来的值,就要替换一下

#7楼    回复  引用    

2007-12-28 16:12 by ivw [未注册用户]
现在就是这样了,在取值时就要用正则替换才行。真是麻烦。

#8楼 [楼主]   回复  引用  查看    

2007-12-28 22:33 by webabcd      
@ivw
:)
是啊

#9楼    回复  引用  查看    

2007-12-31 22:47 by Clingingboy      
元旦快乐,感谢你写这么多的好文章与大家分享:)

#10楼 [楼主]   回复  引用  查看    

2008-01-02 08:02 by webabcd      
@Clingingboy
:)
元旦快乐

过节过得好累啊,感觉工作要比过节轻松多了

#11楼    回复  引用    

2008-01-03 21:44 by 黑白 [未注册用户]
好久没来了
支持一下

#12楼 [楼主]   回复  引用  查看    

2008-01-03 22:10 by webabcd      
@黑白
:)
多谢支持

#13楼    回复  引用    

2008-06-05 00:13 by fooler [未注册用户]
简单明了!Thank you very much!

#14楼 [楼主]   回复  引用  查看    

2008-06-05 08:21 by webabcd      
@fooler
:)
不谢

#15楼    回复  引用  查看    

2008-06-05 11:18 by Howard Queen      
上篇是通过 http,该篇是通过 tcp。
Howard 到此一读。

#16楼 [楼主]   回复  引用  查看    

2008-06-05 14:55 by webabcd      
@Howard Queen
:)
是的,而且实现起来很简单,只要修改配置即可

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-07-24 09:03 编辑过


相关链接: