【发布时间】:2015-09-20 09:16:44
【问题描述】:
我正在尝试将一些现有的 WCF 扩展代码移植到针对 iO、WinPhone 和 Android 的 Xamarin 应用程序中。该代码目前存在于 Windows 桌面应用程序中,没有任何问题。
基本上,所有代码所做的只是捕获每个传出的 WCF 服务调用并附加一些 http 标头,然后捕获响应并读取已返回的任何标头。这是使用代码中的自定义行为和 IClientMessageInspector 实现的,并在我现有的 Windows 桌面应用程序的 app.config 中进行配置:
<behaviors>
<endpointBehaviors>
<behavior>
<MyBehavior ConsumerKey="878846DF-2E7B-4165-8542-9F68583DD7D4" ConsumerType="Windows Forms" />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="MyBehavior" type="MyBehavior.Objects.ServiceLayer.WCFExtensions.ConsumerBehaviorElement, MyBehavior.Objects.ForConsumers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
不幸的是,我的 Xamarin 项目中似乎没有 app.config,因此开始考虑通过代码手动添加行为。我已经看到很多例子展示了:
myServiceClient.Enpoint.Behaviors.Add(…)
但是,我似乎无法从 Xamarin 应用中的 App.cs 访问我的 Endpoint (System.ServiceMode.Description.ServiceEndpoint) 的 Behaviors 属性。我只能访问地址、绑定、合同和名称。
谁能指出我正确的方向。如上所述,我要做的就是将一些标头附加到每个 WCF 服务调用,并在响应中读取一些标头。
更多信息:
调用我的WCF服务的例子如下:
private void CallService()
{
MyAuthenticationService.AuthenticationServiceClient _ws = new MyAuthenticationService.AuthenticationServiceClient();
_ws.AuthenticateCompleted += _ws_AuthenticateCompleted;
_ws.AuthenticateAsync(new MyAuthenticationService.AuthenticationRequest1());
}
private void _ws_AuthenticateCompleted(object sender, MyAuthenticationService.AuthenticateCompletedEventArgs e)
{
throw new NotImplementedException();
}
最后,我的 Xamarin 项目的目标是:
- .Net Framework 4.5
- Windows 8
- Windows Phone Silverlight 8
- Xamarin.Android
- Xamarin.iOs
- Xamarin.iOS(经典)
谢谢 大卫
【问题讨论】:
-
我已经阅读了这篇文章。这篇文章非常好。但它只解释了如何在 WCF 请求中添加自定义标头。但是您在问题中提到您还需要“捕获响应并读取已返回的任何标头。”。所以,我想知道你怎么能做到这一点?
标签: wcf xamarin xamarin.ios xamarin.forms