zoukankan      html  css  js  c++  java
  • WCF and Android Part I

    Introduction

    The Windows Communication Foundation (WCF) provides a very flexible way of exposing network  interfaces to other applications. For cross platform and inter programming language support basically two technologies can be used. SOAP and REST  services. The SOAP approach provides far more features out of the box but is not really suitable for mobile devices if speed matters. Therefore this article  describes how to create a REST WCF-Webservice which can be consumed on android devices.

    WCF part

    The WCF part is composed of three files, the service contract, the service implementation and the app.config.

    The service contract

    The service contract defines the webservice methods.

    To expose the service via REST, the WegGet attribute is required. It specifies the URL of each method, the serialization format (JSON or XML,  use JSON for fast processing). If the method has parameters they can either be provided using POST (requires another attribute) or they can be provided  by specifying them in the URL as shown in the example. The implementation of this method is straight forward and requires no attributes.

    [ServiceContract()]
    public interface ISecurityService
    {
        [OperationContract()]
        [FaultContract(typeof(WCFFault))]
        [WebGet(UriTemplate="test/{param1}", ResponseFormat=WebMessageFormat.Json)]
        void test(string param1);
    }

    app.config

    The application config associates different services with different endpoints and bindings. This configuration does not use any transport security  or other security mechnisms. For REST services the webHttp behaviour is important.

    <configuration>
    <system.serviceModel>
      <bindings>
       <webHttpBinding>
        <binding
         name="web_http"
         bypassProxyOnLocal="false"
         hostNameComparisonMode="WeakWildcard">          
       </binding>
      </webHttpBinding>
    </bindings>
            
    <behaviors>            
      <serviceBehaviors>
       <behavior name="http_behavior" >
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true"/>
       </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
       <behavior name="web_behavior">
        <webHttp helpEnabled="True" />
       </behavior>
      </endpointBehaviors>
    </behaviors>
          
    <services>
      <service name="ServiceImpl" behaviorConfiguration="http_behavior">
       <host>
        <baseAddresses>
         <add baseAddress="http://*:18000/my_service" />
        </baseAddresses>
       </host>
       <endpoint
        address="my_service"
        binding="webHttpBinding"
        bindingConfiguration="web_http"
        contract="IService"
        behaviorConfiguration="web_behavior"
       />
       <endpoint contract="IMetadataExchange" 
           binding="mexHttpsBinding" address="mex"/>
      </service>
    </services>
    </system.serviceModel>

    Run the service

    The service can be mounted to an application container (e.g. IIS) or can be self hosted with just a single line of code:

    new WebServiceHost(typeof(MyService)).Open();

    That's it, browse to http://localhost:18000/test/myparam. Next part (coming soon) will describe how to use the service with android.

    原文地址:http://www.codeproject.com/Articles/358867/WCF-and-Android-Part-I#

  • 相关阅读:
    TCP
    JAVA面向对象
    windows本地搭建easy-mock环境
    创建axios拦截器
    记录axios高效率并发的方法
    JS:数组中push对象,覆盖问题
    使用el-tree-transfer的方式
    从后台拿到echarts的数据值,求出百分比
    项目遇到的小问题(关于vue-cli中js点击事件不起作用和iconfont图片下载页面css样式乱的解答)
    JavaScript 常用内置对象(字符串属性、Math对象、Array数组对象)
  • 原文地址:https://www.cnblogs.com/ttssrs/p/2674041.html
Copyright © 2011-2022 走看看