简体   繁体   中英

Program acting as client and server

I have a few questions regarding WCF: - Can a program act as both client and server ? - What's wrong with my code :

The service:

[ServiceContract]
public interface IShout
{
    [OperationContract]
    String Broadcast(String message);
}

The implementation:

public class eveShout : IShout
{
    public String Broadcast(String message)
    {
        return message + " reply";
    }
}

I start the service in the form contructor:

ServiceHost s = new ServiceHost(typeof(IShout));
s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
s.Open();

The, When I click a button on another form, I want to send a message and get a reply back. I use the following code:

ChannelFactory<IShout> channel = new ChannelFactory<IShout>(new BasicHttpBinding(), "http://localhost:9189");
IShout shout = channel.CreateChannel();

String reply = shout.Broadcast("Test");

Note: all the code is in the same namespace. Note: I first start the "server" (open) then the app continues.

when i run the code, the server is created. I use netstat -a to see if the port is open. when I run the command, i get 9189 is in listening state. but the code stops at the command reply = shout("test"). and I get anexception that says

The request channel timeout while waiting for a reply after 00:00:59...

Yes, you can have an app act as both client and server.

I see a couple of things that may need correcting. First, try adding OperationContract.

[ServiceContract]
public interface IShout
{
    [OperationContract]
    String Broadcast(String message);
} 

Then, take the type of the class, not the interface.

ServiceHost s = new ServiceHost(typeof(eveShout));
s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
s.Open(); 

Make sure you you have permission to access the namespace (s.Open() should throw an exception if it does not).

net http add urlacl url=http://+:9189/ user=...

See if these suggestions help.

(oh yeh, and make Broadcast public in your class)

A quick example WindowsFormsApplication looks like this...

// form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChannelFactory<IShout> channel = new ChannelFactory<IShout>(new BasicHttpBinding(), "http://localhost:9189");
            IShout shout = channel.CreateChannel();

            String reply = shout.Broadcast("Test"); 

        }
    }
}

// and Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ServiceModel;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ServiceHost s = new ServiceHost(typeof(eveShout));
            s.AddServiceEndpoint(typeof(IShout), new BasicHttpBinding(), "http://localhost:9189");
            s.Open(); 

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    public class eveShout : IShout
    {
        public String Broadcast(String message)
        {
            return message + " reply";
        }
    } 

    [ServiceContract]
    public interface IShout
    {
        [OperationContract]
        String Broadcast(String message);
    } 
}

See if you can get something as simple as this working. That will at least prove to you that it can be done and that the problem is somewhere else.

Enable WCF debugging.

The easiest way to do this is with WCF Service Configuration Editor. Open the utility and then browse to open your application's configuration file. From the diagnostics section simply click 'enable tracing'. The default tracing will be fine.

Once you run your application, the framework will dump a log file to a location specified in your configuration file. Double click to open it and read through the red events (these are the ones that have exceptions or unexpected outcome). It's very helpful and should help you identify where the problem is occurring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM