客户端CustomMessageInspector,如何使这段代码在exe文件中工作?

时间:2012-03-01 08:00:35

标签: c# .net wcf interceptor

您好,

我在服务端运行CustomMessageInspector,现在我需要在客户端进行同样的操作。然而,在客户端中没有以相同的方式实现。我找到了这段代码:

https://gist.github.com/1076635

但是我很难在winform项目中运行,我是否真的必须将它放在类库中才能使它工作?

BestRegards

编辑1:

这就是我在客户端实现服务的方式。

    ClientService clientService = new ClientService();
    InstanceContext context = new InstanceContext(clientService);

    DuplexChannelFactory<MyApp.ServiceContracts.IMyAppClientService> factory = new DuplexChannelFactory<MyApp.ServiceContracts.IMyAppClientService>(context, connectionName);
    factory.Credentials.UserName.UserName = anvandarNamn;
    factory.Credentials.UserName.Password = password;
    return factory.CreateChannel();

这就是我当前的行为标记在app.config中的样子:

<behaviors>
  <endpointBehaviors>
    <behavior name="BasicBehavior">
      <CustomMessageInspector/>
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

1 个答案:

答案 0 :(得分:0)

<强>被修改

ClientService clientService = new ClientService();

clientService.Endpoint.Behaviors.Add(new YourBehaviorThatApplysYourExtension());

InstanceContext context = new InstanceContext(clientService);

DuplexChannelFactory<MyApp.ServiceContracts.IMyAppClientService> factory = new 
   DuplexChannelFactory<MyApp.ServiceContracts.IMyAppClientService>(context);
factory.Credentials.UserName.UserName = anvandarNamn;
factory.Credentials.UserName.Password = password;
return factory.CreateChannel();