使用来自android的流将大量数据发送到WCF服务

时间:2013-04-25 21:28:19

标签: android wcf

我目前正在开发一个wcf服务,它需要通过实体框架从数据库中读取数据并将数据传回客户端(在我的情况下是Android)。该服务支持REST和SOAP。 wcf服务托管在Windows服务下。但是,我没有成功将流上传到WCF并将流数据从WCF服务下载到android。我只在下面显示将流数据上传到wcf服务的功能。

有谁知道为什么它不起作用?我试图将流式数据从android传递到wcf服务或从wcf服务传递到android。它们都不起作用。我用于WCF和android的代码如下:

webhttpbinding也支持流吗?

在我的WCF服务中,app.config位于以下位置:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>

  <system.web>
    <compilation debug="true" />
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServBehave" name="iPhysio_Wcf_Service.Service">
        <host>
          <baseAddresses>
            <add baseAddress="http://xx.xxx.xxx.xxx:8733/iPhysio_Wcf_Service/Service/" />
          </baseAddresses>
        </host>
        <endpoint address="soapService" binding="basicHttpBinding" contract="iPhysio_Wcf_Service.IService" />
        <endpoint address="XMLService" behaviorConfiguration="restPoxBehavior" binding="webHttpBinding" contract="iPhysio_Wcf_Service.IService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServBehave">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <!--Behavior for the REST endpoint for Help enability-->
        <behavior name="restPoxBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
           type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.84.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="iPhysioCareDBEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SQLite;provider connection string='data source=&quot;C:\iPhysioCareDB&quot;'" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
</configuration>

服务接口的代码:

[OperationContract(Action = "UploadFile", IsOneWay = true)]
      [WebInvoke(Method = "POST", UriTemplate = "/Fileupload", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
       void AddMonitoringData(Stream stream);

服务代码:

  public void AddMonitoringData(Stream stream)
        {
            StreamReader reader = new StreamReader(stream);
            string text = reader.ReadToEnd();

            File.AppendAllText("c:\\wcf_service\\test.txt", text);

        }

这是我在Java中用于将流数据发送到wcf服务的安卓代码:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        String xml ="valid xml here. Testing here.";
        InputStream is = new ByteArrayInputStream(xml.getBytes());

                // Send GET request to <service>/GetPlates

         String XMLURL = "http://xxxx:8733/iPhysio_Wcf_Service/Service/XMLService";
                HttpPost request = new HttpPost(XMLURL + "/Fileupload");
                request.setHeader("Accept", "binary/octet-stream");
                 request.setHeader("Content-type", "binary/octet-stream");
                 InputStreamEntity reqEntity = new InputStreamEntity(is, -1);
                 reqEntity.setContentType("binary/octet-stream");
                    reqEntity.setChunked(false); // Send in multiple parts if needed
                  request.setEntity(reqEntity);
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpResponse response = httpClient.execute(request);

感谢您提前提供任何帮助。

0 个答案:

没有答案