Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hiii
I am new to WCF and I have written a code in Console Application which is working perfectly.
Now I have try to convert it into Windows Form Application.

I have created a service like this
C#
[ServiceContract]
public interface IHelloService
{
    [OperationContract]
    void SayHello(string msg);
}


and define the function
C#
public partial class Form1 : IHelloService
   {
       public string SayHello(string msg)
       {
           richTextBox1.Text=msg;
           return "Service on " + Environment.MachineName + " rec. " + msg;
}
   }


And I created a class Form1 with simple richTextBox1
C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            RichTextBox richTextBox1=new RichTextBox;
        }
    } 

and I am starting service from main program file
C#
static void Main(string[] args)
        {
            using(ServiceHost host = new ServiceHost(typeof(Form1)))
            {
            
                host.AddServiceEndpoint(typeof(IHelloService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloWcfService");
                host.Open();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }

        }


Now the problem is that WCF host receive data but not showing in richTextBox1
Thanks
Posted
Updated 3-Oct-18 23:45pm
v2

If you managed to develop a self-hosting WCF functionality, nothing really changes if you want a service part be a System.Windows.Forms application. WCF would have no value if it was interlaced with certain types of UI and anything else not related to communication technology.

With you service contract it looks like you want to show the string supplied by a client in the RichTextBox which is on the service side.
Just re-implement your contract method.

For example, assuming you have a form:
C#
using System.Windows.Forms;

//...

public partial class MyForm : Form {
    RichTextBox MyRichTextBox = new RichTextBox();
    //...
}


Implement your service interface in this form. For example, add a separate file with another part of the partial declaration:

C#
public partial class MyForm : IHelloService {
    void IHelloService.SayHello(string msg) {
        MyRichTextBox.Text = msg;
        //or better: 
        //MyRichTextBox.Rtf = msg; //and pass formatted RTF text
    }
}


Pay attention: you don't have to repeat ": Form" and ": IHelloService" in the inheritance lists of both partial class declarations — it is not needed which is very convenient. Also, I made interface implementation non-public using explicit implementation syntax (where any access modifier is not allowed). This syntax is much better as nobody can call the method IHelloService.SayHello accidentally.

—SA
 
Share this answer
 
v2
Comments
XeeShaN AbbAs 18-Aug-11 18:05pm    
Thanks for you help i have implement my code as you said now my code look like this
<pre lang="c#">

[ServiceContract]
public interface IHelloService
{
[OperationContract]
string SayHello(string msg);
}
namespace HelloWcfServiceHost
{
public partial class Form1 : IHelloService
{
string IHelloService.SayHello(string msg)
{
richTextBox1.Text = msg;
return msg;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
using (ServiceHost host = new ServiceHost(typeof(Form1)))
{
host.AddServiceEndpoint(typeof(IHelloService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloWcfServiceHost");
host.Open();

}
}
}


}
</pre>

this running successfully but now my wcf client not working it through exception of that host not running.

my code on client side is

<pre lang="c#">
IHelloService proxy = ChannelFactory<ihelloservice>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloWcfServiceHost"));
string msg;
msg = proxy.SayHello(textBox1.Text);
</pre>
Sergey Alexandrovich Kryukov 18-Aug-11 23:09pm    
You should better add this code using "Improve question", so you could preserve formatting which you could not do in a comment; in all cases, don't place in in the solution.

You don't ask a next question, so you may not need to show this code.
If you think my answer is somehow helpful, please formally accept it (green button).

If this is still a problem feel free to ask a follow-up question.
--SA
XeeShaN AbbAs 19-Aug-11 8:06am    
thanks for you reply...
i hve improved my question.
would you please look that for me...
This below works fine:
C#
public partial class Form1 : Form
    {
        [ServiceContract]
        public interface IHelloWorldService
        {
            [OperationContract]
            string SayHello(string name);
        }

        public class HelloWorldService : IHelloWorldService
        {
            public string SayHello(string name)
            {
                return string.Format("Hello, {0}", name);
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Loading(object sender, EventArgs e)
        {
            Uri baseAddress = new Uri("http://localhost:8758/hello");

            // Create the ServiceHost.
            ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress);
            
			ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
			smb.HttpGetEnabled = true;
			smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
			host.Description.Behaviors.Add(smb);
			host.Open();
		
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900