Skip to content

Instantly share code, notes, and snippets.

@pedroinfo
Last active September 10, 2025 17:31
Show Gist options
  • Select an option

  • Save pedroinfo/f19c301d866126d339110744fda23d17 to your computer and use it in GitHub Desktop.

Select an option

Save pedroinfo/f19c301d866126d339110744fda23d17 to your computer and use it in GitHub Desktop.
WCF Upload Tests
using System;
using System.IO;
using System.ServiceModel;
using System.Windows.Forms;
namespace WcfFileUploadClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSelectFile_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
txtFilePath.Text = ofd.FileName;
}
}
private void btnUpload_Click(object sender, EventArgs e)
{
string filePath = txtFilePath.Text;
if (!File.Exists(filePath))
{
MessageBox.Show("Selecione um arquivo válido.");
return;
}
var binding = new BasicHttpBinding
{
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue,
TransferMode = TransferMode.Buffered,
SendTimeout = TimeSpan.FromMinutes(10),
ReceiveTimeout = TimeSpan.FromMinutes(10)
};
var address = new EndpointAddress("http://localhost:8080/UploadService");
var channelFactory = new ChannelFactory<IUploadService>(binding, address);
var client = channelFactory.CreateChannel();
byte[] fileBytes = File.ReadAllBytes(filePath);
var file = new RemoteFile
{
FileName = Path.GetFileName(filePath),
Data = fileBytes
};
client.UploadFile(file);
MessageBox.Show("Upload concluído!");
}
}
}
using System.ServiceModel;
[ServiceContract]
public interface IUploadService
{
[OperationContract]
void UploadFile(RemoteFile file);
}
[MessageContract]
public class RemoteFile
{
[MessageHeader]
public string FileName { get; set; }
[MessageBodyMember]
public byte[] Data { get; set; }
}
public class UploadService : IUploadService
{
public void UploadFile(RemoteFile file)
{
string path = Path.Combine(@"C:\Uploads", file.FileName);
File.WriteAllBytes(path, file.Data);
Console.WriteLine($"Arquivo recebido: {file.FileName}, {file.Data.Length} bytes");
}
}
using System;
using System.ServiceModel;
class Program
{
static void Main()
{
Uri baseAddress = new Uri("http://localhost:8080/UploadService");
using (ServiceHost host = new ServiceHost(typeof(UploadService), baseAddress))
{
var binding = new BasicHttpBinding
{
MaxReceivedMessageSize = int.MaxValue,
MaxBufferSize = int.MaxValue,
TransferMode = TransferMode.Buffered,
SendTimeout = TimeSpan.FromMinutes(10),
ReceiveTimeout = TimeSpan.FromMinutes(10)
};
host.AddServiceEndpoint(typeof(IUploadService), binding, "");
host.Open();
Console.WriteLine("Servidor rodando em " + baseAddress);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment