zoukankan      html  css  js  c++  java
  • [Programming WCF Services]Chapter 1. WCF Essentials

    1.配置文件方式设置EndPoint(ABC)

    1.1.基本配置

    <system.serviceModel>
        <services>
            <service name="MyNamespace.MyService">
                <endpoint address="http://localhost:8000/MyService" binding="wsHttpBinding" contract="MyNamespace.IMyContract"/>
            </service>
        </services>
    </system.serviceModel>

     

    1.2.为一个Service配置多个结点

    <service name="MyService">
        <endpoint address="http://localhost:8000/MyService" binding="wsHttpBinding" contract="IMyContract"/>
        <endpoint address="net.tcp://localhost:8001/MyService" binding="netTcpBinding" contract="IMyContract"/>
        <endpoint address="net.tcp://localhost:8002/MyService" binding="netTcpBinding" contract="IMyOtherContract"/>
    </service>

    1.3.基地址

    <system.serviceModel>
        <services>
            <service name="MyNamespace.MyService">
                <host>
                    <baseAddresses>
                        <add baseAddress="net.tcp://localhost:8001/"/>
                        <add baseAddress="http://localhost:8002/"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>

    1.4.使用绑定

    <system.serviceModel>
        <services>
            <service name="MyService">
                <endpoint address="net.tcp://localhost:8000/MyService" bindingConfiguration="TransactionalTCP" binding="netTcpBinding" contract="IMyContract"/>
                <endpoint address="net.tcp://localhost:8001/MyService" bindingConfiguration="TransactionalTCP" binding="netTcpBinding" contract="IMyOtherContract"/>
            </service>
        </services>
        <bindings>
            <netTcpBinding>
                <binding name="TransactionalTCP" transactionFlow="true"/>
            </netTcpBinding>
        </bindings>
    </system.serviceModel>

    默认绑定

    应用到所有没有直接指定bindingConfiguration的EndPoint。一种绑定类型BasicHttpBinding/netTcpBinding/wsHttpBinding/...只能有一个默认配置

    <netTcpBinding>
        <binding transactionFlow="true"/> <!--没有name属性-->
    </netTcpBinding>

    2.代码方式设置EndPoint

    2.1.基本方式

    ServiceHost host = new ServiceHost(typeof(MyService));
    Binding wsBinding = new WSHttpBinding();
    Binding tcpBinding = new NetTcpBinding();
    host.AddServiceEndpoint(typeof(IMyContract),wsBinding,
    "http://localhost:8000/MyService");
    host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,
    "net.tcp://localhost:8001/MyService");
    host.AddServiceEndpoint(typeof(IMyOtherContract),tcpBinding,
    "net.tcp://localhost:8002/MyService");
    host.Open();

    2.2.使用基地址

    Uri tcpBaseAddress = new Uri("net.tcp://localhost:8000/");
    ServiceHost host = new ServiceHost(typeof(MyService),tcpBaseAddress); 
    Binding tcpBinding = new NetTcpBinding();
    //Use base address as address
    host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,"");
    //Add relative address
    host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,"MyService");
    //Ignore base address
    host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,
    "net.tcp://localhost:8001/MyService");
    host.Open();

    2.3.使用绑定

    ServiceHost host = new ServiceHost(typeof(MyService)); //没有基地址
    NetTcpBinding tcpBinding = new NetTcpBinding();
    tcpBinding.TransactionFlow = true; //设置绑定属性
    host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,
    "net.tcp://localhost:8000/MyService");
    host.Open();

    使用下列构造函数,可以使用配置文件中的指定绑定配置来初始化绑定,或是使用空串来使用默认的绑定配置

    public class NetTcpBinding : Binding,...
    {
    public NetTcpBinding(string configurationName);
    //More members
    }

    3.默认EndPoint

    如果没有在配置文件和代码中提供终结点,WCF会生成自动生成默认终结点

    例如代码:

    [ServiceContract]
    interface IMyContract
    {...}
    [ServiceContract]
    interface IMyOtherContract
    {...}
    class MyService : IMyContract,IMyOtherContract
    {...}
    for this hosting code:
    Uri httpBaseAddress = new Uri("http://localhost:8000/");
    Uri tcpBaseAddress = new Uri("net.tcp://localhost:9000/");
    Uri ipcBaseAddress = new Uri("net.pipe://localhost/");
    ServiceHost host = new ServiceHost(typeof(MyService),
    httpBaseAddress,tcpBaseAddress,ipcBaseAddress);
    //在host open之前没有添加任何终结点
    host.Open();

    WCF自动生成的终结点如下:

    <service name = "MyService">
    <endpoint name = "BasicHttpBinding_IMyContract"
    address = "http://localhost:8000/"
    binding = "basicHttpBinding"
    contract = "IMyContract"
    />
    <endpoint name = "NetTcpBinding_IMyContract"
    address = "net.tcp://localhost:9000"
    binding = "netTcpBinding"
    contract = "IMyContract"
    />
    <endpoint name = "NetNamedPipeBinding_IMyContract"
    address = "net.pipe://localhost/"
    binding = "netNamedPipeBinding"
    contract = "IMyContract"
    />
    <endpoint name = "BasicHttpBinding_IMyOtherContract"
    address = "http://localhost:8000/"
    binding = "basicHttpBinding"
    contract = "IMyOtherContract"
    />
    <endpoint name = "NetTcpBinding_IMyOtherContract"
    address = "net.tcp://localhost:9000"
    binding = "netTcpBinding"
    contract = "IMyOtherContract"
    />
    <endpoint name = "NetNamedPipeBinding_IMyOtherContract"
    address = "net.pipe://localhost/"
    binding = "netNamedPipeBinding"
    contract = "IMyOtherContract"
    />
    </service>

    也可以通过AddDefaultEndpoints来添加默认结点点。有了这个方法,在已有非终结点的情况下也中添加默认终结点。

    public class ServiceHost : ...
    {
    public void AddDefaultEndpoints();
    //More members
    }

    3.1Protocol mapping

    在没有提供终结点的情况下,WCF根据基地址的scheme来推断所需要的绑定。这个就叫Protocol mapping。可以通过配置文件来改变Protocol mapping的行为:

    <system.serviceModel>
      <protocolMapping>
        <add
        scheme = "http"
        binding = "wsHttpBinding"
    />
      </protocolMapping>
    </system.serviceModel>

    还可以为绑定指定配置:

    <protocolMapping>
      <add
      scheme = "http"
      binding = "wsHttpBinding"
      bindingConfiguration = "..."
    />
    </protocolMapping>
  • 相关阅读:
    ES6小点心第二弹——底部浮现弹窗
    ES6小点心之通用弹窗
    从输入URL到页面加载的过程?如何由一道题完善自己的前端知识体系!
    【quickhybrid】如何实现一个Hybrid框架
    【开源】canvas图像裁剪、压缩、旋转
    优雅的H5下拉刷新【minirefresh】
    前端筑基篇(一)->ajax跨域原理以及解决方案
    钉钉开放与商业化团队前端大量招人
    AJAX请求真的不安全么?谈谈Web安全与AJAX的关系。
    【quickhybrid】iOS端的项目实现
  • 原文地址:https://www.cnblogs.com/vicsmb/p/5244472.html
Copyright © 2011-2022 走看看