C# WCF-忽略客户端行为
我和我的WCF客户有点麻烦。 我已经配置了endpointBehaviors,但是它没有加载到Servicehost中(但是绑定已加载…) 配置:C# WCF-忽略客户端行为,c#,wcf,service,C#,Wcf,Service,我和我的WCF客户有点麻烦。 我已经配置了endpointBehaviors,但是它没有加载到Servicehost中(但是绑定已加载…) 配置: <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="DefaultBehavior"> <dataContractSerializer m
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="DefaultBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="DefaultBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="..." behaviorConfiguration="DefaultBehavior" binding="netTcpBinding" bindingConfiguration="DefaultBinding" contract="..." name="..."/>
<endpoint address="..." behaviorConfiguration="DefaultBehavior" binding="netTcpBinding" bindingConfiguration="DefaultBinding" contract="..." name="..."/>
</client>
</system.serviceModel>
ServiceWrapper类:
public class ServiceWrapper<T> : IDisposable where T : class
{
private ChannelFactory<T> factory;
private T channel;
private readonly NetTcpBinding binding;
private readonly EndpointAddress endpoint;
private readonly object lockObject = new object();
private bool disposed;
public ServiceWrapper(string configName)
{
ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
ChannelEndpointElementCollection endpointCollection = clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection;
foreach (ChannelEndpointElement endpointElement in endpointCollection)
{
if (endpointElement.Name != configName)
continue;
binding = new NetTcpBinding(endpointElement.BindingConfiguration);
endpoint = new EndpointAddress(endpointElement.Address);
disposed = false;
}
if (endpoint == null)
throw new ConfigurationErrorsException(configName + " is not present in the config file");
}
public T Channel
{
get
{
if (disposed)
throw new ObjectDisposedException("Resource ServiceWrapper<" + typeof(T) + "> has been disposed");
lock (lockObject)
{
if (factory == null)
{
factory = new ChannelFactory<T>(binding, endpoint);
channel = factory.CreateChannel();
}
}
return channel;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
lock (lockObject)
{
if (channel != null)
((IClientChannel)channel).Close();
if (factory != null)
factory.Close();
}
channel = null;
factory = null;
disposed = true;
}
}
}
}
公共类ServiceWrapper:IDisposable其中T:class
{
私营渠道工厂;
私人T频道;
私有只读NetTcpBinding;
私有只读端点地址端点;
私有只读对象lockObject=新对象();
私人住宅;
公共服务包装器(字符串configName)
{
ClientSection ClientSection=ConfigurationManager.GetSection(“system.serviceModel/client”)作为ClientSection;
ChannelEndpointElementCollection endpointCollection=clientSection.ElementInformation.Properties[string.Empty]。值为ChannelEndpointElementCollection;
foreach(endpointCollection中的ChannelEndpointElement endpointElement)
{
if(endpointElement.Name!=configName)
继续;
binding=新的nettcppbinding(endpointElement.BindingConfiguration);
端点=新的端点地址(endpointElement.Address);
已处理=错误;
}
if(端点==null)
抛出新的ConfigurationErrorsException(配置文件中不存在configName+);
}
公共T频道
{
得到
{
如果(已处置)
抛出新的ObjectDisposedException(“ResourceServiceWrapper已被释放”);
锁定(锁定对象)
{
如果(工厂==null)
{
工厂=新通道工厂(绑定,端点);
channel=factory.CreateChannel();
}
}
返回通道;
}
}
公共空间处置()
{
处置(真实);
总干事(本);
}
私有无效处置(bool处置)
{
如果(!已处置)
{
如果(处置)
{
锁定(锁定对象)
{
如果(通道!=null)
((IClientChannel)通道).Close();
如果(工厂!=null)
工厂关闭();
}
通道=空;
factory=null;
这是真的;
}
}
}
}
我真的不知道如何继续前进。有人对如何使它工作有想法吗
thx尝试将与端点配置名称一起使用。它将简化您的代码并加载所有行为。行为在ChannelFactory(Factory.Endpoint.Behaviors)中,而不是在绑定本身中