Question : Sending and Receiving Data like a Dataset using WCF between two or more programs over internet without IIS Configurations

Hello all,

I need to run a program on a computer server that listens a specific port and send/receive data over the Internet without IIS, because I do not want that the end user needs of having to configure anything other than a free port in your firewall. On the other hand, the client side, I need that the program send/receive a Dataset with the server program with all kind of data, like images, texts, movies or pdf files. I'm trying to use WCF with a winforms application. Can anyone give me an example of how to do this?

Thanks in advance.

Answer : Sending and Receiving Data like a Dataset using WCF between two or more programs over internet without IIS Configurations

Ok. This is a *very* basic example. You basically create a ServiceHost object to host your service. Give it an endpoint (mine is just running locally). You also want to define a behavior object so you can enable the Mex, if you so choose. Once you have your service defined, you can run it. Navigate to the endpoint you setup in a browser and you should get the "You have created a service" welcome page. You can copy the line similar to

    svcutil.exe http://localhost:10000/?wsdl

and then open up the Visual Studio Command Prompt. Paste the line into the command window and execute it. It should generate two files--a class file, which is basically your proxy to the service, and a config file. Add both files to your client project/application. Rename "output.config" to "app.config" or add the contents of "ouput.config" to your "app.config" if it already exists. Once you have done that, you should be able to create a "proxy" object and call the method you defined in your service/interface. See below.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
/////////////////////////////////////////////////
////                                         ////
////           Service Definition            ////
////                                         ////
/////////////////////////////////////////////////

///////////////////////////
//  Contract (Interface)
///////////////////////////
using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace ServiceDefintion
{
    [ServiceContract()]
    public interface IServiceContratct
    {
        [OperationContract()]
        bool SendMessage(string text);
    }
}

///////////////////////////
//  Implementation
///////////////////////////

using System;
using System.Text;

namespace ServiceDefintion
{
    class TestService : IServiceContratct
    {
        #region ServiceContratct Members

        public bool SendMessage(string text)
        {
            try
            {
                Console.WriteLine(text);
            }
            catch
            {
                return false;
            }

            return true;
        }

        #endregion
    }
}


///////////////////////////
//  Hosting Code
///////////////////////////


using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace ServiceDefintion
{
    class Program
    {
        static void Main()
        {
            ServiceHost host = new ServiceHost(typeof(TestService), new Uri[] { new Uri("http://localhost:10000")});
            ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
            BasicHttpBinding binding = new BasicHttpBinding();

            behavior.HttpGetEnabled = true;
            host.Description.Behaviors.Add(behavior);

            host.AddServiceEndpoint(typeof(IServiceContratct), binding, "TestService");
            host.AddServiceEndpoint(typeof(IMetadataExchange), new BasicHttpBinding(), "MEX");

            host.Open();
            Console.ReadKey();
            host.Close();
        }
    }
}


/////////////////////////////////////////////////
////                                         ////
////           Client Definition             ////
////                                         ////
/////////////////////////////////////////////////

using System;

namespace WCF_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceContratctClient proxy = new ServiceContratctClient();
            string line = Console.ReadLine();

            while (line != "end")
            {
                proxy.SendMessage(line);
                line = Console.ReadLine();
            }            
        }
    }
}
 
Screenshot of results
Screenshot of results
 
Random Solutions  
 
programming4us programming4us