Here is how you can create a WCF service.
Assumption made:
Let’s say you have a list of tblExcel objects that you want to send to a WCF service from you WinForm client application shown in your question.
Step 1:
Create a new Class Library project and name it ExcelDataService
In this project create a new folder called “DataContracts” and under this folder create a new class with the following definition:
[DataContract]
public class ExcelData
{
[DataMember]
public string Sid { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
}
Note: tblExcel is renamed as ExcelData, the definition of the class is the same you posted in original question.
Step 2:
Create another folder named “ServiceContracts”under ExcelDataService project and create a new interface with the following definition
[ServiceContract]
public interface IExcelDataService
{
[OperationContract]
bool SaveData(List<ExceData> data);
}
Step 3:
Next create another folder and name it “Services” and create a new class with the following definition
public class ExcelDataService : IExcelDataService
{
public bool SaveData(List<ExceData> data)
{
// Showing the code how to save into SQL is beyond this question.
// In the data object you have the list of excel data objects that you can save into the sql server
// you can use Enterprise Library Data block or ADO.Net to save this data into the SQL Server.
}
}
Step 4a:
Now in the visual studio solution add a new project and name it ExcelDataServiceConsoleHostManager, set the type of project as Console Application. In the Main method write the following code:
using (ServiceHost host = new ServiceHost(typeof(ExcelDataService)))
{
PrintEndpoints(host.Description);
host.Open();
Console.WriteLine("Service(s) are up and running... Press Enter key to exit!");
Console.ReadLine();
}
Step 4b:
Add another static method with the following definition:
static void PrintEndpoints(ServiceDescription desc)
{
Console.WriteLine(desc.Name);
foreach (ServiceEndpoint nextEndpoint in desc.Endpoints)
{
Console.WriteLine();
Console.WriteLine(nextEndpoint.Address);
}
}
Step 5:
In the App.config file of this project add the following configuration:
<system.serviceModel>
<services>
<service name="ExcelDataService.Services.ExcelDataService">
<endpoint address="net.tcp://localhost:8887/ExcelDataService/"
binding="netTcpBinding"
contract="ExcelDataService. ServiceContracts.IExcelDataService"
></endpoint>
</service>
</services>
</system.serviceModel>
Make sure all the references are added into the project. Once build is successful hit F5 key to start the Excel Data Service Console Host manager.
Next step is modifying your client application:
Step 6:
Add a reference of “ExcelDataService.dll” in your client application.
Create a new class with the following definition:
public class ExcelDataServiceClient : ClientBase<IExcelDataService>
{
public bool SaveData(List<ExcelData> excelData)
{
base.Channel.SaveData(excelData);
}
}
Step 7:
Add app.config file into your client (if already not added) and paste the following configuration
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:8887/ExcelDataService/"
binding="netTcpBinding"
contract="ExcelDataService. ServiceContracts.IExcelDataService"></endpoint>
</client>
</system.serviceModel>
Save all files and resolve any references (WCF uses System.ServiceModel.dll).
Step 8:
Next create a new instance of ExcelDataServiceClient class and call its instance method SaveData.
I would wrap the call from the client into a try-catch block to catch any exception.
Edit 2:
To send a file to WCF service we have
Request class that client will use to send the file...
[DataContract]
public class UploadFileRequest
{
public string FileName { get; set; }
public string Path { get; set; }
public byte[] FileContents { get; set; }
}
and the response class that service will send back:
[DataContract]
public class UploadFileResponse
{
public string Message { get; set; }
}
add another method to the IExcelDataService interface:
[OperationContract]
UploadFileResponse UploadFile(UploadFileRequest request);
and its implementation in the ExcelDataService class:
public UploadFileResponse UploadFile(UploadFileRequest request)
{
// In the request object you have the file as byte array that can be used here.
}
On the client side add this method in the ExcelDataServiceClient class
public string UploadFile(byte[] fileContent, string fileName = "", string filePath = "")
{
UploadFileRequest request = new UploadFileRequest()
{
FileContents = fileContent, FileName = fileName, Path = filePath
};
UploadFileResponse response = base.Channel.UploadFile(request);
return response.Message;
}
Next use the instance of this client class to call UploadFile method and pass in the parameters.
Hope this helps!