简体   繁体   中英

Calling a function of interface C#

I have a solution that includes two projects: server and GUI. In the GUI project there's a WinForm application, and in the server project there's a Webservice. I know that it's slightly incorrect, but the way it works is as following: my GUI has an instance of the server, and my server has a reference to an interface which the GUI implements. In the GUI constructor it initializes the webservice and sets its reference to itself, so that the server can call the functions of the GUI.

The problem is that there is some problem with calling the GUI functions from the server, and it throws some exception that I cannot find (since that I'm debugging the GUI project). Did I write something wrong? Or is it completely impossible to call functions of an interface?

UPDATE: I think that the problem is that the Webservice that's running and handling the http messages is different from the instance that I create in the GUI somehow. Maybe making the webservice static will help?

code: server:

public class WS : System.Web.Services.WebService
{
    ALUMA_GUI gui;

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public void registerUser()
    {
        try
        {
            gui.addUser(tempUser); //This is where it fails
        }
        catch(Exception e)
        {
            HttpContext.Current.Response.Write(e.Message);
        }
    }
 }

GUI (implements ALUMA_GUI):

public MainForm()
    {
        try
        {
            InitializeComponent();
            server = new WS();
            server.setGUI(this);
        }
        catch (Exception e)
        {
        }
    }

Any kind of help would be really appreciated...

Services don't interact with presentation layers in this manner. For the server to be able to call functions in your winforms app, you'd have to serialize the entire app and its functionality and pass it to the server. Even then, you can't pass the state (re: this ) across the web (stateless). My assumption reading the names of your methods is that you're attempting to use the gui to register a new user, and then have the gui update its current registry of users in local memory.

The proper way to do that would be to have both the gui and the web service operate against the interface of what a user object looks like. Have the gui "register" the user (I'm assuming that means insert into a database), and then return the user object to the gui itself. Then the gui is responsible for making sure the returned user is added properly to any local stored memory.

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