WCF tips

Posted Silly Programer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WCF tips相关的知识,希望对你有一定的参考价值。

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

基本配置

 

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

 

 

为一个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>

 

 

基地址

<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>

 

使用绑定

<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>

 

代码方式设置EndPoint

 

基本方式

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

 

 

使用基地址

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

 

 

使用绑定

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
}

 

以上是关于WCF tips的主要内容,如果未能解决你的问题,请参考以下文章

始终在 WCF 服务中返回带有 Json 的 XML 字符串

pandas GroupBy上的方法apply:一般性的“拆分-应用-合并”

Halo2学习笔记——用户手册之Tips and tricks

工具向IntelliJ IDEA 探路

final cut 素材的出入点无法移动,并在提示tips显示:v1媒体限制。请问如何解决?〔急!〕

[转]使用代码去描述WCF配置文件