【问题标题】:Making a WCF service Android compatible使 WCF 服务与 Android 兼容
【发布时间】:2011-05-29 07:57:43
【问题描述】:

我有一个使用 IIS 托管在远程服务器上的 WCF 服务。我想让这项服务与 android 兼容,这样我就可以在 android 客户端上使用该服务。我遇到的问题是我正在尝试使用 ksoap2 连接到服务,但每次尝试调用该方法时都会出现 400 错误。我已经使用 fiddler2 来尝试查看问题所在,除了 400 错误之外,它还发送回 content-length: 0 (不知道这是否是原因。有趣的事实是,当我指定内容时-length 在提琴手的请求中,它不会给我 400 错误,而是根本不返回响应)我已经尝试将绑定更改为 webHttpBinding 而不是 wsHttpBinding 并在方法上方添加 WebGet 属性使用 URITemplate、BodyStyle 等,但没有运气。这是我的代码:

服务

    public class PublicService : IPublicService
{
    public Wallpaper[] GetWallpapers()
    {
        //return _wallpaperRepository.Items.ToArray();
        return new Wallpaper[]{ new Wallpaper()
                                    {
                                        Id = 10
                                    }};
    }
}

界面

[ServiceContract(Namespace = "http://XXXXXXXX.com/ServiceA")]
public interface IPublicService
{
    [OperationContract]
    Wallpaper[] GetWallpapers();
}

Web.Config

    <bindings>
            <wsHttpBinding>
<binding name="android" 
maxReceivedMessageSize="4097152" 
maxBufferPoolSize="4097152">
                    <readerQuotas 
maxStringContentLength="4097152" 
maxArrayLength="4097152" 
maxBytesPerRead="4097152" 
maxNameTableCharCount="4097152" 
maxDepth="4097152"/>
                </binding>
            </wsHttpBinding>
        </bindings>
        <services>
            <service name="Service.Concrete.PublicService" behaviorConfiguration="ServiceBehavior">
                <endpoint address="PublicService" binding="wsHttpBinding" name="PublicService" contract="Service.Abstract.IPublicService" bindingConfiguration="android"/>
                <endpoint binding="mexHttpBinding" bindingConfiguration="" name="mex" contract="IMetadataExchange"/>
            </service>

在过去的几个小时里,我一直在用头撞墙,并通过无数次谷歌搜索,我完全不知道下一步该做什么。如果有人知道我做错了什么,任何建议将不胜感激。

【问题讨论】:

  • 最好避免使用 KSOAP2 以获得与 android 的最大可比性。 Google android 官方不支持 SOAP。 KSOAP2 可能有效,但它不是商业 Android 应用程序的正确解决方案。

标签: android wcf http


【解决方案1】:

请改用basicHttpBinding。您可以使用默认配置进行尝试:

<endpoint address="PublicService" binding="basicHttpBinding" name="PublicService" contract="Service.Abstract.IPublicService" />

在您的 android 服务中,确保您指定了正确的 SOAP 操作、正确的命名空间,并且 SoapSerializationEnvelope 是为 SOAP 1.1 构建的,并且将 dotNet 标志设置为 true

WsHttpBinding 默认使用许多高级 WS-* 协议。例如 WS-Addressing,它是一组众所周知的 SOAP 标头。如果您未将它们包含在您的消息中,WCF 服务将拒绝该请求。

WebHttpBinding 也无法解决问题,因为该绑定用于 REST 服务,但您想通过 ksoap 使用该服务 = 您需要 SOAP 服务。

编辑:

关于堆栈溢出的另一个问题是nice example in the answer

【讨论】:

  • 不走运,我将绑定更改为 basicHttpBinding 并使用链接中的 android 代码,但是当我在 HttpTransportSE 对象上调用 call 方法时,我仍然收到 XmlPullParserException。消息是“意外类型(java.io.InputStreamReader@44e98d20 中的位置:END_DOCUMENT null@1:0)”
  • 在您的服务上打开 WCF 跟踪并使用 SvcTraceViewer 检查跟踪日志:msdn.microsoft.com/en-us/library/ms732023.aspx 它应该告诉您为什么 WCF 服务不喜欢来自您的 Android 客户端的请求。您还可以尝试为您的服务创建 .NET 客户端,并比较来自 .NET 客户端和 Android 客户端(由 Fiddler 捕获)的请求。
  • 感谢您的帮助。我发现这个名为 soapui 的非常有用的程序可以帮助我查看我的肥皂请求应该是什么样子。
  • stackoverflow.com/questions/11374416/… 尝试在android 中使用RESTful 客户端,Google Android 官方不支持SOAP。商业安卓应用总是推荐使用 RestFul 客户端。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 2019-10-21
相关资源
最近更新 更多