标签: endpointbehavior

在编程构造的端点上声明性地配置WCF行为扩展

我有一个WCF行为扩展,我想添加到WCF客户端.但是,客户端是以编程方式构建的.端点地址可能会有所不同,但我知道类型.我可以通过编程方式或在配置文件中添加行为(首选),但我只需要在配置文件中传递一些配置.

我不想在Common行为(machine.config)中使用它.

我可以通过编程方式添加行为

endpoint.Behaviors.Add(new MyCustomBehavior())
Run Code Online (Sandbox Code Playgroud)

但我宁愿在配置中这样做,所以我也可以在那里配置扩展.

是否可以声明性地添加和配置端点行为扩展到编程构造的端点,只知道类型或接口,同时保留客户端端点以编程方式构造?

<system.serviceModel>
  <client>
    <!-- Created programmatically -->
  </client>
<extensions>
  <behaviorExtensions>
    <add name="MyCustomBehavior" type="namespace.CustomBehaviors", MyAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </behaviorExtensions>   
</extensions>
  <behaviors>
    <endpointBehaviors>
      <behavior name="MyCustomBehavior">
        <MyCustomBehavior MyImportantBehaviorParam1="foo"  />
      </behavior>
    </endpointBehaviors>   
  </behaviors>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

当然我可以把配置放在另一个部分,并让我的行为在那里阅读,但如果可能的话我宁愿使用WCF设施.

wcf app-config endpointbehavior

7
推荐指数
1
解决办法
9620
查看次数

每个端点的不同服务行为

情况

我们正在某些WCF服务上实现不同类型的安全性.ClientCertificate,UserName&Password和Anonymous.

我们有2个ServiceBehaviorConfigurations,一个用于httpBinding,另一个用于wsHttpBinding.(我们有基于声明的安全性的自定义授权策略)作为一项要求,我们需要为每项服务提供不同的端点.带有httpBinding的3个端点和带有wsHttpBinding的1个端点.

一项服务的示例:

  • basicHttpBinding:匿名
  • basicHttpBinding:UserNameAndPassword
  • basicHttpBinding:BasicSsl
  • wsHttpBinding:BasicSsl

注意:我们正在开发.NET 3.5

问题

第1部分:我们不能两次指定相同的服务,一次使用http服务配置,一次使用wsHttp服务配置.

第2部分:我们无法在端点上指定服务行为.(抛出和异常,未找到端点行为...服务行为无法设置为端点行为)

配置

第1部分:

<services>
  <service name="Namespace.MyService" behaviorConfiguration="securityBehavior">
   <endpoint address="http://server:94/MyService.svc/Anonymous" contract="Namespace.IMyService" binding="basicHttpBinding" bindingConfiguration="Anonymous">
    </endpoint> 
    <endpoint address="http://server:94/MyService.svc/UserNameAndPassword" contract="Namespace.IMyService" binding="basicHttpBinding" bindingConfiguration="UserNameAndPassword">
    </endpoint>
    <endpoint address="https://server/MyService.svc/BasicSsl" contract="Namespace.IMyService" binding="basicHttpBinding" bindingConfiguration="BasicSecured">
    </endpoint>
  </service>
  <service name="Namespace.MyService" behaviorConfiguration="wsHttpCertificateBehavior">
    <endpoint address="https://server/MyService.svc/ClientCert" contract="Namespace.IMyService" binding="wsHttpBinding" bindingConfiguration="ClientCert"/>
  </service>
</services>
Run Code Online (Sandbox Code Playgroud)

服务行为配置:

<serviceBehaviors>
<behavior name="securityBehavior">
  <serviceAuthorization serviceAuthorizationManagerType="Namespace.AdamAuthorizationManager,Assembly">
    <authorizationPolicies>
      <add policyType="Namespace.AdamAuthorizationManager,Assembly" />
    </authorizationPolicies>
  </serviceAuthorization>
</behavior>
<behavior name="wsHttpCertificateBehavior">
  <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
  <serviceAuthorization serviceAuthorizationManagerType="Namespace.AdamAuthorizationManager,Assembly">
    <authorizationPolicies>
      <add policyType="Namespace.AdamAuthorizationManager,Assembly" />
    </authorizationPolicies>
  </serviceAuthorization>
  <serviceCredentials>
    <clientCertificate> …
Run Code Online (Sandbox Code Playgroud)

wcf claims-based-identity servicebehavior endpointbehavior

7
推荐指数
1
解决办法
4810
查看次数

使用WCF和Autofac的自定义端点行为

我正在尝试实现如下所示的UoW:http://blog.iannelson.systems/wcf-global-exception-handling/

但我不能为我的生活弄清楚如何用Autofac连接它.我完全不知道从哪里开始.

使用http://autofac.readthedocs.org/en/latest/integration/wcf.html我使用Autofac让WCF正常工作

但要注入或添加IEndpointBehavior?不知道...

如果有更好的方法来实现UoW,我想听听.

编辑:

现在我刚刚完成了:

builder.RegisterType(typeof (UnitOfWork))
    .As(typeof (IUnitOfWork))
    .InstancePerLifetimeScope()
    .OnRelease(x =>
    {
        Trace.WriteLine("Comitted of UoW");
        ((IUnitOfWork) x).Commit();
        // OnRelease inhibits the default Autofac Auto-Dispose behavior so explicitly chain to it
        x.Dispose(); 
    });
Run Code Online (Sandbox Code Playgroud)

虽然我不知道这是否是一种可以接受的方式,但看起来像是黑客:(

EDIT2:

似乎不可能在WCF中运行UoW:/

编辑3:

我在这里发布了我的解决方案:http://www.philliphaydon.com/2011/11/06/unit-of-work-with-wcf-and-autofac/

wcf unit-of-work autofac endpointbehavior

5
推荐指数
1
解决办法
1490
查看次数

WCF在多个客户端之间共享IClientMessageInspector实例

我在通过此处的方法" 集中式cookie管理 "中概述的方法进行WCF服务调用时管理共享的auth cookie :http://megakemp.com/2009/02/06/managing-shared-cookies-in- WCF /

我已经建立了一个自定义的IClientMessageInspector,IEndpointBehavior,BehaviorExtensionElement,的作品.我的端点行为添加了一个消息检查器,如下所示:

public class MyEndpointBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }
    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        // yuck.. Wish I had an instance of MyClientMessageInspector
        // (which has the auth cookie already) so I could just inject that
        // instance here instead of creating a new instance
        clientRuntime.MessageInspectors.Add(new MyClientMessageInspector());
    }
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
    } …
Run Code Online (Sandbox Code Playgroud)

structuremap wcf dependency-injection wcf-client endpointbehavior

3
推荐指数
1
解决办法
3053
查看次数