Has any one ever implemented a custom serializer in WCF ? The reason i want to replace the WCF default serializer with custom serializer is to call different services from the same wcf proxy client.I would be glad if some one can suggest a way to do this ?
问问题
807 次
2 回答
0
I did something similar on a project I recently did.
However I did have 2 different WCF clients. How I "switched" was I then created a shared interface between the clients and then used a ServiceLocator
to fetch the IClient
.
Does this make sense?
于 2013-09-10T16:08:24.383 回答
0
如果我正确理解了这个问题,那么您有一个应用程序,您想与基于某些标准使用相同接口的两个服务之一进行通信。这些服务具有不同的配置,因此您不能重复使用相同的配置。
为了解决这个问题,我将在应用程序配置中设置两个配置,如果您愿意,也可以在代码中完成。
<client>
<endpoint address="http://service1"
binding="basicHttpBinding"
bindingConfiguration="Service1Binding"
behaviorConfiguration="Service1Behavior"
contract="IServiceInterface, Service"
name="Service1"/>
<endpoint address="http://service2"
binding="basicHttpBinding"
bindingConfiguration="Service2Binding"
behaviorConfiguration="Service2Behavior"
contract="IServiceInterface, Service"
name="Service2"/>
</client>
然后,在您的代码中,您需要某种条件语句来确定您要与哪个服务通信。完成此操作后,您可以ChannelFactory
为所需的配置创建一个。
string serviceName = FullMoon ? "Service1" : "Service2";
var channelFactory = new ChannelFactory<IServiceInterface>(serviceName);
var proxy = channelFactory.CreateChannel();
proxy.SomeServiceCall();
channelFactory.Close();
如果您使用 IoC 注入代理,您可能需要将其推送到某种工厂。您还可以考虑优化这一点,因为创建 ChannelFactory 是昂贵的部分,可以创建 Factory 而无需仅指定合同的配置。然后,您需要在创建通道时指定绑定和端点。
于 2013-09-10T16:29:29.383 回答