C# - Castle WCF工具 - 如何正确设置客户端OperationBehavior + WcfEndpoint.FromEndpoint用法

时间:2015-01-03 17:09:13

标签: c# castle-windsor castle wcffacility

我正在玩wcf设施并尝试设置DataContractResolver,但我找不到任何示例...

最后我让它发挥作用..不确定这是否正常..

问题

  1. (下面的代码)这是完成行为配置的正确方法还是我误解了什么?

  2. 也..我不得不禁用异步使它工作..这是一个库bug /问题吗?

  3. 其他人在研究之前很高兴。

    1. 我真的在考虑wcf设施拦截对性能的影响..与使用它的好处。有什么想法吗?

    2. 为什么这个库不再更新,(http://docs.castleproject.org/Windsor.AllPages.aspx?Cat=Windsor.WCF-Facility)我的问题被认为是使用不再需要维护的库?


    3.         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是de fault configuration)所以这里..

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

      可以看出上面没有复制契约或操作行为。

      就这样,谢谢。

0 个答案:

没有答案