我正在开发一个wcf服务来将数据文件及其相关信息流式传输到android客户端,但是我仍然是WCF的新手,只有大量的阅读和学习很多时候并不清楚哪种方法是最好的!
考虑到该服务将由 Android 设备调用,我应该采用哪种格式?考虑我的代码以及什么可以轻松实现,soap、json、rest 还是其他?
任何示例也将受到欢迎,尤其是带有我在下面定义的代码的示例。 谢谢!
[MessageContract]
public class DownlaodStreamItem
{
[MessageHeader]
public Int64 ItemID { set; get; }
[MessageHeader]
public Int64 SizeOfFile { set; get; }
[MessageHeader]
public String Name { set; get; }
[MessageBodyMember]
public Stream Data { set; get; }
}
[MessageContract]
public class someString
{
[MessageBodyMember]
public string SomeString{ set; get; }
}
服务
[OperationContract]
DownlaodStreamItem DownloadMessagecontact(someString SomeString);
服务:IService
public DownlaodStreamItem DownloadMessagecontact(someString SomeString)
{
DownlaodStreamItem DLITEM = new DownlaodStreamItem();
// Populate & return DownlaodStreamItem ....
return DLITEM;
}
最佳答案
对于android作为客户端和.NET作为服务,最好的方法是编写WCF REST服务。因为在这种情况下,你不需要在android端创建任何代理类,你只需发出Http请求即可使用它。
您可以使用 WCF REST 服务模板 40 (CS) 轻松编写 REST WCF 服务。 请引用this教你如何写。
然后您可以简单地在 android 上使用以下代码来使用此 REST 服务。希望这会对您有所帮助。
try {
String URL;
URL = "http://localhost/MyRESTSvc/DownloadStreamItem";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
InputStream inputStream = getResources().openRawResource(R.raw.task);
// Here task is any raw file you want to keep as input.. you may ignore it.
InputStreamEntity reqEntity = new InputStreamEntity(inputStream, -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
displayAlert("Success !!");
}
catch (ClientProtocolException e) {
displayAlert(e.getMessage());
}
catch (IOException e) {
displayAlert(e.getMessage());
}
catch (Exception e) {
displayAlert(e.getMessage());
}
关于c# - 流式下载从WCF到android,使用Json、Rest还是soap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9888036/