一台桌面电脑,系统Win2003,一个笔记本电脑,win10系统,一部华为mate20 pro手机,准备在这三端通信,宿主放在Win2003上,客户端放在笔记本电脑和安卓手机上。
先建一个WCF服务库,将IService改名为IQueryFile,Service改名为QueryFile。
IQueryFile内容是这样
[ServiceContract]
public interface IQueryFile
{
[OperationContract]
[WebGet(UriTemplate=@"/mydocs",ResponseFormat =WebMessageFormat.Json)]
RemoteFile[] GetMydocuments();
// TODO: 在此添加您的服务操作
[OperationContract]
[WebInvoke(UriTemplate=@"/files",RequestFormat =WebMessageFormat.Json, ResponseFormat =WebMessageFormat.Json,Method ="POST")]
RemoteFile[] GetFiles(string path);
}
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
public class RemoteFile
{
public string Name { get; set; }
public DateTime FileTime { get; set; }
}
QueryFile.cs内容
[ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]
public class QueryFile : IQueryFile
{
public RemoteFile[] GetFiles(string path)
{
RemoteFile[] remoteFiles = GetRemoteFiles(path);
if (remoteFiles.Length > 0)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
}
else
{
WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
}
return remoteFiles;
}
public RemoteFile[] GetMydocuments()
{
RemoteFile[] remoteFiles= GetRemoteFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
if (remoteFiles.Length > 0)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
}
else
{
WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
}
return remoteFiles;
}