Wcf 什么';这是ChannelFactory与lt;T>;和ClientBase<;T>;
你能给我解释一下ChannelFactory和ClientBase之间的区别吗?它们中哪一个更适合用于SelfHost客户机-服务器应用程序Wcf 什么';这是ChannelFactory与lt;T>;和ClientBase<;T>;,wcf,Wcf,你能给我解释一下ChannelFactory和ClientBase之间的区别吗?它们中哪一个更适合用于SelfHost客户机-服务器应用程序 using(ChannelFactory<MyInterface> cf = new ChannelFactory<MyInterface>("Endpoint From Web.Config")){} public class MyClient : ClientBase<MyInterface>, MyInterf
using(ChannelFactory<MyInterface> cf = new ChannelFactory<MyInterface>("Endpoint From Web.Config")){}
public class MyClient : ClientBase<MyInterface>, MyInterface {}
使用(ChannelFactory cf=newchannelfactory(“来自Web.Config的端点”){}
公共类MyClient:ClientBase,MyInterface{}
谢谢让我们有一个定义合同的界面:
[ServiceContract]
public interface TheInterface
{
[OperationContract]
string DoWork( string Work );
}
ChannelFactory
自动创建一个仅实现接口的代理
var factory = new ChannelFactory<TheInterface>( new BasicHttpBinding() );
var address = new EndpointAddress( "http://..." );
var client = factory.CreateChannel( address );
// there are no other methods on the "client" reference
// than the interface's DoWork method
client.DoWork( "foo" );
然后
var address = new EndpointAddress( "http://..." );
using ( var client = new TheInterfaceProxy( new BasicHttpBinding(), address ) )
{
// DoWork is here
// but multiple other members are there too
// for example - applying a custom endpoint behavior:
client.Endpoint.EndpointBehaviors.Add( new InspectorBehavior() );
client.DoWork( "bar" );
}
其中,使用示例简单行为检查客户端的传入/传出消息
class InspectorBehavior : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters( ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters )
{
}
public void ApplyClientBehavior( ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime )
{
clientRuntime.ClientMessageInspectors.Add( new DispatchInspector() );
}
public void ApplyDispatchBehavior( ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher )
{
}
public void Validate( ServiceEndpoint endpoint )
{
}
#endregion
}
class DispatchInspector : IClientMessageInspector
{
#region IClientMessageInspector Members
public void AfterReceiveReply( ref Message reply, object correlationState )
{
MessageBuffer buffer = reply.CreateBufferedCopy( Int32.MaxValue );
reply = buffer.CreateMessage();
Console.WriteLine( "Receiving:\n{0}", buffer.CreateMessage().ToString() );
}
public object BeforeSendRequest( ref Message request, IClientChannel channel )
{
MessageBuffer buffer = request.CreateBufferedCopy( Int32.MaxValue );
request = buffer.CreateMessage();
Console.WriteLine( "Sending:\n{0}", buffer.CreateMessage().ToString() );
return null;
}
#endregion
}
但是,没有哪个答案更好:
- 如果您不需要任何东西,只需要默认行为-您可以坚持使用
,这是两种方法中最简单的一种ChannelFactory
- 对于任何额外的内容,请使用
,但它需要一个额外的类ClientBase
client.Endpoint.EndpointBehaviors.Add(new-InspectorBehavior())
将检查器添加到交互代理的行为中。使用channel factory创建的代理无法执行此操作。您好,谢谢。我现在知道ChannelFactory和ClientBase编程的区别了。但是,异步运行、切换、资源消耗和速度等其他差异又如何呢?这两个类是基于Rest还是SOAP的?在我看来,这些附加问题远远超出了原始问题的范围。考虑接受这个答案,并提出另外一些具体的问题来解决你的担忧。请注意,被认为“过于宽泛”的问题可能会被社区搁置。您好,Wiktor,对不起,但这些是我关于ChannelFactory和ClientBase之间差异的真正问题。当做
class InspectorBehavior : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters( ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters )
{
}
public void ApplyClientBehavior( ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime )
{
clientRuntime.ClientMessageInspectors.Add( new DispatchInspector() );
}
public void ApplyDispatchBehavior( ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher )
{
}
public void Validate( ServiceEndpoint endpoint )
{
}
#endregion
}
class DispatchInspector : IClientMessageInspector
{
#region IClientMessageInspector Members
public void AfterReceiveReply( ref Message reply, object correlationState )
{
MessageBuffer buffer = reply.CreateBufferedCopy( Int32.MaxValue );
reply = buffer.CreateMessage();
Console.WriteLine( "Receiving:\n{0}", buffer.CreateMessage().ToString() );
}
public object BeforeSendRequest( ref Message request, IClientChannel channel )
{
MessageBuffer buffer = request.CreateBufferedCopy( Int32.MaxValue );
request = buffer.CreateMessage();
Console.WriteLine( "Sending:\n{0}", buffer.CreateMessage().ToString() );
return null;
}
#endregion
}