我正在使用 wcf 工具并尝试设置 DataContractResolver 但我找不到任何示例...
最后我让它工作..虽然不确定这是否可以..
问题
(下面的代码)这是完成行为配置的正确方法还是我误解了什么?
也..我必须禁用异步才能使其工作..这是一个库错误/问题吗?
其他人很高兴在研究之前拥有..
我真的在考虑 wcf 设施拦截的性能影响.. 与使用它的好处。有什么想法吗?
为什么这个库不再更新,(http://docs.castleproject.org/Windsor.AllPages.aspx?Cat=Windsor.WCF-Facility)我担心使用不再维护的库?
var contractdesc = ContractDescription.GetContract(typeof(T));
if (dcr != null)
{
foreach (var operation in contractdesc.Operations)
{
operation.Behaviors.Find<DataContractSerializerOperationBehavior>().DataContractResolver = dcr;
}
}
var myEndpoint = WcfEndpoint.FromEndpoint(
new System.ServiceModel.Description.ServiceEndpoint(
contractdesc
, binding, new EndpointAddress(Url)));
var clientModel = new DefaultClientModel { Endpoint = myEndpoint };
clientModel.WithoutAsyncCapability();
container
.Register(
Types
.From(typeof(T))
.InSameNamespaceAs<T>()
.If(m => m.IsInterface)
.Configure(
c => c.Named(c.Implementation.Name)
.AsWcfClient(
clientModel
).Interceptors(typeof(CastleServiceInterceptor))
),
Component.For<CastleServiceInterceptor>()
);
所以我意识到(调试 wcf 工具)如果 WantsAsyncCapability=true DefaultClientModel 不会复制所有 ServiceEndpoint 行为,只是端点行为(true 是默认配置)所以在这里..
public override ChannelFactory CreateChannelFactory(Type channelFactoryType, M clientModel,
params object[] constructorArgs)
{
if (!clientModel.WantsAsyncCapability)
{
return base.CreateChannelFactory(channelFactoryType, clientModel, constructorArgs);
}
EnsureValidChannelFactoryType(channelFactoryType);
ReplaceServiceEndpointAsyncContracts(constructorArgs);
var interceptor = new CreateDescriptionInterceptor();
var proxyOptions = new ProxyGenerationOptions(asyncChannelFactoryProxyHook);
return (ChannelFactory)generator.CreateClassProxy(
channelFactoryType, Type.EmptyTypes, proxyOptions, constructorArgs, interceptor);
}
然后在 ReplaceServiceEndpointAsyncContracts 正在重新创建 ServiceEndpoint
private static void ReplaceServiceEndpointAsyncContracts(object[] constructorArgs)
{
for (int i = 0; i < constructorArgs.Length; ++i)
{
var endpoint = constructorArgs[i] as ServiceEndpoint;
if (endpoint != null)
{
var asyncEndpoint = new ServiceEndpoint(ContractDescription.GetContract(
AsyncType.GetAsyncType(endpoint.Contract.ContractType)))
{
Name = endpoint.Name,
Address = endpoint.Address,
Binding = endpoint.Binding,
ListenUri = endpoint.ListenUri,
ListenUriMode = endpoint.ListenUriMode
};
asyncEndpoint.Behaviors.Clear();
foreach (var behavior in endpoint.Behaviors)
{
asyncEndpoint.Behaviors.Add(behavior);
}
constructorArgs[i] = asyncEndpoint;
}
}
}
上图可以看出,不是契约或操作行为被复制。
就是这样,谢谢。