WCF的主机的邦定和客户端的注册

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();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值