开发者

Winforms server side consumed by ASP.NET, WCF?

Where can I find a tutorial/sample o开发者_JS百科f asp.net page consuming a WCF service that uses a winforms application server side?

Thanks


You are mentioning two separate and independent operations:

  1. Consuming a WCF service from an ASP .Net application
  2. Creating a WCF service that uses a win forms application

Which of them is the actual problem? Exposing functionality as a service separates the service consumer from the service provider. The service consumer (the ASP .Net application in your case) will never need to know the service provider (the WCF application) is implementing its functionality behind the scenes. All it needs to know is the public interface exposed by the service.

Update

If you are new to WCF, the video tutorials available here might be a good starting point. They present you the basic knowledge of exposing as well as consuming a service with WCF.

Now, related to "that uses a winforms application server side". I assume that what you are trying to do is expose in the service some of the functionality available in the win forms application. If that is the case all you have to do is reference the exe of the forms app (with Add reference in Visual Studio) inside the WCF app and call all the needed methods from there.

Even cleaner from an architectural perspective would be to separate the user interface (UI) and business logic (BL) of your win forms application in separate projects, which will result in separate binary files after compilation (an exe file corresponding to the UI and a dll for the BL). Then you will only need to reference the BL corresponding dll in the WCF service.


OK, This is what you need to do:

  1. Create a Solution with a ASP.Net project in it. (Project A)
  2. Go to File > Add > New Project and add a Windows Forms application. (Project B)

So, now you have two projects in your solution,

  1. Add a reference from project B to project A.
  2. Add a method that returns the main form of project B in Program.cs, here is an example:

    public static class Program
    {
        public static MainForm mainForm;
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            mainForm = new MainForm();
            Application.Run(mainForm);
        }
    
        public static MainForm RunForm()
        {
            Main();
            return mainForm;
        }
    }
    
  3. Define a new Thread in Global.asax > Application_Start to call the RunForm method and store the result in a static variable. example:

    public static MainForm mainForm;
    public void Application_Start()
    {
        new Thread(
            () => { mainForm = Program.RunForm(); }
            ).Start();
    }
    

(Don't forget to use using B;)
(If you don't run the form application in another thread, your website doesn't load.)
So, now you can access the main form using Global.mainForm
You can define some methods for showing MessageBox in MainForm class, and call them from the website! (The form doesn't need to be shown at all)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜