【发布时间】:2015-07-01 21:47:58
【问题描述】:
我有两个解决方案 TransferService 和 Sender。 TranferService 具有 WCF 服务和 IISHost 来托管该服务。在发件人解决方案中,我有 Windows 窗体应用程序。在该表单中,我使用按钮浏览和选择文件,使用文本框显示所选文件路径,使用另一个按钮(发送)通过 WCF 服务传输该文件。但我无法访问传输解决方案中的文本框值。它显示“当前上下文中不存在该名称”。
TransferService 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace TransferService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "TransferService" in both code and config file together.
public class TransferService : ITransferService
{
public File DownloadDocument()
{
File file = new File();
String path = txtSelectFilePath.Text;
file.Content = System.IO.File.ReadAllBytes(@path);
//file.Name = "ImagingDevices.exe";
return file;
}
}
}
我在这条线上遇到错误
String path = txtSelectFilePath.Text;
form.cs 的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sender
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Browse_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtSelectFilePath.Text = openFileDialog1.FileName;
}
}
private void Send_Click(object sender, EventArgs e)
{
TransferService.TransferServiceClient client = new TransferService.TransferServiceClient();
TransferService.File file = client.DownloadDocument();
System.IO.File.WriteAllBytes(@"C:\DownloadedFiles\" + file.Name, file.Content);
MessageBox.Show(file.Name + " is downloaded");
}
}
}
ITransferService.cs 的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace TransferService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ITransferService" in both code and config file together.
[ServiceContract]
public interface ITransferService
{
[OperationContract]
File DownloadDocument();
}
[DataContract]
public class File
{
[DataMember]
public string Name { get; set; }
[DataMember]
public byte[] Content { get; set; }
}
}
提前多谢了…………
【问题讨论】:
-
如果我正确理解你的问题:你不能。
-
我认为最好在 DownloadDocument(string path) 中添加一个字符串参数,并在调用时传递路径
-
您的回复......乔治先生,当我试图传递参数时,这个错误来了:-'TransferService.TransferService'没有实现接口成员'TransferService.ITransferService.DownloadDocument()'
-
当我创建 WCF 服务并实现该接口时,它生成了没有参数的函数。是这个原因吗??
-
是的,但我认为你可以通过创建一个构造函数来克服这个问题......用代码查看我的 awnser