使WCF服务与Android兼容

时间:2011-05-29 07:57:44

标签: android wcf http

我有一个使用IIS托管在远程服务器上的WCF服务。我想让这个服务与Android兼容,所以我可以在Android客户端上使用该服务。我遇到的问题是我正在尝试使用kso​​ap2连接到服务,但每次我尝试调用该方法时它都会给我400错误。我已经使用fiddler2尝试查看问题是什么,除了400错误之外,它还发送回内容长度:0(不知道是否是原因。有趣的是,当我指定内容时fiddler请求中的-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>

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

1 个答案:

答案 0 :(得分:3)

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

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

在你的android服务中,确保你指定正确的SOAP操作,更正名称空间,并为SOAP 1.1构造SoapSerializationEnvelope并将dotNet标志设置为true

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

WebHttpBinding也无法解决问题,因为该绑定用于REST服务,但您希望使用kso​​ap =您需要SOAP服务来使用该服务。

编辑:

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

相关问题