http://technet.microsoft.com/zh-cn/magazine/ee126350(VS.100).aspx
http://hi.baidu.com/yyylajzwwfbegpe/item/1a426c0add1f596dd55a119f
http://support.microsoft.com/kb/899191
http://www.cnblogs.com/chnking/archive/2010/01/10/1643384.html
http://msdn.microsoft.com/zh-cn/library/cc949057.aspx
http://technet.microsoft.com/zh-cn/magazine/ms752261(VS.90).aspx
1.契约
/// <summary>
/// 服务契约
/// </summary>
[ServiceContract(SessionMode=SessionMode.Required)]
public partial interface IContract
{
[OperationContract(Name = "AddInfo")]
[TransactionFlow(TransactionFlowOption.Allowed)]
[FaultContract(typeof (WcfFaultInfo))]
bool Add(Info info);
}
2.服务端实现
/// <summary>
/// 服务
/// </summary>
[ServiceBehavior(ReleaseServiceInstanceOnTransactionComplete = false,
InstanceContextMode = InstanceContextMode.PerCall
, ConcurrencyMode = ConcurrencyMode.Multiple), ErrorHandlerBehavior]
public partial class Service:IContract
{
/// <summary>
/// 添加一条Info信息
/// </summary>
[OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
public bool Add(Info info)
{
//实现代码
}
}
2.1服务端的WEB.config
<system.serviceModel>
<services>
<service behaviorConfiguration="transactionBehavior" name="Service">
<endpoint address="net.tcp://localhost:1234/IContract" binding="netTcpBinding" bindingConfiguration="TcpBind" contract="IContract"/>
</service>
</services>
<behaviors>
<!--netTcpBinding-->
<serviceBehaviors>
<behavior name="transactionBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<serviceTimeouts transactionTimeout="00:02:00"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceThrottling maxConcurrentCalls="5000" maxConcurrentSessions="1000" maxConcurrentInstances="1000" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="TcpBind" closeTimeout="00:03:00" sendTimeout="00:03:00" openTimeout="00:03:00" maxBufferSize="2147483647" maxConnections="50" maxReceivedMessageSize="2147483647" transactionFlow="true" receiveTimeout="00:10:00" transactionProtocol="OleTransactions" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<reliableSession enabled="true" ordered ="true" inactivityTimeout="00:30:00" />
<security mode="None"></security>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
3.客户端实现
/// <summary>
/// 服务客户端
/// </summary>
internal partial class SyncHandle : ClientBase<IContract>,IContract
{
public bool Add(Info info)
{
//实现代码
try
{
return Channel.Add(info);
}
catch (FaultException<WcfFaultInfo> ex)
{
Abort();
ex.ExceptionHandle();
}
catch (FaultException ex)
{
Abort();
ex.ExceptionHandle();
}
catch (CommunicationException ex)
{
Abort();
LogFunc.AddLog(ex, "CommunicationException--");
}
catch (Exception ex)
{
Abort();
LogFunc.AddLog(ex, "客户端异常");
}
}
3.1客户端web.config
<system.serviceModel>
<client>
<endpoint behaviorConfiguration="tcpbehavior" address="net.tcp://localhost:1234/IContract" binding="netTcpBinding" bindingConfiguration="TcpBind" contract="IContract" name="EndPoint"/>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="tcpbehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="TcpBind" sendTimeout="00:03:00" maxBufferSize="2147483647" maxConnections="50" maxReceivedMessageSize="2147483647" transactionFlow="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<reliableSession enabled="true" ordered="true" inactivityTimeout="00:30:00"/>
<security mode="None"></security>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
3.2客户端调用
/// <summary>
/// 客户端
/// </summary>
public partial class SyncClient
{
private const string ENDPOINTNAME = "EndPoint"; //WCF终结点
/// <summary>
/// 添加一条信息
/// </summary>
/// <returns></returns>
public static bool Add(Info info)
{
var proxy = new SyncHandle(ENDPOINTNAME);
bool binfo;
try
{
info = proxy.Add(info);
}
//catch (FaultException<WcfFaultInfo> ex)
//{
// throw ex;
//}
//catch(Exception ex)
//{
// throw ex;
//}
finally
{
proxy.Close();
}
return binfo;
}
}